GridBagLayout extends column - java

I have a JPanel with GridBagLayout set. It has 2 columns and 1 row (2 cells). Every cell contains one JPanel which contains one JLabel (type of component is insignificant).
The JLabel in the left cell has width attribute set to 100px. The JLabel in the right cell has width attribute set to 50px. In such case the left cell extends a little and it's wider than the right cell.
I thought that GridBagLayout expands cell only when contained components take too much space.
Is it a GBL bug?
Any idea how I can solve this problem?

Any idea how I can solve this problem?
Java Swing does not work the same way as CSS/HTML. You create the components in Swing and let the GUI worry about the sizing.
Without knowing what you're trying to accomplish, all I can do is say define the Swing components and let the GridBagLayout manage the layout.
If you want the grid areas to be the same size, you would use the GridLayout.

I am afraid this is a difficult question to answer because GridBagLayout doesn't arrange it's columns and rows this way. If you have two columns and two components the columns will size themselves as the sizes of the largest components put into those columns. So your left column is bigger, because your left component is bigger.
You can adjust how much of any spare space a cell takes by adjusting your component's corresponding GridBagConstraints attributes. The weight attributes (weighty & weightx) control how much of any spare space the columns (weightx) and rows (weighty) take up. If for example your JPanels were using GridBagConstraints objects called rightGbc and leftGbc you could use the following code to alter their relative sizes.
leftGbc.weightx = 0.5;
rightGbc.weightx = 1;
This means that the right column will take up twice as much spare space as the left column and therefore (hopefully) make up for the difference in the size of your components.
Using the weight attributes can feel like a very abstract process, and it can take a while to get used to them, but once you start using them for a while you will get it. Depending on which components you are using, there are other quirks which can effect how much affect the weight attributes have on the columns/rows.

Related

Java Layout for specific task

I need suggestion for layout type for following task. I have a panel on which user will be able to add or remove some components (label or another panel), which are all same size. There will be specific number of components at same column (like 4 components per column) but the number of components in rows will depend on user. The distance between components will be fixed, right, left up top. I will link you the image of what i need... Thanks.
link
I think you want a GridLayout, however I suggest using a third-party layout manager like MigLayout. For the case where you have empty cells, you can nest JComponents within each other with different layout managers ( see this SO question). MigLayout would be easier because it can simulate a GridLayout while respecting the preferred size you set on your JComponents, which allows you to have empty cells without the components stretching.

A Flexible Layout manager that will change the layout according to resize

In my JPanel, I have 6 buttons laid out in a row (using FlowLayout as of now). The default size of the panel is enough to accommodate these buttons in one row. But, when the frame is resized it gets stuck at the size that is the sum of the minimum sizes of each button.
I need a layout manager that simply puts the buttons in a new row on re-sizing of the panel.
I'm pretty new to Java Swing so I apologize in advance if this is a trivial question.
MigLayout is by far the best layout manager I've ever used. Things that used to require nested containers and lots of hard to understand code can be done in a single container with simple to understand (and maintain) string constraints.
The flow layout is capable of your desired behavior (moving components into new row if they cannot fit). Check out the swing tutorial (run FlowLayoutDemo). You'll have to show us your source code to find out, whether there is some other constrain which prevents it.
FlowLayout does actually paint components on a new row, but the problem is that the preferred size of the panel doesn't change so in many cases you can't see the components (unless you happen to add the panel to the CENTER of a BorderLayout).
One solution is to use the Wrap Layout, which extends FlowLayout to recalculate the preferred size of the panel so that you see the buttons on a new row.

Specify the number of grids in a GridBagLayout

GridLayout creates a grid matrix whose dimension (no of rows and columns) is customizable using two int parameters that is takes as part of its constructor.
Why don't we have such a similar option in GridBagLayout?
In a typical scenario using a Gridbaglayout, If i wanted a component to be placed at say extreme right or middle or at any other arbitrary location wouldn't it help if I know the size of Grid Matrix?.
Each 'row' of a GridBagLayout can have different numbers of columns, so it hardly makes sense to specify it in the constructor.
Because you specify the grid coordinates of a component through GridBagContraints when adding the component. The GridBagLayout’s size is then determined automatically by the largest coordinates (and probably the grid width and grid height of the component at these coordinates).

How to stick down border of JScrollPane with the Jframe or JPanel?

I mean when I resize the frame to the left or right side the table automatically resizes itself but I want the same future when I resize it from down or up sides of the table. There is scroll for down and up but I want default size to be smaller.
In fact aJScrollPane.setSize() doesn't work, aJPanel.setSize() too. Can you help me?
If you are trying to restrict the number of rows the table displays by default, you can use JTable.setPreferredScrollableViewportSize(). For example for 10 rows you can do the following:
table.setPreferredScrollableViewportSize(
new Dimension(table.getPreferredScrollableViewportSize().width,
10 * table.getRowHeight()));
Note however, table rows can have variable heights. Also note, that hard-coding sizes in general can have side effects.

Which layout should i use to achieve two colums 20% and 80%(Java)?

I'd like to achieve the following:
The first column 20% of the total width and the second the 80%. And it should be dynamic(ex. when i expand/shrink it should change accordingly like liquid layout in css)
MigLayout is your best friend: http://www.miglayout.com
The code would look like this:
JPanel panel = new JPanel(new MigLayout("wrap 2, fill", "[fill,20%][fill,80%]"));
panel.add(panel1);
panel.add(panel2);
For Swing you'd use a gridbag layout.
You have two columns, one row.
The gridwidth properties will be set so that the first column's is .2 and the second column is .8 or any set of numbers such that the first column's value is 1/4 the second column's value.
The gridheight for both columns should be the same.
You can experiment a bit with fill. If you don't mind space in your UI which is not filled with a component, then use none. If you want them to resize nicely but keep the .2 to .8 ratio then try horizontal and see if that keeps the proper ratio automatically.
If it doesn't then try setting weight to .2 for the first column and .8 for the second. Yo're trying to keep that.2 to .8 ratio no matter how big the JPanel is made by the user.
Let me know if you need more help.
If you are asking about desktop java application (Swing based), you can use GridBagLayout - a standard layout comes with JDK. Although it is somewhat hard to understand.
http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
The best solution would be making your own layout - it's not that hard at all.
GridBagLayout is more feared than understood.
I had worked on an application with most screens having a header footer and a left navigation panel. I used Gridbag layout then and only that gave the required behaviour during resize. Had used visualcafe.
Your two column requirement seems to be a good one to start using Gridbag since only a couple of gridbag constraints will be affected.
I suggest to use a tool to build the UI if you have more rows/colums.

Categories

Resources