Lately i've tried docking a tool bar to a split pane.. though I found each time it threw an exception

so heres some code I wrote that fixes this problem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import java.awt.BorderLayout; import java.awt.Component; import javax.swing.JSplitPane; import javax.swing.JToolBar;
public class MySplitPane extends JSplitPane { private boolean autoCorrectToolBarOrientation = true;
public MySplitPane(int newOrientation, boolean newContinuousLayout, Component newLeftComponent, Component newRightComponent) { super(newOrientation, newContinuousLayout, newLeftComponent, newRightComponent); }
public MySplitPane(int newOrientation, Component newLeftComponent, Component newRightComponent) { super(newOrientation, newLeftComponent, newRightComponent); }
public MySplitPane(int newOrientation, boolean newContinuousLayout) { super(newOrientation, newContinuousLayout); }
public MySplitPane(int newOrientation) { super(newOrientation); }
public MySplitPane() { }
public boolean isAutoCorrectToolBarOrientation() { return autoCorrectToolBarOrientation; }
public void setAutoCorrectToolBarOrientation(boolean autoCorrectToolBarOrientation) { this.autoCorrectToolBarOrientation = autoCorrectToolBarOrientation; }
@Override protected void addImpl(Component comp, Object constraints, int index) { if (comp instanceof JToolBar) { if (this.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) { if (constraints.equals(BorderLayout.WEST) || constraints.equals(BorderLayout.SOUTH)) { constraints = JSplitPane.LEFT; } else if (constraints.equals(BorderLayout.EAST) || constraints.equals(BorderLayout.NORTH)) { constraints = JSplitPane.RIGHT; } if (constraints.equals(JSplitPane.LEFT) && super.getLeftComponent() != null) { if (this.getRightComponent() == null) { constraints = JSplitPane.RIGHT; } else { return; } } if (constraints.equals(JSplitPane.RIGHT) && super.getRightComponent() != null) { if (this.getLeftComponent() == null) { constraints = JSplitPane.LEFT; } else { return; } } } else if (this.getOrientation() == JSplitPane.VERTICAL_SPLIT) { if (constraints.equals(BorderLayout.WEST) || constraints.equals(BorderLayout.SOUTH)) { constraints = JSplitPane.BOTTOM; } else if (constraints.equals(BorderLayout.EAST) || constraints.equals(BorderLayout.NORTH)) { constraints = JSplitPane.TOP; } if (constraints.equals(JSplitPane.TOP) && super.getTopComponent() != null) { if (this.getBottomComponent() == null) { constraints = JSplitPane.BOTTOM; } else { return; } } if (constraints.equals(JSplitPane.BOTTOM) && super.getBottomComponent() != null) { if (this.getTopComponent() == null) { constraints = JSplitPane.TOP; } else { return; } } } } super.addImpl(comp, constraints, index); if (autoCorrectToolBarOrientation) { if (comp instanceof JToolBar) { if (this.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) { ((JToolBar) comp).setOrientation(JToolBar.VERTICAL); } else if (this.getOrientation() == JSplitPane.VERTICAL_SPLIT) { ((JToolBar) comp).setOrientation(JToolBar.HORIZONTAL); } } } } } |
use this class instead of JSplitPane
have fun, and report any problems to me