How to set BackGround color to a divider in JSplitPane

Posted by Sunil Kumar Sahoo on Stack Overflow See other posts from Stack Overflow or by Sunil Kumar Sahoo
Published on 2010-03-15T15:57:22Z Indexed on 2010/03/15 15:59 UTC
Read the original article Hit count: 386

Filed under:
|

I have created a divider in JSplitPane. I am unable to set the color of divider. I want to set the color of divider. please help me how to set color of that divider

import javax.swing.; import java.awt.; import java.awt.event.*;

public class SplitPaneDemo { JFrame frame; JPanel left, right; JSplitPane pane; int lastDividerLocation = -1;

public static void main(String[] args) {
    SplitPaneDemo demo = new SplitPaneDemo();
    demo.makeFrame();
    demo.frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    demo.frame.show();
}


public JFrame makeFrame() {
    frame = new JFrame();
    // Create a horizontal split pane.
    pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
    left = new JPanel();
    left.setBackground(Color.red);
    pane.setLeftComponent(left);
    right = new JPanel();
    right.setBackground(Color.green);
    pane.setRightComponent(right);

    JButton showleft = new JButton("Left");
    showleft.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Container c = frame.getContentPane();
            if (pane.isShowing()) {
                lastDividerLocation = pane.getDividerLocation();
            }
            c.remove(pane);
            c.remove(left);
            c.remove(right);
            c.add(left, BorderLayout.CENTER);
            c.validate();
            c.repaint();
        }
    });
    JButton showright = new JButton("Right");
    showright.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Container c = frame.getContentPane();
            if (pane.isShowing()) {
                lastDividerLocation = pane.getDividerLocation();
            }
            c.remove(pane);
            c.remove(left);
            c.remove(right);
            c.add(right, BorderLayout.CENTER);
            c.validate();
            c.repaint();
        }
    });
    JButton showboth = new JButton("Both");
    showboth.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Container c = frame.getContentPane();
            c.remove(pane);
            c.remove(left);
            c.remove(right);
            pane.setLeftComponent(left);
            pane.setRightComponent(right);
            c.add(pane, BorderLayout.CENTER);
            if (lastDividerLocation >= 0) {
                pane.setDividerLocation(lastDividerLocation);
            }
            c.validate();
            c.repaint();
        }
    });

    JPanel buttons = new JPanel();
    buttons.setLayout(new GridBagLayout());
    buttons.add(showleft);
    buttons.add(showright);
    buttons.add(showboth);
    frame.getContentPane().add(buttons, BorderLayout.NORTH);

    pane.setPreferredSize(new Dimension(400, 300));
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.pack();
    pane.setDividerLocation(0.5);
    return frame;
}

}

Thanks Sunil kumar Sahoo

© Stack Overflow or respective owner

Related posts about java

Related posts about swing