0

I have a Canvas subclass object that I'm trying to add, along with some other Canvas subclasses, to a JLayeredPane. In the documentation for JLayeredPane, the layer is given as an Integer, e.g.

layeredPane.add(child, new Integer(0));

However, when I use an Integer for the layer, I get some kind of runtime error. (I can't really tell what this error is because my IDE keeps complaining about the lack source for the Swing libraries.) Oddly, when I use the following form:

layeredPane.add(child, 0);

the line actually executes error-free. I'm very new to Java and still haven't figured out how autoboxing works other than to form the opinion that it doesn't work very well. I'm not sure if the bare 0 would be autoboxed in this case.

I wish I could add more detail about this error, but I'm not even seeing an exception. I will work on trimming my code down to a simple example, but I was hoping someone with experience with JLayeredPane has seen this before.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jorocco
  • 83
  • 2
  • 9
  • 2
    Post a [SSCCE](http://sscce.org/) so we can see what you are doing – MadProgrammer Aug 22 '13 at 23:37
  • See [`Container.add(Component,int)`](http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component,%20int%29). That is where the method originates from, and it does not claim to accept an `Integer`, but an `int`. – Andrew Thompson Aug 22 '13 at 23:39

2 Answers2

0

Unfortunately I can't add comment so I will leave reply. If you work with IDE then you can use design form to create your GUI easily, just drag-&-drop thing....

I have worked with JLayeredPane and if you add component to it you can use add method manually like layeredPane.add(Component, javax.swing.JLayeredPane.PALETTE_LAYER); or layeredPane.add(Component, javax.swing.JLayeredPane.DEFAULT_LAYER);

Use static constant Integer field and read the JLayeredPane API, or some examples of using it on oracle. Its a good feature to learn.

H-Patel
  • 98
  • 5
  • I had tried using JLayeredPane.DEFAULT_LAYER before posting, but both JLayeredPane.DEFAULT_LAYER and JLayeredPane.PALETTE_LAYER produce the same unidentifiable error. I think it's probably best if I boil this down to a few-line example so I can post the entire problematic source. – Jorocco Aug 23 '13 at 01:06
  • This error had something to do with the fact that I was using BorderLayout on the JLayeredPane. The error went away when I changed to another layout. – Jorocco Aug 23 '13 at 02:58
  • Good that you itself figured out, because all other trouble shooting was not helpful. The layout manager is a tricky thing when working with Swing!! – H-Patel Aug 23 '13 at 04:36
0

This error had something to do with the fact that I was using BorderLayout on the JLayeredPane. The error went away when I changed to another layout.

Jorocco
  • 83
  • 2
  • 9