Java JSplitPane setSplitPaneDividerColor(final JSplitPane pane, final Color color)

Here you can find the source of setSplitPaneDividerColor(final JSplitPane pane, final Color color)

Description

Change the colour of a JSplitPane divider.

License

Open Source License

Parameter

Parameter Description
pane a parameter
color a parameter

Declaration


public static void setSplitPaneDividerColor(final JSplitPane pane, final Color color) 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.SplitPaneUI;
import javax.swing.plaf.basic.BasicSplitPaneDivider;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import java.awt.*;

public class Main {
    /***********************************************************************************************
     * Change the colour of a JSplitPane divider.
     *//from   w w w. j  a  v a2  s  .c o m
     * @param pane
     * @param color
     */

    public static void setSplitPaneDividerColor(final JSplitPane pane, final Color color) {
        final SplitPaneUI splitUI;

        splitUI = pane.getUI();

        // Obviously this will not work if the ui doen't extend BasicSplitPaneUI
        if (splitUI instanceof BasicSplitPaneUI) {
            //System.out.println("set split colour");
            final BasicSplitPaneDivider divider;

            divider = ((BasicSplitPaneUI) splitUI).getDivider();

            if (divider != null) {
                //System.out.println("got divider");
                final Border divBorder;
                final Border newBorder;
                final Border colorBorder;

                divBorder = divider.getBorder();

                class FilledBorder implements Border {
                    private Color color;
                    private final Insets NO_INSETS = new Insets(0, 0, 0, 0);
                    final Rectangle r = new Rectangle();

                    private FilledBorder(final Color bgcolor) {
                        this.color = bgcolor;
                    }

                    /***********************************************************************************
                     *
                     * @param c
                     * @param g
                     * @param x
                     * @param y
                     * @param width
                     * @param height
                     */

                    public void paintBorder(final Component c, final Graphics g, final int x, final int y,
                            final int width, final int height) {
                        //System.out.println("paint border in set divider colour");
                        g.setColor(this.color);
                        g.fillRect(x, y, width, height);

                        if (c instanceof Container) {
                            final Container cont;

                            cont = (Container) c;

                            for (int i = 0, n = cont.getComponentCount(); i < n; i++) {
                                final Component comp;
                                final Graphics tmpg;

                                comp = cont.getComponent(i);
                                comp.getBounds(r);
                                tmpg = g.create(r.x, r.y, r.width, r.height);
                                comp.paint(tmpg);
                                tmpg.dispose();
                            }
                        }
                    }

                    public Insets getBorderInsets(final Component c) {
                        return NO_INSETS;
                    }

                    public boolean isBorderOpaque() {
                        return true;
                    }
                }

                colorBorder = new FilledBorder(color);

                if (divBorder == null) {
                    //System.out.println("added to null border");
                    newBorder = colorBorder;
                } else {
                    //System.out.println("compound border");
                    newBorder = BorderFactory.createCompoundBorder(divBorder, colorBorder);
                }

                //System.out.println("set border on divider");
                divider.setBorder(newBorder);
            }
        }
    }
}

Related

  1. rememberOldHeightRatio (JSplitPane splitPane)
  2. sanitizeSplitPaneDivider(JSplitPane splitPane)
  3. setDividerLocation(final JSplitPane splitter, final int position)
  4. setNewHeightRatio (JSplitPane splitPane, JComponent comp, float oldFraction, int v1, int v2)
  5. setSplitDivider(final JSplitPane split, float proportion)
  6. standardJSplitPane(final JSplitPane splitPane)