Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I used <a href="http://download.oracle.com/javase/7/docs/api/javax/swing/JSplitPane.html#setResizeWeight%28double%29"><code>setResizeWeigth</code></a> to tell the <code>JSplitPanes</code> how to distribute size. As a bonus, the split panes keep the even distribution even when you resize the window. They will lose the even distribution once you resize one of the panes.</p> <p>Note that you do not necessarily need the <code>AncestorListener</code> anymore. I used it just to print out the sizes of the <code>JScrollPanes</code>.</p> <pre><code>import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTable; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; public class TestSplitPanels extends JPanel { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); final TestSplitPanels tps = new TestSplitPanels(); frame.setContentPane(tps); frame.pack(); frame.setVisible(true); } public TestSplitPanels() { JTable jt1 = new JTable(); JTable jt2 = new JTable(); JTable jt3 = new JTable(); final JScrollPane jsr1 = new JScrollPane(); final JScrollPane jsr2 = new JScrollPane(); final JScrollPane jsr3 = new JScrollPane(); jsr1.setViewportView(jt1); jsr2.setViewportView(jt2); jsr3.setViewportView(jt3); JSplitPane jsl1 = new JSplitPane(); JSplitPane jsl2 = new JSplitPane(); jsl1.setOrientation(JSplitPane.VERTICAL_SPLIT); jsl1.setTopComponent(jsr1); jsl1.setBottomComponent(jsl2); jsl1.setResizeWeight(0.33); // &lt;-- here jsl2.setOrientation(JSplitPane.VERTICAL_SPLIT); jsl2.setTopComponent(jsr2); jsl2.setBottomComponent(jsr3); jsl2.setResizeWeight(0.5); // &lt;-- here this.addAncestorListener(new BaseAncestorListener() { @Override public void ancestorMoved(AncestorEvent event) { System.out.println("jsr1 size: " + jsr1.getSize()); System.out.println("jsr2 size: " + jsr2.getSize()); System.out.println("jsr3 size: " + jsr3.getSize()); System.out.println("----"); } }); setLayout(new BorderLayout()); add(jsl1, BorderLayout.CENTER); } public static class BaseAncestorListener implements AncestorListener { @Override public void ancestorAdded(AncestorEvent event) { } @Override public void ancestorRemoved(AncestorEvent event) { } @Override public void ancestorMoved(AncestorEvent event) { } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload