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

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

This is most noticeable with nested JSplitPanes - for example:

 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 embedded component is apparently installed even further - i.e. some form of shadow is added.

  • Why is this border added? (Is this adding a border ...?)
  • How can I prevent this border from being added?
+4
source share
5 answers

If you want to remove these borders on all JSplitPane, you can change the default settings for the user interface. However, I usually try not to get confused with the user interface settings.

 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 might want to take a look at the SwingX project's JXMultiSplitPane, instead of embedding so many splitpanes.

+6
source

We use this method to โ€œsmoothโ€ 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 borders are added, I would not know. Apparently, this is a kind of function. We found this to be undesirable, and the method described above works around it. There is probably an easier way to deal with this problem, but hey, when you find that it works, you stop looking for alternatives.

+3
source

I used this to reset the border to form a delimiter.

  SplitPaneUI ui = sp.getUI(); if( ui instanceof BasicSplitPaneUI ) { ((BasicSplitPaneUI)ui).getDivider().setBorder( null ); } 

NOTE: your example does not work splitPane unknown, it must be sp .

 public class JSplitPaneToy { public static void main(String[] args) { JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), makePanel()); SplitPaneUI ui = sp.getUI(); if( ui instanceof BasicSplitPaneUI ) { ((BasicSplitPaneUI)ui).getDivider().setBorder( null ); } sp.setBorder( BorderFactory.createEmptyBorder() ); sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp); ui = sp.getUI(); if( ui instanceof BasicSplitPaneUI ) { ((BasicSplitPaneUI)ui).getDivider().setBorder( null ); } sp.setBorder( BorderFactory.createEmptyBorder() ); sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp); ui = sp.getUI(); if( ui instanceof BasicSplitPaneUI ) { ((BasicSplitPaneUI)ui).getDivider().setBorder( null ); } sp.setBorder( BorderFactory.createEmptyBorder() ); sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, makePanel(), sp); ui = sp.getUI(); if( ui instanceof BasicSplitPaneUI ) { ((BasicSplitPaneUI)ui).getDivider().setBorder( null ); } sp.setBorder( BorderFactory.createEmptyBorder() ); 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; } } 
+2
source

An alternative answer is to set an empty border for each Component in JSplitPane - for example,

 JComponent a = ...; JComponent b = ...; a.setBorder(BorderFactory.createEmptyBorder()); b.setBorder(BorderFactory.createEmptyBorder()); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, a, b); 
0
source

Just override setBroder with JScrollPane as

 public class MyScrollPane extends JScrollPane { ... @Override public void setBorder(Border b) {} } 
0
source

Source: https://habr.com/ru/post/1438643/


All Articles