I have a JPanel
with three buttons and one label. One button and the label are in the first row and the other two buttons are in the second row. I need the label to be at the upper right coner, but in doing so I cause the label to increase unexpectedly and to push the two down buttons to the left.
public class UserInterface extends JFrame {
int width;
int height;
JPanel panel;
public static void main(String[] args) {
run();
}
public UserInterface() {
setup();
}
private void setup() {
width=800;
height=600;
panel=new UserInterfacePanel();
getContentPane().add(panel, BorderLayout.NORTH);
setSize(width, height);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void run() {
UserInterface gui=new UserInterface();
gui.setVisible(true);
}
}
class UserInterfacePanel extends JPanel {
private JToggleButton startButton;
private JToggleButton stopButton;
private JButton homeStatusButton;
private JLabel timeStatusLabel;
public static void main(String[] args) {
}
public UserInterfacePanel() {
setup();
}
private void setup() {
setLayout(new GridBagLayout());
setupButtons();
timeStatusLabel=new JLabel();
timeStatusLabel.setMinimumSize(new Dimension(180,200));
timeStatusLabel.setPreferredSize(new Dimension(180,200));
timeStatusLabel.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx=0;
c1.weighty=0;
c1.anchor=GridBagConstraints.CENTER;
c1.gridx=0;
c1.gridy=0;
c1.insets=new Insets(20, 20, 250, 50);
add(homeStatusButton, c1);
c1.gridx=1;
c1.gridy=0;
c1.insets=new Insets(0, 50, 10, 10);
c1.weightx=1;
add(timeStatusLabel, c1);
GridBagConstraints c2=new GridBagConstraints();
c2.insets=new Insets(30,20,30,20);
c2.anchor=GridBagConstraints.CENTER;
c2.weightx=0.0;
c2.gridx=0;
c2.gridy=1;
add(startButton, c2);
c2.gridx=1;
c2.gridy=1;
add(stopButton, c2);
}
private void setupButtons() {
startButton=new JToggleButton("Start Button");
stopButton=new JToggleButton("Stop Button");
homeStatusButton=new JButton("OUT");
homeStatusButton.setBackground(Color.RED);
homeStatusButton.setForeground(Color.BLACK);
homeStatusButton.setSize(new Dimension(100, 20));
}
}
I don't understand why it's happening. It looks like I stated implicitly the positions of the buttons as well as the position of the label. Why does the label gets big and the buttons get pushed to the left.
but it's still too big of a label
You specify a size of (180, 200) with additional insets of (250, 50).
too big of a push for the buttons
the constraints are used by all components unless you reset the constraint each time you add a component to the panel.
So the insets for the label are also used for the buttons, unless you reset the insets.
Also, remember that the GridBagLayout will position compnents withing the cell. So the cell with of the first column is equal to the largest width of the two buttons. The width of the second column will be the width of the label, since it is the largest component in that column.
If you want the start/stop buttons to be together, then add them to a panel first and then add the button panel to the GridBagLayout panel.