Results 1 to 2 of 2
  1. #1
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Rep Power

    Default JLayeredPane.remove() removes the wrong layer. What am I doing wrong?

    Hello. I'm trying to use a JlayeredPane in a small application and it isn't going well at all. So to try and find out about why it isn't working and how to make it work I made a very simple demo application that demonstrates my problem.

    All the application does is make a 400x400 pixel JlayeredPane appear and then adds 8 NumberPanel objects to it, each on its own layer. The NumBerPanel object is simply a non-opaque JPanel that draws its number that it represents at the co-ordinate n + 1 x 30, n + 1 x30. Essentially drawing numbers 0 - 7 in a diagonal line from the top corner (before some layers are removed anyway). The problem is that any time I try to remove a layer, the wrong one is always removed. It is always the opposite layer that gets removed. So when the loop that removes layers begins and pane.remove(r); is called while r equals 7, it actually removes layer 0. When r equals 6 then layer 1 is removed and so on. Here's the code I have right now which includes a call to System.out.println(); in each loop that adds and removes the panels, so I could be sure no re-indexing of NumberPanels in layers was going on, and a call to JLayeredPane.setLayer() on line 24 which I noticed the documentationat Java Platform SE 7 said
    Quote Originally Posted by
    The layer should be set before adding the child to the parent.
    But doesen't seem to make any difference at all in this example.

    Java Code:
    package layeredpain;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JFrame;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    public class LayeredPain extends JFrame {
        private final Dimension paneSize = new Dimension(400, 400);
        public LayeredPain() {
            setTitle("Layered Pain");
            JLayeredPane pane = new JLayeredPane();
            getContentPane().add(pane, BorderLayout.CENTER);
            for(int a = 0; a < 8; a++) {
                NumberPanel numPan = new NumberPanel(a, paneSize);
                pane.setLayer(numPan, a);
                pane.add(numPan, new Integer(a));
            NumberPanel panel = (NumberPanel) pane.getComponentsInLayer(0)[0];
            System.out.println("Panel #" + panel.getNumber());
            for(int r = 7; r > 3; r--) {
            NumberPanel panel = (NumberPanel) pane.getComponentsInLayer(7)[0];
            System.out.println("Panel #" + panel.getNumber());
        private class NumberPanel extends JPanel {
            private final int n;
            public NumberPanel(int i, Dimension s) {
            n = i;    
            public int getNumber() {
                return n;
            protected void paintComponent(Graphics g) {
            Graphics2D g2 = (Graphics2D) g;
                int cord = n + 1;
                g2.drawString(new Integer(n).toString(), 
                        cord * 30, 
                        cord * 30);
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new LayeredPain().setVisible(true);
    Can anyone show me what I'm doing wrong?

    P.S. I realize that if the layers I am trying to remove were actually removed then the application would not run because line 31 would cause an ArrayIndexOutOfBoundsException to be thrown if the layer at index 7 was actually removed when "pane.remove(7); "is called. I simply left it that way fror the purposes of demonstration and to allow anyone else to be able to compile and run the code.
    Last edited by Mr Wogan; 01-07-2013 at 05:19 PM.

  2. #2
    Mr Wogan is offline Member
    Join Date
    Dec 2012
    Rep Power

    Default Re: I think I misunderstand the documentation for JLayeredPane

    Here's a snapshot of the example when run. As you can see from the source remove() is called with the ints 7, 6, 5 and 4 , but as you can see from the snapshot those layers are unchanged and instead layers 3, 2, 1. and 0 are removed. Can anyone show me what I am doing wrong?

    I think I misunderstand the documentation for JLayeredPane-layeredpain.png
    Last edited by Mr Wogan; 01-08-2013 at 12:27 PM.

Similar Threads

  1. JLayeredPane isnt displaying
    By rasen58 in forum Java Applets
    Replies: 1
    Last Post: 11-01-2012, 05:20 PM
  2. JLayeredPane issues?!
    By seniorstatus in forum AWT / Swing
    Replies: 21
    Last Post: 04-29-2012, 01:47 AM
  3. JLayeredPane - JApplet trouble
    By Dustin T in forum New To Java
    Replies: 4
    Last Post: 09-20-2011, 04:29 AM
  4. JLayeredPane not displaying components
    By abc in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2011, 04:40 PM
  5. How to add JScrolPane to a JLayeredPane?
    By chyrl in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2010, 04:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts