Search code examples
javaswingjpanellayout-managerjapplet

how do i divide JPanel in 70% 30%


Possible Duplicate:
Swing: How do I set a component height to the container's height?

how do i divide JPanel like the picture shown below there are 2 panels panel1 and panel2 panel1 should take 70% and panel2 30% or panel1 should be bigger than panel2... I have tried Gridlayout, Border Layout but its not working.any help would be appreciated.

public class TestApplication extends JApplet {

private static final long serialVersionUID = 1L;

    JPanel p1,p2;

    public void init(){         
        setLayout(new GridLayout(3,1));
        p1=new JPanel();
        p2=new JPanel();

        p1.setBackground(Color.RED);
        p2.setBackground(Color.GREEN);

        add(p1);
        add(p2);
    }   
}

enter image description here


Solution

  • Have you considered using a JSplitPane (How to Use Split Panes)?

    import java.awt.BorderLayout;
    import java.awt.Color;
    
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import javax.swing.JSplitPane;
    
    public class TestApplication extends JApplet {
    
    private static final long serialVersionUID = 1L;
    
        JPanel p1,p2;
    
        @Override
        public void init(){         
            setLayout(new BorderLayout());
    
            p1=new JPanel();
            p2=new JPanel();
    
            p1.setBackground(Color.RED);
            p2.setBackground(Color.GREEN);
            JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
            sp.setResizeWeight(0.7);
            sp.setEnabled(false);
            sp.setDividerSize(0);
    
            sp.add(p1);
            sp.add(p2);
            add(sp, BorderLayout.CENTER);
        }   
    }
    

    enter image description here