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);
}
}
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With