4

A followup question to my other one;

How do I enable proportional resizing? I thought this would work but it doesn't:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import net.miginfocom.swing.MigLayout;

public class SplitPaneQuestion {
    public static void main(String[] args) {
        JFrame frame = new JFrame("SplitPaneQuestion");
        JPanel panel = new JPanel();
        frame.setContentPane(panel);
        panel.setLayout(new MigLayout("","[]","[grow]"));
        JSplitPane splitpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        panel.add(splitpane, "push, grow");

        splitpane.setTopComponent(new JButton("top"));
        splitpane.setBottomComponent(new JButton("bottom"));
        splitpane.setDividerLocation(0.333);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

What I would like is for the ratio of the top/bottom button sizes to be constant when I resize the entire frame. (with a 1:2 ratio by default)

How can I do this?

Community
  • 1
  • 1
Jason S
  • 171,795
  • 155
  • 551
  • 900

2 Answers2

6
splitPane.setResizeWeight(0.333);
camickr
  • 308,339
  • 18
  • 152
  • 272
  • Aha! thanks, I was expecting setDividerLocation to work and when it didn't I had no idea what to do instead. – Jason S Jan 31 '11 at 17:31
2

I can't talk for MigLayout, which I do not know. However, to my mind, the best solution to that issue is swing-generic : you add a ComponentListener to your JSpiltPane and resize its contained component by a coefficient built from size ratio.

Riduidel
  • 21,048
  • 12
  • 78
  • 165