Home arrow static arrow Java Programming [Archive] - Getting really irritated with JLayeredPane
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Getting really irritated with JLayeredPane
This topic has 1 reply on 1 page.

Posts:20
Registered: 9/22/02
Getting really irritated with JLayeredPane  
Jul 13, 2004 6:31 PM



 
Hi, ive been trying all morning to get things working with JLayeredPane. At first I was trying to do something fairly complicated - adding a JTextPane at the lowest level and a JPanel that you can draw on above it (it will eventually be used to "sketch" annotations for the text in the JTextPane. When I added them to the JLayeredPane they refused to show up. So I kept simplifying things to find out where the problem was, and I got to this:

import javax.swing.* ;import javax.swing.border.* ;import java.awt.* ; public class Blah2 extends JFrame {     public Blah2() {	JLayeredPane layers = new JLayeredPane() ;	layers.setPreferredSize(new Dimension(100, 100)) ;	layers.setBorder(new TitledBorder("Layers")) ;		for (int i = 0; i < 5; i++) {            JLabel label = new JLabel("Blah" + String.valueOf(i)) ;	    label.setOpaque(true) ;	    label.setBackground(Color.BLUE) ;	    label.setBorder(new LineBorder(Color.BLACK)) ;	    label.setLocation(new Point(i*10, i*10)) ;            layers.add(label, new Integer(i));        }		getContentPane().add(layers) ;	setSize(200,200) ;	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }     public static void main(String[] args) {		Blah2 blah = new Blah2() ;	blah.show() ;    }}


.. which I think you will agree is pretty bloody simple. Am I doing something really stupid?? Why wont anything show up inside the layered pane? I've read through the tutorial on this and I cant see why this wouldnt work.
 

Posts:9,091
Registered: 8/15/01
Re: Getting really irritated with JLayeredPane  
Jul 13, 2004 6:59 PM (reply 1 of 1)



 
The problem is the layout manager, or more of the lack of one.
import javax.swing.* ;import javax.swing.border.* ;import java.awt.* ; public class Blah2 extends JFrame {     public Blah2() {	JLayeredPane layers = new JLayeredPane() ;	layers.setPreferredSize(new Dimension(100, 100)) ;	layers.setBorder(new TitledBorder("Layers")) ;	layers.setLayout( null ); // Set the Layout manger to null (so you can use absulte) 	for (int i = 0; i < 5; i++) {            JLabel label = new JLabel("Blah" + String.valueOf(i)) ;	    	    label.setOpaque(true) ;	    label.setBackground(Color.BLUE) ;	    label.setBorder(new LineBorder(Color.BLACK)) ;	    Dimension d = label.getPreferredSize();	    label.setBounds( i*10, i*10, d.width, d.height );  // set the size based on its pref. size            layers.add(label, new Integer(i));        }		getContentPane().add(layers) ;	setSize(200,200) ;	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }     public static void main(String[] args) {		Blah2 blah = new Blah2() ;	blah.show() ;    }}
 
This topic has 1 reply on 1 page.