Anyone here familiar with the GridBadLayout in Java? I'm trying to make a GUI and I think that GridBagLayout would be the best fit. Here's a picture of what I have in mind:
I've been reading about GridBagLayout on Oracle, but I'm confused by a few things. The way I was taught to do a GUI was as follows:
Looking at the article, the if(shouldFill) and if(shouldWeightX) conditional statements confuse me. Do I need anything like that for what I want to do?
How would I add the first two rows from my drawing to a GridBagLayout GUI? Assuming I have c as my Container, label as my JLabel, field as my JTextField, and dropdown as my JComboBox, would it be something like:
That code would go in the constructor. If possible, I'd like to do this as close to the way I learned as possible to avoid confusion. Any advice?
Thanks!
I've been reading about GridBagLayout on Oracle, but I'm confused by a few things. The way I was taught to do a GUI was as follows:
Code:
//example.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class example extends JFrame implements ActionListener
{
Container c;
JButton button;
public example()
{
super("title goes here");
c = getContentPane();
c.setLayout(new FlowLayout());
button = new JButton("push me");
button.addActionListener(this);
c.add(button);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
//do whatever here
}
public static void main (String args[])
{
example application = new example();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
How would I add the first two rows from my drawing to a GridBagLayout GUI? Assuming I have c as my Container, label as my JLabel, field as my JTextField, and dropdown as my JComboBox, would it be something like:
Code:
c.setLayout(new GridBagLayout());
GridBagConstraints layout = new GridBagConstraints();
layout.gridx = 0;
layout.gridy = 0; //place it at position (0, 0)
layout.add(label, c);
layout.gridx = 0;
layout.gridy = 1; //place it at position (0, 1)
layout.add(field, c);
layout.gridx = 1;
layout.gridy = 1; //place it at position (1, 1)
layout.add(dropdown, c);
Thanks!