tags:

views:

250

answers:

6

Hi there. I have a JFrame with two JPanels inside. One is set on west, other on east with BorderLayout. The thing is, it just shows two 10 pixel width, 100% JFrame height strips: alt text

What i want to do is to setsize each panel having as end result that the jpanel on the west be 80% of the jframe width, the remaining 20% to the one on the east. Is it possible? Should I use another layout?

Thanks a lot.

A: 

I would suggest that you use FreeDesign layout, which will look like this :

alt text

Netbeans IDE makes all this job fun and easy ;)

Yan Cheng CHEOK
I wanna do it by hand, im just starting with Java and want to have a strong knowledge of what's going on in my program. Thanks!
Gabriel A. Zorrilla
+1  A: 

Have you tried a JSplitPane, specifically using the setDividerLocation(double proportionalLocation) method? This allows you to set a percentage of the enclosing component's width or height.

Ash
JSplitPane allows manual resizing in runtime, it's not what i want! Thanks!
Gabriel A. Zorrilla
See @camickr's answer regarding disabling the split pane to prevent resizing.
Ash
+1  A: 

I would recommend trying GridBagLayout, you can set the "weight" or priority of horizontal space per component.

You could also just go with a BoxLayout (along the X axis) and just add the pre-sized components to your container. The layout is a bit easier to work with.

Kavon Farvardin
+1  A: 

It depends on your exact requirement.

When using a split pane you can make it disabled which will prevent resizing.

You can also hide the divider using:

BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
ui.getDivider().setVisible(false);

However, this doesn't give you an exact 80/20 split. I created a splitpane of width 400. The right component started at 312 when is should be 320. So it depends what you want.

If you want a real 80/20 split then the Relative Layout was specifically written for this.

camickr
A: 

Three options here:

1 - Keep the border layout and set a prefered size on the two JPanels. This will work with fixed size window.

2 - Use the GridBagLayout:

public class TestJPanel extends JFrame {


    public TestJPanel() {

        JPanel rootPanel = new JPanel();
        rootPanel.setBackground( Color.WHITE );
        rootPanel.setPreferredSize( new Dimension( 0, 100 ) );

        rootPanel.setLayout( new GridBagLayout() );
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground( Color.BLUE );
        c.weightx = 0.2;
        c.gridx = 0;
        rootPanel.add( leftPanel, c );

        JPanel rightPanel = new JPanel();
        c.weightx = 0.8;
        c.gridx = 1;
        rightPanel.setBackground( Color.RED );
        rootPanel.add( rightPanel, c );

        this.add( rootPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {
        TestJPanel window = new TestJPanel();
        window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        window.setSize( 1024, 768 );
        window.setVisible( true );
    }

}

3 - Use the famous SpringLayout. This layout manager is really hard to master but here is a starter:

public class TestJPanel extends JFrame {


    public TestJPanel() {

        JPanel rootPanel = new JPanel();
        rootPanel.setBackground( Color.WHITE );
        rootPanel.setPreferredSize( new Dimension( 0, 100 ) );

        SpringLayout layout = new SpringLayout();
        rootPanel.setLayout( layout );

        SpringLayout.Constraints rootPaneCons = layout.getConstraints( rootPanel );
        rootPaneCons.setWidth( Spring.width( this ) );
        rootPaneCons.setHeight( Spring.height( this ) );

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground( Color.BLUE );
        rootPanel.add( leftPanel );
        SpringLayout.Constraints leftPaneCons = layout.getConstraints( leftPanel );
        leftPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .2f ) );
        leftPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );

        JPanel rightPanel = new JPanel();
        rightPanel.setBackground( Color.RED );
        rootPanel.add( rightPanel );
        SpringLayout.Constraints rightPaneCons = layout.getConstraints( rightPanel );
        rightPaneCons.setX( Spring.scale( rootPaneCons.getWidth(), .2f ) );
        rightPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .8f ) );
        rightPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );

        this.add( rootPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {
        TestJPanel window = new TestJPanel();
        window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        window.setSize( 1024, 768 );
        window.setVisible( true );
    }

}
Manuel Darveau
+1  A: 

I just used MIG Layout and presto.

Gabriel A. Zorrilla