Results 1 to 3 of 3

Thread: Litte Help with SpringLayouts

  1. #1

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Litte Help with SpringLayouts

    Im a bit confused why this code only shows panel 2 and not panel 1.

    Code:
            container = frame.getContentPane();
            SpringLayout sl = new SpringLayout();
            container.add(pnl1);
            container.add(pnl2);
            
            sl.putConstraint(SpringLayout.WEST, pnl1, 5, 
                    SpringLayout.WEST, this);
            sl.putConstraint(SpringLayout.NORTH, pnl1, 5, 
                    SpringLayout.NORTH, this);
            
            sl.putConstraint(SpringLayout.WEST, pnl2, 5, 
                    SpringLayout.EAST, pnl1);
            sl.putConstraint(SpringLayout.NORTH, pnl2, 5, 
                        SpringLayout.SOUTH, pnl1);
    If i try just removing panel2 (pnl2), pnl1 will show, but when put together its only pnl2 that is showing.

    Ive also tried this for pnl2

    Code:
    layout.putConstraint (SpringLayout.WEST, pnl2,
               10, SpringLayout.WEST, this);
    layout.putConstraint (SpringLayout.NORTH, pnl2,
               5, SpringLayout.SOUTH, pnl1);
    but still no luck
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Litte Help with SpringLayouts

    I just made a couple of changes on your code to make sure it's working, and it worked
    Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SpringLayout;
    
    public class Util extends JFrame {
    	private static final long serialVersionUID = -1869532151600910911L;
    
    	public Util() {
    		JLabel label1 = new JLabel("Panel1");
    		JLabel label2 = new JLabel("Panel2");
    		JPanel pnl1 = new JPanel();
    		pnl1.setSize(100, 100);
    		pnl1.setBackground(Color.RED);
    		pnl1.add(label1, BorderLayout.CENTER);
    		JPanel pnl2 = new JPanel();
    		pnl2.setSize(100, 100);
    		pnl2.setBackground(Color.YELLOW);
    		pnl2.add(label2, BorderLayout.CENTER);
    		Container container = getContentPane();
    		SpringLayout sl = new SpringLayout();
    		container.add(pnl1);
    		container.add(pnl2);
    
    		sl.putConstraint(SpringLayout.WEST, pnl1, 5, SpringLayout.WEST, this);
    		sl.putConstraint(SpringLayout.NORTH, pnl1, 5, SpringLayout.NORTH, this);
    
    		sl.putConstraint(SpringLayout.WEST, pnl2, 5, SpringLayout.EAST, pnl1);
    		sl.putConstraint(SpringLayout.NORTH, pnl2, 5, SpringLayout.SOUTH, pnl1);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(300, 300);
    		setVisible(true);
    	}
    
    	public static void main(String[] args) {
    		new Util();
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member Greyskull's Avatar
    Join Date
    Dec 2003
    Location
    somewhere in England
    Posts
    382

    Re: Litte Help with SpringLayouts

    hanks for some reason my code still doesnt work

    its now one better than before as i can see both panels but it seems that one goes on top of the other.

    not sure why this is happening though....

    the code ive used is also attached.

    Thanks in advance
    Attached Images Attached Images  
    Attached Files Attached Files
    Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered
    If someone helped you today then please consider rating their post.

Posting Permissions

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



Click Here to Expand Forum to Full Width