Problems with GridBagLayout
I have a frame sized 640x480, which I want to divide horizontally into 2 parts, one taking 1/3 of the total width and the other the rest. The former section will contain some buttons, the latter some TextFields.
I've been trying GridBagLayout and BoxLayout but I am not able to get the 1/3 - 2/3 division.
Re: Problems with GridBagLayout
Quote:
Originally posted by irona20
I have a frame sized 640x480, which I want to divide horizontally into 2 parts, one taking 1/3 of the total width and the other the rest. The former section will contain some buttons, the latter some TextFields.
I've been trying GridBagLayout and BoxLayout but I am not able to get the 1/3 - 2/3 division.
Well, your problem is that you need to set the weight of the GridBagConstraints. The left side should have a weight of 1, the right side should have a weight of 2.
If your main class extends JFrame, you can do this by saying:
Code:
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
getContentPane().setLayout(gbl);
gbc.weightx = 1.0;
gbc.weighty = 1.0;
addLeftElements();
gbc.weightx = 2.0;
gbc.weighty = 2.0;
addRightElements();
This will divide it up appropriately. If you don't know how to add elements and apply them to constraints, then say so and I'll tell you.
(when specifiying a weight, every item you add after specifying a weight will have that weight. So if you want to change the weight for every item, you'll have to say those two lines of code before you add every item)
Re: Re: Problems with GridBagLayout
Quote:
Originally posted by Demonpants Software
(when specifiying a weight, every item you add after specifying a weight will have that weight. So if you want to change the weight for every item, you'll have to say those two lines of code before you add every item)
only if you reuse the gbc ;)
i think my netbeans makes a new one each time...