javaswingjsplitpane

How can I change the style of the split bar in a JSplitPane?


I am using JSplitPanes for some of my panels, but they are a bit thick, especially when you have a few of them showing.

Is it possible to style these bars into something thinner, like a line with an arrow, or even just a line?


Solution

  • You can use setDividerSize to it in order to change its width. Its default value is 10. Full example:

    public class SplitPaneTest extends JFrame {
        public SplitPaneTest() {
            super("test");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLayout(new BorderLayout());
    
            JPanel left = new JPanel();
            JPanel right = new JPanel();
    
            JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
            System.out.println(sp.getDividerSize()); //Prints 10
            sp.setDividerSize(1);
    
            add(sp, BorderLayout.CENTER);
            pack();
            setLocationRelativeTo(null);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                new SplitPaneTest().setVisible(true);
            });
        }
    }
    

    However, Look & Feels are capable of changing the way it looks since they change its UI. If the above solution does not work for you, you will have to mess with its UI. For example, in one of my applications I did not want any line (while using Windows Look and Feel), so in order to make it invisible I had to:

    sp.setUI(new BasicSplitPaneUI() {
        @Override
        public BasicSplitPaneDivider createDefaultDivider() {
            return new BasicSplitPaneDivider(this) {
                private static final long serialVersionUID = -6000773723083732304L;
    
                @Override
                public void paint(Graphics g) {
                    //Divider gets no painting
                }
            };
        }
    });