I have the following,
GridLayout layout = new GridLayout(4, 0);
In the event that I have 5 items, this will create 2 columns, where the first contains 3 rows and the second contains 2 rows. This is not what I want, nor is this what I expected. I expected the first column to contain 4 rows and the second column to contain 1 row.
Why isn't this layout manager respecting the number of rows I want per column? Or better yet, how do I make this layout manager respect this?
The result seems expected: "Specifying the number of columns affects the layout only when the number of rows is set to zero."
You can get the desired effect using JList, as shown here.
private static final int N = 5;
...
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(N - 1);

Disclaimer: This is not provided as a concrete answer, but rather to prove a point of the helpfulness of a SSCCE...
GridLayout constructor is GridLayout(int rows,int cols) (The reason I mention it is as @AndrewThompson said in his answer seems like you might have mixed up the parameters of rows/cols for the LayoutManager). Thus 4,0 will give us 4 rows and a variable amount of columns.
When I add 4 labels I get 4 rows and 1 coloumn (as expected):

when you add 5 labels I get 3 rows and 2 coloumns each having 2 items except for the last which has 1:

IMO this is expected GridLayout must honor column/row count >0 (anything less than 0 and the LayoutManager calculates the amount), thus if we add more components than the rows allowed it creates a new coloumn >0 but also >1 as 0 and 1 perform the same. Thus it creates 2 coloums now when we fill a Grid thats 4x0 with 5 components, we expect the 2 components on each line ( for each coloums) and the remaining on the last row (not necessarily the last row depending on the amount of components i.e 8 would fill it to the last row as now its 4x2 but adding 9 components would cause a GridLayout of 4x3 - filling all coloumns of the row before going to the new row)
Please post an SSCCE which re-inacts the problem or else we are just guessing here is my example I made which shows different behavior than what you said/get:
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class Test {
public Test() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
GridLayout layout = new GridLayout(4, 0);
frame.setLayout(layout);
for (int i = 0; i < 5; i++) {
frame.add(new JLabel(String.valueOf(i + 1)));
}
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Create Swing components on EDT
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Test();
}
});
}
}
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