• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

JButton in Swing

I am using GridBagLayout for rendering the 6 Buttons in
B1 B2
B3 B4
B5 B6
But it screwed out .I can't even see some buttons.
What should be the numbering for it.
Thanks.

private final JComponent getNewMonitorButtonsPane() {
    BasicCompositePane wAnswer = new BasicCompositePane(ChannelMode.WRITE);
    wAnswer.setLayout(new GridBagLayout());
 
    wAnswer.setOpaque(false);
   
    for (int i=0; i<getMonitorButtons().length; i++) {
      wAnswer.add(getMonitorButtons()[i], ViewUtilities.getGridBagConstraints(
        i, i, 5+i, 5, .50, 0.5, GridBagConstraints.NORTHWEST,
        GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
    }
return wAnswer;
}
0
Anj78
Asked:
Anj78
1 Solution
 
Bart CremersJava ArchitectCommented:
Just guessing here what BasicCompositePane and ViewUtilities.getGridBagConstraints actually do, but try this:
               
      wAnswer.add(getMonitorButtons()[i], ViewUtilities.getGridBagConstraints(
            i % 2, i / 2, 1, 1, .50, 0.5, GridBagConstraints.NORTHWEST,
            GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));


The first to parameters could be the column and row for the component. So, using the given math it will add the buttons alternating in column 0 and 1 and place each next 2 buttons on a new row.

The 3rd and 4th parameter tell us about the gridwidth and height. In your case they should simply be 1 as every button will ge exact the same amount of space. I left all other parameters as you provided them, because they wont do much in the given example.

Check out this http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html for more info on how to work with GridBagLayout
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now