Fix for docking JToolBars to JSplitPanes

Lately i’ve tried docking a tool bar to a split pane… though I found each time it threw an exception :frowning:

so heres some code I wrote that fixes this problem


import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;

/**
 *
 * @author David
 */
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) {
					//System.out.println("UNDOCKABLE");
					if (this.getRightComponent() == null) {
						constraints = JSplitPane.RIGHT;
					} else {
						return;
					}
				}
				if (constraints.equals(JSplitPane.RIGHT) && super.getRightComponent() != null) {
					//System.out.println("UNDOCKABLE");
					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) {
					//System.out.println("UNDOCKABLE");
					if (this.getBottomComponent() == null) {
						constraints = JSplitPane.BOTTOM;
					} else {
						return;
					}
				}
				if (constraints.equals(JSplitPane.BOTTOM) && super.getBottomComponent() != null) {
					//System.out.println("UNDOCKABLE");
					if (this.getTopComponent() == null) {
						constraints = JSplitPane.TOP;
					} else {
						return;
					}
				}
			}
		}
		//System.out.println("Constraints: " + constraints);
		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