Question

I am using the MigLayout library for a Swing GUI.

I would like to overlay two JPanel, as so:

+----+-----------------------+
| 1  |         2             |
|    |                       |
+----+                       |
|                            |
|                            |
|                            |
|                            |
+----------------------------+

much like a minimap in a video game.


It seems to me there are two ways to achieve this:

JLayeredPane

JPanel main = new JPanel();
main.setBounds(0, 0, WIDTH, HEIGHT);
// ... fill it with Swing components ...
JPanel minimap = new JPanel();
minimap.setBounds(10, 10, 100, 100);
/// ... fill it with Swing components ...
JLayeredPane layer = new JLayeredPane();
layer.add(main, new Integer(0));
layer.add(minimap, new Integer(1));

This approach, however, is flimsy because I have to specify WIDTH and HEIGHT. If the window is resized, then it seems like I have to calculate these values again.

JLayeredPane & MigLayout

JPanel main = new JPanel();
// ... fill it with Swing components ...
JPanel minimap = new JPanel();
/// ... fill it with Swing components ...
JLayeredPane layer = new JLayeredPane();
layer.setLayoutManager(new MigLayout());

CC positioning;

positioning = // ??? something specified in percents and z-order?
layer.add(main, positioning, new Integer(0));

positioning = // ??? something specified in percents and z-order?
layer.add(minimap, positioning, new Integer(1));

Is it possible to fill in the positioning code with something that allows for overlays using MigLayout while letting me specify percent values (as opposed to the pixel resolution WIDTH and HEIGHT in the first example)?

EDIT:

Here is a solution that partially works:

positioning = new CC().width("100%").height("100%");
// ...
positioning = new CC().pos("10", "10");

However, the display jitters and sometimes is invisible. I believe this has to do with an indeterminate rendering order (although that's just intuition).

Was it helpful?

Solution

I've also tried setComponentZOrder, but that doesn't seem to have any effect

I don't know how MigLayout works behind the scenes but I would think ZOrder is important.

Check out the Overlap Layout which uses ZOrder for laying out components and has a little blurb on my understanding of how ZOrder works.

Maybe the isOptimizedDrawingEnabled() method, as mentioned in the article, is also important is this case,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top