I'm using a GridLayout and my code is as follows:
int changingVar = 1; JPanel panel = new JPanel(new GridLayout(changingVar, 2)); panel.add(new JButton("BUTTON1")); panel.add(new JButton("BUTTON2"));
This looks like:
___________________________________________ | [ BUTTON1 ] [ BUTTON2 ] | ___________________________________________
which is two evenly sized columns. I would like to make it like this:
___________________________________________ | [ BUTTON1 ] [ BUTTON2 ] | ___________________________________________
in which one column takes up more of the panel space then the other. How do I do this with gridlayout? I am not oppose to using another layout as long as I can have a varying amount of rows and columns that are two different sizes.
Thanks
While you can't make components span multiple rows with GridLayout, you can resize them all uniformly. Put your GridLayout on its own JPanel, and then you can use panel. setSize(x,y) to change the panel size and thus increase or decrease the size of the cells.
A GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.
To add Components to a GridLayout You do not (can not) use the row and column to tell where to add the components -- add them in starting at the top left and going across the row first.
Class constructors S.N. Creates a grid layout with a default of one column per component, in a single row. Creates a grid layout with the specified number of rows and columns.
If you want this effect then you need to utilize the GridBagLayout.
http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
Have fun with that one =P
EDIT:
You can work around the problem by employing a mixture of FlowLayout and GridLayout to get a similar effect. However, this solution will become extremely tedious and messy as your layout complexities become bigger.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With