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 http://docs.oracle.com/javase/7/docs/api/index.html?javax/swing/JLayeredPane.html
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.
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() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Layered Pain");
JLayeredPane pane = new JLayeredPane();
getContentPane().add(pane, BorderLayout.CENTER);
pane.setPreferredSize(paneSize);
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--) {
pane.remove(r);
NumberPanel panel = (NumberPanel) pane.getComponentsInLayer(7)[0];
System.out.println("Panel #" + panel.getNumber());
}
pack();
setLocationRelativeTo(null);
}
private class NumberPanel extends JPanel {
private final int n;
public NumberPanel(int i, Dimension s) {
super();
setOpaque(false);
setSize(s);
n = i;
}
public int getNumber() {
return n;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
int cord = n + 1;
g2.drawString(new Integer(n).toString(),
cord * 30,
cord * 30);
g2.dispose();
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
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.
1 Attachment(s)
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?