1

I currently have a JApplet within which I add two objects that both extend JComponet. Object A is basically a large square and object B is a small square, I need Object B to always be in front of Object A, however I cant work out how to set layering within the JApplet to do this. Current I am using the follow code, which adds both the items and displays them how I want however sometimes Object A is front of Object B.

public void init() {
    add(myapplet, BorderLayout.CENTER);
    resize(200, 400);
    B = new Block(Color.green, 10, 10);
    myapplet.add(B);
    A = new Block(Color.red, 100, 100);
    myapplet.add(A);
    myapplet.addMouseListener(this);
    startTimer();
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Zac Powell
  • 103
  • 10

2 Answers2

2

You might look at JLayeredPane, seen here, or OverlayLayout, seen here. Either should work in an applet, but this hybrid approach may offer additional flexibility.

JLayeredPane

OverlayLayout

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Giving a JLayeredPane a try, the applet will run but does not display anything, my init now looks like: http://pastebin.com/Bct9dVsb – Zac Powell Dec 09 '12 at 19:19
  • Too much missing; please edit your question to include an [sscce](http://sscce.org/) that shows your current approach. – trashgod Dec 09 '12 at 22:31
0

You may want to look at this method: Container.setComponentZOrder(Component comp, int index)

Nemanja
  • 1,161
  • 11
  • 13
  • I added `myapplet.setComponentZOrder(B, 0); myapplet.setComponentZOrder(A, 1);` however I get `java.lang.IllegalArgumentException: component and container should be in the same top-level window` – Zac Powell Dec 07 '12 at 14:51