In my Swing application I have a panel that uses a GridBagLayout. There, I have 3 columns and in one of these columns there is a JTextField. What I want is to set the width of this JTextField (using setPreferredSize() method) so that it occupies all the available space of its column.
As I understand the widths of the columns are calculated based on the weights that I've assigned to them using GridBagConstraints. I tried to subtract from the width of the panel the sum of widths of other components at the same line, but that doesn't seem to work, apparently because of different margins and insets.
So, how do I make my JTextField occupy all the available space?
Take a look at ...
GridBagConstraints.weightx
GridBagConstraints.fill
These will allow you to control how a component fills a given cell and the amount of "weight" a cell is given over the others
Related
After starting a JPanel using GridLayout(4,4) i insert a JLabel (and attach an imageicon to it) inside every grid cell with size of (150,150).
when i resize the JLabel to size (100,100) the image get cropped (which is perfectly fine by me), but i get a wierd looking grid (imaged added at the end).
if this helps: i dont actually resize the window, i just need to make sure the the size of the JLabel is set to (100,100) always, no metter what is the original image size.
before:
http://postimg.org/image/iolyeb8e7/
after:
http://postimg.org/image/5j6g87ein/
thanks
Unfortunately you did not say what you expect the grid to look like. I assume you don't want the cells to be so far apart from each other.
The GridLayout documentation states that...
The container is divided into equal-sized rectangles, and one component is placed in each rectangle.
If you shrink the size of each JLabel (i.e. the components in each of those rectangles) you just do that. You shrink the size of the component, not that of the rectangle. The grid does not care if the component is to small to fill the whole rectangle. At the moment you add the component to the grid1, the grid tries to set the components size to best fit the available space. But if you later change the labels size, the grid does not care.
What you probably want is to change the size of the whole grid: If you set the grids size to 400 by 400 it should evenly divide it to all 4 rows and 4 columns, so you get rectangles of size 100 by 100. All labels should automatically be sized accordingly.
1 Probably it is not exactly while adding the labels but while validating the container, but I don't know all the internal details about how and when layouts do there magic.
I want to edit the height of a single row in a JFrame with a GridLayout. Is there any way to do this or must the height of every row be constant?
Is there any way to do this..
Not with a single GridLayout.
..or must the height of every row be constant?
Yes, every row in a single GridLayout is the same height, and every cell is the same width.
Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height.
In my project,i need to adjust the sizes of columns in JTable as per the sizes of GUI Components such as JComboBox and JTextField. Can we adjust the size of column as per the size of GUI Component ?
but the size of column seems to be in pixels
What is the problem with that? The size of Swing components are also in pixels.
See the section from the Swing tutorial on Setting and Changing Column Widths for more information.
I am wanting to grow only the last component in a panel to fill the remaining vertical space.
I currently have:
panel.setLayout(new MigLayout("inset 0, filly", "[grow, fill, right][grow, fill, left]"));
Which adds padding after each component in order to fill the remaining vertical space. Is there a way to tell MigLayout not to add the padding and grow the last row?
The last row is docked incase that changes anything:
panel.add(new JScrollPane(getTable()), "newline, dock south");
Oh, one more thing I forgot to mention (and this is probably important) The amount of rows isn't know at compile time.
Looks like the dock indeed makes a difference, replacing it with pushy growing cell constraint (and no filly in the layout constraint) works fine. Might be an option if you can live without the dock:
panel.add(new JScrollPane(new JTable(8, 5)), "span, pushy, growy");
I have solved this by adding the component constraint of: height :100%: and removing the filly layout constraint.
My understanding is that this tells the component that it has no minimum size and the preferred size is 100% of it's container. This means that it will take up as much room as possible.
So when you do use the size constraint :pref: I believe it's saying "Up to the specified size`
And when you do min:: I think that resolves to "at least the specified size"?
FlowLayout performs "pressure" from the right, so as all components are trying to take their minimal widths. Contrary, BoxLayout tries to spread all components to fill entire height of the space.
Can I add some filler as last component or something to make all components have minimal heights in BoxLayout?
You could use Box.createGlue(), which returns a component that takes up as much space as the BoxLayout will give it. Adding it to the bottom of a vertical BoxLayout will scrunch the other components to the top.
You could also use nested layouts.