JButton Image Ignoring GridBagConstraints
- by daemor
I am working on an application and have a screen that needs to have elements (namely some custom JButtons) appear and disappear based on a user selection. However for some reason, when I add these buttons to their pane, the buttton image goes to the top corner, and leaves the text in the center, completely ignoring GridBagConstraints. I am completely stumped on this one as I have done this same exact thing dozens of times earlier in the program without any issues. Here is an image of the problem:
The problem is in this method here, and occurs down towards the bottom.
public void init(){
contentPane.removeAll();
// Setup jlabels
JLabel countyLabel = new JLabel("County");
countyLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18));
JLabel measureByLabel = new JLabel("Measure By: ");
measureByLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18));
String[] countyChoices = {"Washtenaw", "Oakland", "Livingston"};
// setup components
JComboBox<String> countyCombo = new JComboBox<String>(countyChoices);
// place baseComponents
c.weightx = 0.5;
c.weighty = 0.5;
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.NORTH;
contentPane.add(countyLabel, c);
c.gridx = 2;
contentPane.add(countyCombo, c);
c.gridy = 1;
c.gridx = 0;
contentPane.add(trenchButton, c);
c.gridx = 2;
contentPane.add(bedButton, c);
c.gridy = 2;
c.gridx = 1;
contentPane.add(systemSelection, c);
c.gridy = 3;
c.gridx = 0;
contentPane.add(lengthButton, c);
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 4;
c.gridy = 4;
c.gridx = 0;
contentPane.add(choicePane, c);
GridBagConstraints con = new GridBagConstraints();
con.weightx = 0.5;
con.weighty = 0.5;
con.gridx = 0;
con.gridy = 0;
choicePane.add(lengthButton, c);
// revalidate and repaint
choicePane.revalidate();
choicePane.repaint();
contentPane.revalidate();
contentPane.repaint();
}
I have tried doing this in separate methods, the button looks fine when added to the contentPane, the pane is for sure set to gridbagconstraints as I used the expression
JPanel choicePane = new JPanel(new GridBagLayout())
to initialize it.