4

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?

mre
  • 43,520
  • 33
  • 120
  • 170

3 Answers3

6

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);

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I like this approach, but it's a shame you can't configure the orientation with this layout manager... :( – mre Jan 31 '13 at 01:25
  • 1
    Yes; instead of a layout manager, `ListUI` uses a `CellRendererPane` for flyweight rendering. See also this [Q&A](http://stackoverflow.com/q/3546115/230513) suggesting MigLayout. – trashgod Jan 31 '13 at 01:53
  • this actually won't work so nice for me since the components are `JRadioButton`s and that will force me to implement a custom renderer, and etc. I think I'm going to nest panels with `FlowLayout` and `BoxLayout`...I'll report back. – mre Jan 31 '13 at 03:27
  • Ah, you'll need an editor, which is not supported by `JList`. You _might_ be able to use a `JToggleButton`, for [example](http://stackoverflow.com/a/14478432/230513) and [example](http://stackoverflow.com/a/7137801/230513). – trashgod Jan 31 '13 at 03:37
4

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):

enter image description here

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

enter image description here

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();
            }
        });
    }
}
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
2

The GridLayout you specified has number of columns as 0. That means number of columns is to be decided by the layout. In which case it tries to compute number of columns required based on number of items.

If you added 4 components you would have got 4 rows with one component each. If you add 5 components two columns are required and they are filled with the components serially that means only 3 rows will be consumed.

So depending on your number of components you may see all specified rows utilized or not. For instance try with 7 components.

Ashwinee K Jha
  • 9,187
  • 2
  • 25
  • 19