Is there a way to insert a “cell” in MigLayout?

I am trying to create a panel that can dynamically insert event-based components. The panel is based on a row with a variable number of components per row. However, I have problems inserting components between existing ones.

For example, if I have the following layout (rows represent MigLayout cells):

+----+----+----+ | X1 | X2 | X3 | +----+----+----+----+ | Y1 | Y2 | Y3 | Y4 | +----+----+----+----+ 

Is it possible to create a cell between rows X and Y to get:

 +----+----+----+ | X1 | X2 | X3 | +----+----+----+ | Z1 | +----+----+----+----+ | Y1 | Y2 | Y3 | Y4 | +----+----+----+----+ 

I tried content.add(component, "cell 1 0, wrap"); but it inserts the component into cell Y1 .

The only solution I have so far is to call content.add(component, "wrap", index); . However, this requires that I know the total number of previous components.

+4
source share
5 answers

Instead of using the concept of "cell", you can use the "absolute" positioning for your cells in migLayout and glue the cells together, referring to the coordinates of neighboring cells. You need to specify the names in your cells in order to refer to them in migLayout.

Then you can reconfigure the cells at any time by changing the name references in the coordinates of the cells, you just need to call setComponentConstraints (...) for the affected cells.

fi you for insertige Z cell, you

See the miglayout demo application. some code might look like this:

 myPanel.add(createCell("X1"), "pos 0 0 100 100"); myPanel.add(createCell("X2"), "pos X1.x2 0 200 X1.y2"); 

etc.

When you insert cell "Z",

 myPanel.add(createCell("Z"), "pos 0 X1.z2 200 100"); 

you only need to change the y link of Y1 from X1.y2 to Z.y2.

I have done it. similar to creating a kind of multi-splitpane, and it seems to work well.

+1
source

"hidemode" holds you back:

 new MigLayout("hidemode 3","","[][][]") 
+2
source

There is no direct way to insert a new component, however, it is quite simple to remove and re-add all existing components without pasta copies. An important role in this is that wherever the original component builder was not available, you do not need to have all the code layout in more than one place.

  Map<Component,Object> constraintMap = migLayout.getConstraintMap(); Component[] allComps = jPanel.getComponents(); jPanel.removeAll(); for (Component c : allComps) { if ( condition_to_insert ) { jPanel.add(insertComponent, new CC()); } jPanel.add(c, constraintMap.get(c)); } migLayout.invalidateLayout(jPanel); 
+1
source

I'm not sure if this is the best solution, but you can again lay the entire container, including your new component. I don’t think there is a way to insert components the way you want.

I hope I'm wrong.

but for this there should not be much unnecessary overhead, and the user would quickly notice this!

0
source

Swing panels and components are not designed for dynamic restructuring at all, so I think the psanton solution is probably the safest and easiest way. You can reuse a panel by calling its removeAll() before re-adding items using existing code.

If your own suggestion for using the index also works, then I would go for it: tracking the previous lines in your model should be very easy to implement, and restructuring your components will be a little faster, although it is unlikely that you will feel the difference if you do not have hundreds of subelements.

0
source

All Articles