How to set BackGround color to a divider in JSplit

2019-06-05 04:47发布

问题:

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

回答1:

Or, since the divider is a container, you can do the following:

dividerContainer = (BasicSplitPaneDivider) splitPane.getComponent(2);
Component leftBtn = dividerContainer.getComponent(0);
Component rightBtn = dividerContainer.getComponent(1);
dividerContainer.setBackground(Color.white);
dividerContainer.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 4));
dividerContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
dividerContainer.add(toolbar);
dividerContainer.setDividerSize(toolbar.getPreferredSize().height); 


回答2:

This code works for me:

splitPane.setUI(new BasicSplitPaneUI() {
        public BasicSplitPaneDivider createDefaultDivider() {
        return new BasicSplitPaneDivider(this) {
            public void setBorder(Border b) {
            }

            @Override
                public void paint(Graphics g) {
                g.setColor(Color.GREEN);
                g.fillRect(0, 0, getSize().width, getSize().height);
                    super.paint(g);
                }
        };
        }
    });
    splitPane.setBorder(null);

You can change divider color "g.setColor(new Color(R,G,B))".



回答3:

This worked for me fine.First you are creating JFrame with it's normal methods such as setDefaultCloseOperation(), setBounds(), getContentPane(). Then create an object from your class then use that to call all the other methods through out the program, in this case I created object called app. One thing you have to keep in mind is that don't forget to use ActionListener e.

Also color changes must go with setBackground() function, while you getting the values from getSource() for the color change.

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

public class Main implements ActionListener {

  private static void createAndShowGUI() {
    Main app=new Main();
    // make frame..
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("I am a JFrame");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(20,30,300,120);
    frame.setLayout(null);

    //Create a split pane
    JSplitPane myPane = new JSplitPane();
    myPane.setOpaque(true);
    myPane.setDividerLocation(150);

    app.right = new JPanel();
    app.right.setBackground(new Color(255,0,0));
    app.left = new JPanel();
    app.left.setBackground(new Color(0,255,0));
    app.left.setLayout(null);
    myPane.setRightComponent(app.right);
    myPane.setLeftComponent(app.left);
    // make buttons
    app.butt1=new JButton("Red");
    app.butt2=new JButton("Blue");
    app.butt3=new JButton("Green");
    // add and size buttons
    app.left.add(app.butt1);
    app.butt1.setBounds(10,10, 100,20);
    app.left.add(app.butt2);
    app.butt2.setBounds(10,30, 100,20);
    app.left.add(app.butt3);
    app.butt3.setBounds(10,50, 100,20);
    // set up listener
    app.butt1.addActionListener(app);
    app.butt2.addActionListener(app);
    app.butt3.addActionListener(app);
    frame.setContentPane(myPane);
    frame.setVisible(true);              
  }

  public void actionPerformed(ActionEvent e)
  {
    // check which button and act accordingly
    if (e.getSource()==butt1)
      right.setBackground(new Color(255,0,0));
    if (e.getSource()==butt2)
      right.setBackground(new Color(0,0,255)); 
    if (e.getSource()==butt3)
      right.setBackground(new Color(0,255,0));
  }


  public static void main(String[] args) {
    // start off..
    try  {        
      UIManager.setLookAndFeel(   
      "javax.swing.plaf.metal.MetalLookAndFeel"   );   
    } 
    catch (Exception e) 
    {
      System.out.println("Cant get laf"); 
    }
    Object a[]= UIManager.getInstalledLookAndFeels();
    for (int i=0; i<a.length; i++)
      System.out.println(a[i]); 


    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        createAndShowGUI();
      }
    });
  }

  // application object fields    
  int clickCount=0;  

  JLabel label;
  JButton butt1;
  JButton butt2;
  JButton butt3;
  JPanel left;
  JPanel right;
}