-1

GridBagLayout is not working as expected. I can not find where I'm making the mistake. I know that somewhere I'm doing some silly mistake but can't find it.

Here I have made 3 buttons, 4 text fields and 3 combo boxes, and I want to align them in proper order using GridBagLayout.

Code is as follows :

    GridBagConstraints c = new GridBagConstraints();
            JPanel secondPanel = new JPanel();
            f=new JFrame("Server Side");
            b=new JButton("3DES ALGO");
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 0;
            secondPanel.add(b, c);    
            b1=new JButton("Browse");
            c.gridx = 2;
            c.gridy = 0;
            secondPanel.add(b1, c);
                b2=new JButton("Color");
            c.gridx = 0;
            c.gridy = 0;
            secondPanel.add(b2, c);
            comboBox1 = new JComboBox(s);
            c.gridx = 0;
            c.gridy = 1;
            secondPanel.add(b, c);
                comboBox2 = new JComboBox(s);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 1;
            secondPanel.add(b, c);
                comboBox3 = new JComboBox(s);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 1;
            secondPanel.add(b, c);

            b10=new JButton("OK");

            tf=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 1;
            secondPanel.add(tf, c);

            tf1=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 2;
            secondPanel.add(tf1, c);
                tf2=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 2;
            secondPanel.add(tf2, c);
                tf3=new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 2;
            c.gridy = 2;
            secondPanel.add(tf3, c);

            b.addActionListener(this);
            b1.addActionListener(this);
            b2.addActionListener(this);

            b.setBounds(200,0,330,50);
            b1.setBounds(500,100,90,30);
            b2.setBounds(0,0,70,30);

            Container content = f.getContentPane();
            content.setBackground(Color.RED);
            b.setForeground(Color.white);
            b.setBackground(Color.black);
            b1.setForeground(Color.white);
            b1.setBackground(Color.black);
            b2.setForeground(Color.white);
            b2.setBackground(Color.black);
 tf.setBackground(Color.CYAN);
            tf1.setBackground(Color.GRAY);
            tf1.setForeground(Color.white);

            tf2.setBackground(Color.GRAY);
            tf2.setForeground(Color.white);
                tf3.setBackground(Color.GRAY);
            tf3.setForeground(Color.white);

            f.add(b);
            f.add(b1);
            f.add(b2);


            secondPanel.setLayout(new GridBagLayout());
            secondPanel.add(comboBox1);
            secondPanel.add(comboBox2);
            secondPanel.add(comboBox3);
            f.add(tf);
            f.add(tf1);
            f.add(tf2);
            f.add(tf3);
                f.add(secondPanel);
             f.setLayout(new GridBagLayout());

            f.addWindowListener(new WindowEventListener());
            f.pack();

            f.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
rocky
  • 1
  • 6
  • 2
    "but its not working properly" - that doesn't tell us anything about what you expected vs what happens. – Jon Skeet Apr 02 '15 at 18:15
  • 1
    `i want to allign them in proper order` - We don't know what your definition of proper order is. – camickr Apr 02 '15 at 18:16
  • i want to allign the 3 buttons in single row, then in next row i want to have the 3 combobox. and in the last row i want to have 3 TextFields – rocky Apr 02 '15 at 18:19
  • @rocky, yes, well your original question stated you had 4 text fields which makes the question confusing. Be careful when you ask a question, we can't assume what you are asking. – camickr Apr 02 '15 at 18:31
  • @rocky--I would be remiss not to follow up on this with suggestion to consider what @Splungebob has to say [here](http://stackoverflow.com/questions/21010799/looking-for-general-method-for-gridbaglayout-component-creation). With careful planning and a manageable learning curve, `GridBagLayout` is relatively easy with his code, as demonstrated by my "answer" at the bottom of the linked page. – DSlomer64 Apr 02 '15 at 19:04

2 Answers2

1

The grids are 0 based, so your basic code would be:

c.gridx = 0;
c.gridy = 0;
panel.add(b1, c);
c.gridx = 1;
panel.add(b2, c);
c.gridx = 2;
panel.add(b3, c);
c.gridx = 0;
c.gridy = 1;
panel.add(combo1, c)
c.gridx = 1;
panel.add(combo2, c);
...
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Actually, when a GridBagConstraints is created, gridx and gridy are initialized to GridBagConstraints.RELATIVE. – VGR Apr 02 '15 at 19:42
  • @vgr, oops, I guess its good that I always manually set the gridx and gridy values to 0. Post updated, thanks. – camickr Apr 02 '15 at 19:51
1

I'm pretty sure you need to set the layout to a GridBagLayout before adding your components to secondPanel.

I also see that you add your JComboBoxes without any constraints at all, which almost certainly is not what you want to do.

Also, I see you are calling setBounds. Don't do that. It is a LayoutManager's job to set child components' bounds, not yours.

Although the documentation isn't as clear as it could be, GridBagConstraints is a lot easier to use when you make use of GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER. gridx and gridy are set to RELATIVE by default, probably because it's usually the easiest use case.

I find my code is easiest to read if I create all of my components first, then add them to a layout.

So, if you want to lay out your buttons and fields in three rows, you could do this:

b = new JButton("3DES ALGO");
b1 = new JButton("Browse");
b2 = new JButton("Color");

b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);

comboBox1 = new JComboBox(s);
comboBox2 = new JComboBox(s);
comboBox3 = new JComboBox(s);

tf = new JTextField(10);
tf1 = new JTextField(10);
tf2 = new JTextField(10);
tf3 = new JTextField(10);

GridBagConstraints c = new GridBagConstraints();
JPanel secondPanel = new JPanel(new GridBagLayout());

c.fill = GridBagConstraints.HORIZONTAL;

// Components are easier to see and use if there is some space between them.
c.insets.right = 6;
c.insets.bottom = 6;

// At this point, c.gridx and c.gridy are both GridBagConstraints.RELATIVE.
// c.gridwidth and c.gridheight are 1.

secondPanel.add(b, c);
secondPanel.add(b1, c);
// A gridwidth of REMAINDER means "This is the last component on this row."
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(b2, c);

// Since the last component was added with a width of REMAINDER, the next
// component is automatically on the next row, as long as gridx and gridy
// are still set to GridBagConstraints.RELATIVE.

c.gridwidth = 1;
secondPanel.add(tf, c);
secondPanel.add(tf1, c);
secondPanel.add(tf2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(tf3, c);

c.gridwidth = 1;
secondPanel.add(comboBox1, c);
secondPanel.add(comboBox2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(comboBox3, c);
VGR
  • 40,506
  • 4
  • 48
  • 63