Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JSplitPane add a border to my components, and how do I stop it?

JSplitPane seems to add a border to any Component added to it.

This is most visible with nested JSplitPanes - e.g.:

public class JSplitPaneToy {

  public static void main(String[] args) {
    JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
      makePanel(), makePanel());
    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
    sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);

    JFrame frame = new JFrame("JSplitPane Toy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(sp);
    frame.pack();
    frame.setVisible(true);
  }

  private static JScrollPane makePanel() {
    JScrollPane pane = new JScrollPane(new JTable(
      new Object[][]{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}}, new Object[]{1, 2, 3}));
    pane.setPreferredSize(new Dimension(200, 100));
    return pane;
  }
}

i.e. each subsequent nested component appears to be set further back - i.e. there is some form of shadow border being added.

  • Why is this border being added? (Is this actually a border being added...?)
  • How can I prevent this 'border' from being added?
like image 408
amaidment Avatar asked Oct 09 '12 12:10

amaidment


People also ask

What is JSplitPane in Java?

JSplitPane is used to divide two (and only two) Component s. The two Component s are graphically divided based on the look and feel implementation, and the two Component s can then be interactively resized by the user. Information on using JSplitPane is in How to Use Split Panes in The Java Tutorial.

What is a split pane?

A split pane is useful when creating multi-view layouts. It allows UI elements, like menus, to be displayed as the viewport width increases. If the device's screen width is below a certain size, the split pane will collapse and the menu will be hidden.


2 Answers

If you want to drop those borders on all JSplitPane, you can change the defaults of the UI like this. However, I usually try not to mess with UI-defaults.

import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JSplitPaneToy {

    public static void main(String[] args) {
        UIManager.getDefaults().put("SplitPane.border", BorderFactory.createEmptyBorder());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JSplitPaneToy().initUI();
            }
        });
    }

    public void initUI() {
        JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), makePanel());
        sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
        sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);
        sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp);

        JFrame frame = new JFrame("JSplitPane Toy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(sp);
        frame.pack();
        frame.setVisible(true);
    }

    private JScrollPane makePanel() {
        JScrollPane pane = new JScrollPane(new JTable(new Object[][] { { 0, 1, 2 }, { 1, 2, 3 }, { 2, 3, 4 } }, new Object[] { 1, 2, 3 }));
        pane.setPreferredSize(new Dimension(200, 100));
        return pane;
    }
}

You may want to have a look at the JXMultiSplitPane of the SwingX project, instead of nesting so many splitpanes.

like image 119
Guillaume Polet Avatar answered Oct 02 '22 01:10

Guillaume Polet


We use this method to "flatten" a JSplitPane. Perhaps this is what you are looking for:

/**
 * Makes a split pane invisible. Only contained components are shown.
 *
 * @param splitPane
 */
public static void flattenJSplitPane(JSplitPane splitPane) {
    splitPane.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    BasicSplitPaneUI flatDividerSplitPaneUI = new BasicSplitPaneUI() {
        @Override
        public BasicSplitPaneDivider createDefaultDivider() {
            return new BasicSplitPaneDivider(this) {
                @Override
                public void setBorder(Border b) {
                }
            };
        }
    };
    splitPane.setUI(flatDividerSplitPaneUI);
    splitPane.setBorder(null);
}

As for why the borders get added, would not know. Apparently its some sort of a feature. We found it to be an unwanted one and the above method works around it. There's probably a simpler way of dealing with this problem, but hey, when you find somethig that works, you stop looking for alternatives.

like image 29
predi Avatar answered Oct 01 '22 23:10

predi