Results 1 to 4 of 4
  1. #1
    abc
    abc is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default JLayeredPane not displaying components

    Hi all,

    I am having a problem with the JLayeredPane component. It does not display the JSplitPane that's added to it. Please help!

    If you run the code below with line 58 the code works fine, but with line 57 uncommented it does not! Anyone can spot a problem here?

    Thx

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JLayeredPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    
    public class Main {
    	public static void main(String[] args) {
    		K k = new K();
    		k.create();
    	}
    }
    
    class K {
    	private final static JFrame f = new JFrame("");
    
    	public void create() {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    	private void createAndShowGUI() {
    		JPanel container = new JPanel();
    		final ContentClass content = new ContentClass();
    		content.setOpaque(false);
    		container.add(content, BorderLayout.CENTER);
    		f.getContentPane().add(container, BorderLayout.CENTER);
    		f.pack();
    		f.setVisible(true);
    	}
    }
    
    class ContentClass extends JPanel {
    	private final JSplitPane splitPane;
    	private final JLayeredPane container = new JLayeredPane();
    	private static AbstractWindow rw,lw = null;
    
    	public ContentClass() {
    		super(new BorderLayout());
    		rw = new AbstractWindow();
    		lw = new AbstractWindow();
    		splitPane = new JSplitPane(0x1,rw, lw);
    		splitPane.setResizeWeight(0.5);
    		splitPane.setOneTouchExpandable(true);
    		splitPane.setContinuousLayout(true);
    		
    		container.setPreferredSize(new Dimension(600,200));
    		splitPane.setPreferredSize(new Dimension(600,200));
    		
    		container.add(splitPane, new Integer(0), -1);
    		add(container, BorderLayout.CENTER);   // THIS DOES NOT WORK
    //		add(splitPane, BorderLayout.CENTER);   // THIS DOES WORK
    	}
    }
    
    class AbstractWindow extends JPanel {
    	private final JScrollPane scrollPane;
    	JLabel table = new JLabel("TEXT TEXT TEXT TEXT ");
    	AbstractWindow() {
    		setPreferredSize(new Dimension(300,200));
    		scrollPane = new JScrollPane(table);
    		table.setPreferredSize(new Dimension(300,200));
    		scrollPane.setPreferredSize(new Dimension(300,200));
    		add(scrollPane, BorderLayout.CENTER);
    	}
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    JLayeredPanes don't have layout managers by default, so you have to set the bounds of anything added to them.

    Replacing your splitPane.setPreferredSize with this line fixes your problem:
    Java Code:
    splitPane.setBounds(0, 0, 600, 200);
    But I would definitely recommend reading the JLayeredPane tutorial, which goes into more detail about why this is the case, and other ways around it.

    How to Use Layered Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    abc
    abc is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    Thanks KevinWorkman - Exactly what I was looking for!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default

    Quote Originally Posted by abc View Post
    Thanks KevinWorkman - Exactly what I was looking for!
    No problemo. Glad you got it sorted out. And thank you for posting a real question and real runnable code. Not something you see very often.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to add JScrolPane to a JLayeredPane?
    By chyrl in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2010, 04:28 AM
  2. Getting and Displaying Swing Components From DataBase
    By vivekkumar in forum AWT / Swing
    Replies: 1
    Last Post: 07-27-2010, 03:32 PM
  3. placing the components
    By rajeevhegde in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2010, 03:12 PM
  4. Displaying Chinese characters on Swing components
    By vaskarbasak in forum AWT / Swing
    Replies: 3
    Last Post: 06-26-2008, 08:27 AM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 04:04 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
  •