More than 1 JPanel in GridBagLayout - java

Hello please see the attached image ,i am coding interface that has three JPanels and i am using GridBagLayout , i have read some good tutorials and got some understanding as well , but i need your guidance regarding placing 2 JPanels side by side as in given pic .
for example if i do frame.add(leftpanel); and then there is only one panel on the JFrame .. how to align it left on half of the JFrame so that when i do frame.add(panelright); it is added to right sid ,
please guide me to do the functionality shown in Pic ,
i can handle 1 JPanel and all the components but dont know abut to handle more than 1 ,

You need to specify the position and other settings using GridBagConstraints.
For example, you could do something like this:
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.BOTH;
frame.add(leftPanel, constraints);
constraints.gridx = 1;
frame.add(panelright, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
frame.add(bottomPanel, constraints);
GridBagConstraints.gridx and GridBagConstraints.gridy determine the row and column for this element. fill tells the layout to use all the available space both horizontally and vertically.
If you want to set that one cell will use a space with certain proportion relative to other cells, you can use the weightx and weighty fields.

I think in your case BorderLayout will be more useful. Try reading about it here.
You can add your panels north south east west or at the center it will be much better than GridBag in this case.

Also consider two JSplitPane instances. The outermost pane would have a VERTICAL_SPLIT between top and bottom. The top of the outer pane would contain another JSplitPane having a HORIZONTAL_SPLIT between left and right.

Related

How to fill JFrame window with a JPanel?

I am trying to set a JPanel fill the whole window of a JFrame. The layout of JPanel is GridBagLayout. If i change it to BorderLayout it works. But i need the GridBagLayout and in this way the JPanel appears on the Center of the window.
How can i fill the whole window of JFrame with the JPanel( just like with BorderLayout )?
First, make sure you read How to Use GridBagLayout
Next, make sure you have the JavaDocs for GridBagConstraints open and available
These two basic references will provide you with 99% of the information you need to answer just about most of you issues.
Without more context, something like...
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
will achieve the result you're looking for
Remember though, you are not stuck to a single layout manager, you can use multiple containers all using different layout managers to achieve your desired results

Placing GridLayout JPanels onto a GridBagLayout JFrame

I'm trying to place two JPanels onto a JFrame using GridBagLayout.
The first JPanel uses gridLayout to create 35 columns and 27 rows of JButtons, and should have a width of 3/4 of the JFrame. This panel needs to fill the vertical space available to it.
The second JPanel also uses gridLayout and should fill the last 1/4 of the main JFrame.
Unfortunately, my first JPanel (sPan) isn't even fitting properly on the screen, and is forcing the whole second JPanel (cPan) off of the screen. How can I constrain these values to take up only their allowed proportion on the screen without them moving each other around?
If I use a blank JPanel with a background colour with my code, everything works out perfectly fine:
[1]
However, when I use my JPanel consisting of JButtons, the proportions get completely messed up:
[2]
I speculate that when I instantiate my sPan object, it's sizing each button to accommodate the size of the whole JFrame. Then when I instantiate the cPan object and try to place it next to the sPan object, the buttons aren't resizing themselves to accommodate the additional panel in the main JFrame. Does anybody know how I can fix this?
I have to use GridBagLayout for this assignment, so using normal gridLayout isn't an option.
Any tips regarding what's happening would be appreciated.
Could you simply deal with two columns? The first one taking 5/6 of the available width and the second one taking the 1/6th remaining?
You could try the following:
final JPanel sPan = new JPanel();
sPan.setBackground(Color.RED);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 5 / 6f; // change this value...
constraints.weighty = 1.00;
add(sPan, constraints);
final JPanel cPan = new JPanel();
cPan.setBackground(Color.BLUE);
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1 / 6f; // ... and this one.
constraints.weighty = 1.00;
add(cPan, constraints);
Please note: I replaced your JPanels by some empty JPanels with a background color, so even like this you can see what's going on.

GridBagLayout one cell alignment for label

I have for example 3x3 components in grid layout, and I would like all of them to be labels, but since I have some padding I want my labels to be in center of their cell. But I can't seem to manage it...
Part of the relevant code:
panel = new JPanel();
GridBagLayout gridBag = new GridBagLayout();
panel.setLayout(gridBag);
panel.setSize(new Dimension(30, 400));
GridBagConstraints c = new GridBagConstraints()
JLabel lab = new JLabel("proba");
lab.setBorder(outline);
c.fill = GridBagConstraints.BOTH;
c.gridx =2; c.gridy=2; c.ipady = 10; c.ipadx=10;
c.ipadx=100; panel.add(lab,c);
[update]
You really should post an SSCCE so we can all try it and not have to guess where the problem is. My last guess - and this is something you should do regardless of whether it fixes your current problem - is
label.setHorizontalAlignment(JLabel.CENTER);
If your label is centered and fills the entire grid cell, the text will still be left justified by default. The above change will cause the text to be centered within the label.
However if your label is not filling the cell, this won't help.
[original]
It's hard to say exactly what's wrong since this is not a full program, but here are a few comments that might get you on the right track.
First, you should be using setPreferredSize instead of setSize (see Java: Difference between the setPreferredSize() and setSize() methods in components)
When you make this change you will see that the panel is not quite what you want. It's very tall and thin. Perhaps a typo - did you mean (300,400) instead of (30,400)
Now I'm guessing all your labels will be clumped together. In order to get them to spread out you need to add:
c.weightx = .5;
c.weighty = .5;
(actually any non-zero value will work). This is described in http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
This should get you close(r) ...

Nested JPanel with GridBagLayout

I have a JFrame with GridBagLayout. weightx and weighty values are assigned different no-zero values and GridBagConstraints.fill = GridBagConstraints.BOTH. I nested a JPanel in one of the cells and gave it a GridBagLayout too. When adding components to the nested JPanel, the cell size where the nested JPanel resides grows on all sides missing up the parent's layout. Insets and padding are not used. How can I fix this problem?Thanks.
This is an example of the GridBagConstraints values:
GridBagConstraints treePanCon = new GridBagConstraints();
treePan.setLayout(treePanGridbag);
treePanCon.fill = GridBagConstraints.BOTH;
treePanCon.weightx = 0.5;
treePanCon.weighty = 1;
treePanCon.gridx = 0;
treePanCon.gridy = 0;
treePanCon.gridwidth = 1;
treePanCon.gridheight = 1;
This is a screenshot before adding components to the nested JPanel:
This a screenshot after adding components to the nested JPanel:
That's exactly what is supposed to happen. Please explain the behavior you're looking for. As an aside the layout managers available with J2SE are less than ideal. Having done a large amount of Swing work in a past life I would highly recommend checking out JGoodies forms: http://www.jgoodies.com/freeware/forms/. Check out the whitepaper, it's easy to use and much more intuitive than GridBag.
This is probably of absolutely no use to you at this late date, but...
It looks like you probably used BOTH when laying out the two new buttons.
If the preferredSize.width on the tree is the width you want for the inner panel,
and you wanted the two new buttons to each be half the width of the tree,
set their preferredSize.width to half the preferredSize.width of the tree, and use NONE on the two buttons.
Use VERTICAL on the tree in the inner panel.
Use VERTICAL on the inner panel in the outer panel.

Using Java and its SWING library

I am trying to code a very simple form using java but i am running into more trouble than i expected because of the way swing lays out components, I have tried various different layout managers and still cannot layout the form the way i would like.
Here is how i would like the form to look: link text
Does anyone have any ideas about how to go about this because im stumped?
Thanks.
The easiest for your purposes is the GridLayout. As Aaron said, Netbeans has a great GUI builder if you don't want to learn the layout managers.
Take a look at Table Layout. It is much easier to use than nearly all of the default layout managers. Your form would be very simple to construct with it.
Short of using gridbaglayout, it is doable by borderlayout, gridlayout and flowlayout. Start by breaking the form into big visual pieces and make sub layouts under top level ones.
For example, in this case I would do this:
Divide the form in two vertical pieces by gridlayout (or borderlayout); top (or central) part for the labels and textfields, bottom (or south) for the button.
In top part, make either flowlayout with center align or another gridlayout to arrange the labels and textfields.
In the bottom part, use flowlayout with center align for the button.
I have worked with Java swing for 4 years and it was a hurdle to design layouts at first, but it gets easier the more you practise.
GridBagLayout can be daunting at first, but it's very flexible, and is worth getting to know.
JButton button = ...;
JLabel[] labels = new JLabel[] {
new JLabel("Label 1"),
...
};
JTextField[] fields = new JTextField[] {
new JTextField(15),
...
};
JPanel[] rows = new JPanel[] {
new JPanel(),
...
}
Container container = getContentPane();
container.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
// Layout each row's contents(label + text field)
cons.fill = GridBagConstraints.BOTH;
cons.insets = new Insets(2, 8, 2, 8);
for (int i = 0; i < rows.length; ++i) {
rows[i].setLayout(new GridBagLayout());
// Labels get 30% of the row's extra horizontal space.
cons.weightx = 0.3;
rows[i].add(labels[i], cons);
// Text fields gets 70% of the row's extra horizontal space.
cons.weightx = 0.7;
rows[i].add(fields[i], cons);
// Add each row to the panel, top to bottom
// Each row takes up all of the horizontal space.
cons.gridx = 0;
cons.weightx = 1;
container.add(rows[i], cons);
cons.gridx = GridBagConstraints.RELATIVE;
}
// Add the button at the bottom.
// Dont let button get any extra horizontal space (so it won't stretch)
cons.gridx = 0;
cons.weightx = 0;
cons.fill = GridBagConstraints.NONE;
container.add(button, cons);
By far the easiest to use layout manager I've tried, at least for doing something similar to that, is DesignGridLayout. With that, the code would look something like:
layout.row().grid(new JLabel("JLabel1")).add(new JTextField());
layout.row().grid(new JLabel("JLabel2")).add(new JTextField());
...
layout.row().grid(new JLabel("JLabel5")).add(new JTextField());
layout.row().center().add(new JButton("JButton"));
A form like that should be very easy to design and code by hand. Just keep reading the documentation and playing around.
Another alternative is to use a GUI Builder, Netbeans has one that really impressed me when I took a look at it last year. I don't work in Java anymore so I can't provide you with much detail.
Netbeans GUI Builder (Formerly Matisse)
I would recommed you to use NetBeans.
It's much easier since it generates code with layout manager included, so you don't have to worry about it. It's good for beginners.
I'd do a main Panel with a BorderLayout.
The south area would hold a panel with the button in it.
The center area would hold a panel with a grid layout, grid bag layout or some kind of box layout that would lay your labels and text boxes out.
I really like the JGoodies Forms tool for laying out simple (and complex) forms. It's easy to work with and has great examples. If you want something really nice looking, you can use one of their look and feels too.

Categories

Resources