So here you go.
First off, GUI development is quite verbose and can be painful to debug. BUT, a consistent plan is followed it isn’t to hard to figure out where things go wrong.
The biggest key with using GridBagLayout and GridBagConstraints is understanding how the different components relate to one another and then group them with panels accordingly.
Here is the code:
package swing;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class JoesAutomotiveMain {
public JoesAutomotiveMain() {
this.init();
}
private void init() {
JFrame jframe = new JFrame();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jframe.setLayout(gbl);
jframe.setTitle("Joe's Automtive");
JPanel topjpanel = createTopJPanel();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
jframe.add(topjpanel, gbc);
JPanel totaljpanel = createTotalJPanel();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
jframe.add(totaljpanel, gbc);
JPanel buttonjpanel = createButtonJPanel();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
jframe.add(buttonjpanel, gbc);
jframe.setSize(400, 300);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
public static void main(String[] args) {
new JoesAutomotiveMain();
}
public JPanel createTopJPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
JPanel leftjpanel = createMaintenanceServicesJPanel();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
jpanel.add(leftjpanel, gbc);
JPanel rightjpanel = createTopRightPanel();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
jpanel.add(rightjpanel, gbc);
return jpanel;
}
public JPanel createTopRightPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
JPanel miscservicesjpanel = createMiscServicesJPanel();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
jpanel.add(miscservicesjpanel, gbc);
JPanel partsandlaborjpanel = createPartsAndServicesJPanel();
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
jpanel.add(partsandlaborjpanel, gbc);
return jpanel;
}
public JPanel createMaintenanceServicesJPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
jpanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Maintenance Services"));
JCheckBox jcheckBox = new JCheckBox("CheckBox1");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jcheckBox, gbc);
jcheckBox = new JCheckBox("CheckBox2");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jcheckBox, gbc);
jcheckBox = new JCheckBox("CheckBox3");
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jcheckBox, gbc);
return jpanel;
}
public JPanel createMiscServicesJPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
jpanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Misc. Services"));
JLabel jlabel = new JLabel("Total Misc. Services");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jlabel, gbc);
JTextField jtextfield = new JTextField();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jtextfield, gbc);
return jpanel;
}
public JPanel createPartsAndServicesJPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
jpanel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Parts and Labor"));
JLabel jlabel = new JLabel("Parts");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.0;
jpanel.add(jlabel, gbc);
JTextField jtextfield = new JTextField();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jtextfield, gbc);
jlabel = new JLabel("Labor");
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 0.0;
jpanel.add(jlabel, gbc);
jtextfield = new JTextField();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = 1.0;
jpanel.add(jtextfield, gbc);
return jpanel;
}
public JPanel createTotalJPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
JLabel jlabel = new JLabel("Total JPanel");
gbc.gridx = 0;
gbc.gridy = 0;
jpanel.add(jlabel, gbc);
return jpanel;
}
public JPanel createButtonJPanel() {
JPanel jpanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
jpanel.setLayout(gbl);
JLabel jlabel = new JLabel("Button JPanel");
gbc.gridx = 0;
gbc.gridy = 0;
jpanel.add(jlabel, gbc);
return jpanel;
}
}
I didn’t write everything for you, but this should give you a reasonable start.
Now depending on how you have to handle getting values you might want to look at the law do demeter. You might consider extending JPanel and adding getters for the different possible values of the components in that specific panel. You could use inner classes to accomplish that behavior if you wanted.
Also, how to handle button clicks? I always extend JButton with a class called ExtendedJButton that is a listener of itself and then use an anonymous inner class to handle the button click by overriding the handleEvent method…
package swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class ExtendedJButton extends JButton implements ActionListener {
// Override all constructors and call super with the correct arguments but add this as a listener of itself...
public ExtendedJButton() {
super();
this.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(this + " actionPerformed method needs to be overrided!");
// You could also consider throwing a org.apache.commons.lang.NotImplementExcetpion but you would have to use the apache commons library.
}
}
Then use the above button the following way:
JButton jbutton = new ExtendedJButton() {
public void actionPerformed(ActionEvent e) {
// your code here... most likely delegating to a method in your JFrame class or a Service.
}
};
// Do your GridBagConstraint work here.
And there you go.
Like I said before GUI programming is quite verbose and it takes a lot of work to get everything just right.
Others on this site might disagree with the above code and statements, but I’ve seen the above work for production systems and have absolutely blown away some serious java haters with how nice the gui can look/behave in java.