Suzan Cioc
Suzan Cioc

Reputation: 30107

How to setup last row or all rows except last not knowing number of rows in MigLayout?

The following code

            int numberofrows = 3;

            JFrame frame = new JFrame("Question 12844473");

            frame.setLayout(new MigLayout("fill, debug", "", "[][][150]"));

            for(int i=0; i<numberofrows-1; ++i) {
                frame.add(new JButton("Button #" +i), "wrap");
            }
            frame.add(new JButton("Exit"));

            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            frame.pack();
            frame.setVisible(true);

provides the following picture

enter image description here

meaning that third row was affected by setup because each square bracket pattern corresponds to each row.

But how to affect LAST row if number of rows vary? Or how to affect all rows? Or how to affect all rows except last one, while setup last row separately?

I mean not knowing number of rows.

Upvotes: 1

Views: 207

Answers (2)

LSerni
LSerni

Reputation: 57408

You mean that you are in this situation:

JFrame frame = new JFrame("Question 12844473");

frame.setLayout(new MigLayout("fill, debug", "", "[][][150]"));

while( 0 != (i = hasRow()))
{
    frame.add(new JButton("Button #" +i), "wrap");
}

...and now when hasRow() returns 0, it is too late, for you already added a frame on the previous iteration and you cannot un-add it and re-add some other way.

You can do it like this:

prev = hasRow();
while( 0 != (i = hasRow()))
{
    frame.add(new JButton("Button #" + prev), "wrap");
    prev = i;
}
// Now prev is the value of the last-but-one element returned
// and you can process it differently.
frame.add(new JButton("Button #" + prev));

You can adapt this pattern to any method whereby you are returned a row at a time, but do not know whether you will get an EOF instead.

I am not entering into the matter of whether such a frame adding can be done or not: this is just a generic off-by-one processing trick I cribbed off a HTML generator.

Upvotes: 1

Confusion
Confusion

Reputation: 16841

You can't generically set the row constraints such that they apply to any number of rows. However, you always know the number of rows, otherwise you can't render that number of rows either. Knowing the number of rows, you can programmatically generate the constraint string. E.g.

 String rowConstraint = ""
 rowConstraint += "[]"
 for(int i = 1; i < numberOfRows - 1; i++) {
    rowConstraint += "[150]"
 }     
 rowConstraint += "[]"

 frame.setLayout(new MigLayout("fill, debug", "", rowConstraint));

Upvotes: 3

Related Questions