Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i divide JPanel in 70% 30% [duplicate]

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

like image 219
madhur Avatar asked Sep 05 '12 07:09

madhur


2 Answers

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

like image 88
GrahamA Avatar answered Nov 06 '22 20:11

GrahamA


If you have an external JFrame (or another JPanel) f, you can apply a BorderLayout and put p1 in NORTH and p2 in SOUTH. Then, using

Dimension df = f.getSize();

You will get the Dimension of the external Container (f). Next, call to:

void setSize(Dimension d)

this way:

p1.setSize(new Dimension(df.getWidth(), df.getHeight()*0.7));
p2.setSize(new Dimension(df.getWidth(), df.getHeight()*0.3));

Finally, add these JPanels to its container.

like image 44
arutaku Avatar answered Nov 06 '22 21:11

arutaku