Using setDividerSize() and setResizeWeight() in JSplitPane

The following example illustrates setting divider size and resize weight that allows you to position split pane at center or where ever you want. Here are prototypes of the methods..
// Set divider size
public void setDividerSize(int size)

// Throws IllegalArgumentException if weight doesn't conform to below rule
public void setResizeWeight(double weight) // 0<=weight<=1

import javax.swing.*;
import java.awt.*;
class JSplitPaneExample extends JFrame
{
JSplitPane js;
JPanel p1,p2;

    public JSplitPaneExample()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JSplitPane Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
       
        // Create panels
        p1=new JPanel();
        p1.setBackground(Color.GRAY);
       
        p2=new JPanel();
        p2.setBackground(Color.BLACK);
       
        // Add JSplitPane
        js=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,p1,p2);
       
        // Add divider size, weight
        js.setDividerSize(20);
        js.setResizeWeight(0.5);
       
        add(js);
       
        setSize(400,400);
        setVisible(true);   
    }
   
    public static void main(String args[])
    {
    SwingUtilities.invokeLater(new Runnable(){
            public void run()
            {
            new JSplitPaneExample();
            }
        });
    }
}

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

No comments: