Results 1 to 3 of 3
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Problems with size of JPanels inside JSplitPane

    SSCCE:

    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    import javax.swing.SwingUtilities;
    
    
    public class SplitPaneDemo {
    
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				Dimension dim = new Dimension(500, 300);
    				
    				JPanel bluePanel = new JPanel();
    				bluePanel.setBackground(Color.blue);
    				bluePanel.setMinimumSize(dim);
    				bluePanel.setPreferredSize(dim);
    				
    				JPanel yellowPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    				yellowPanel.setBackground(Color.yellow);
    				yellowPanel.add(bluePanel);
    				
    				JPanel redPanel = new JPanel();
    				redPanel.setBackground(Color.red);
    				redPanel.setMinimumSize(dim);
    				redPanel.setPreferredSize(dim);
    				
    				JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
    						yellowPanel, redPanel);
    				
    				JFrame frame = new JFrame("Split Pane Demo");
    				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				frame.add(splitPane);
    				frame.pack();
    				frame.setVisible(true);				
    			}
    		});
    	}
    }
    Notice how the top panel of the JSplitPane is actually two nested JPanels. When the window containing the JSplitPane is resized, the red panel grows to take up its share of the available space. But the blue panel remains fixed at its preferred size. It also remains fixed when the split divider is dragged. I want it to grow like the red panel.

    The problem seems to be that the yellow panel is allocating all the available space to itself instead of resizing its child.

    I think I can change this behavior by replacing the yellow panel's LayoutManager. But I don't know which one to use. I hope I don't have to write a custom one.

    Any suggestions?
    Get in the habit of using standard Java naming conventions!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Problems with size of JPanels inside JSplitPane

    The problem seems to be that the yellow panel is allocating all the available space to itself instead of resizing its child.
    FlowLayout shows a component at its preferredSize. If you want the bluePanel to always fill the yellowPanel you can use a BorderLayout and add it at CENTER. Or if you want to be fancy, a GridBagLayout with non-zero weightx/weighty. Or a GridLayout (1, 1) or (1, 0) or (0, 1) or (). Or...

    Have you gone through the Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing) ?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Problems with size of JPanels inside JSplitPane

    BorderLayout did the trick, thanks.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 03:50 PM
  2. How do a size a JSplitPane to the form?
    By Delphi_Programmer in forum AWT / Swing
    Replies: 1
    Last Post: 09-24-2010, 11:04 PM
  3. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 09:33 PM
  4. Switching JPanels inside JFrame attempt
    By frenk_castle in forum AWT / Swing
    Replies: 7
    Last Post: 03-31-2010, 09:39 AM
  5. Managing jPanels in jSplitPane
    By calexander in forum Advanced Java
    Replies: 6
    Last Post: 11-13-2008, 08:06 PM

Posting Permissions

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