package com.onaro.util.jfc; import javax.swing.JComboBox; import java.awt.Dimension; /** * Fixes the toolbar's problem in which the combo-box consume all the * space available in the toolbar by limiting its size. */ /** * @param */ public class OnaroComboBox extends JComboBox { private static final long serialVersionUID = 1L; private final int maxBoxWidth; private int minListWidth = -1; /** * Create a combo box containing the given items. The box will be as * wide as the widest item string up to the specified maximum width. * Its size will not vary. The drop-down list will be as wide as necessary * to show all the item strings. *

* If {@link #setPreferredSize} or {@link #setPreferredSize} is invoked * before the box is rendered, then the arguments passed to those methods * will determine the box and list size. * * @param maxBoxWidth Maximum allowed width of the box * @param items Objects selected in drop-down list */ @SafeVarargs public OnaroComboBox(int maxBoxWidth, T... items) { super(items); this.maxBoxWidth = maxBoxWidth; } /** * Create a combo box containing the given items. The box will be as wide * as the widest item string. Its size will not vary. * * @param items Objects selected in drop-down list */ @SafeVarargs public OnaroComboBox(T... items) { this (Integer.MAX_VALUE, items); } /** * Fixes the toolbar's problem in which the combo-box consume all the * space available in the toolbar by limiting its size. *

* BSK: This method doesn't do exactly what it says above, but retaining * it for backward compatibility. What it does is prevent the box * from getting bigger than its preferred size. If its preferred * size is very wide, it will still consume all the width available * in the toolbar. To limit the preferred size, pass an int to the * {@link #OnaroComboBox constructor}. * * @return the maximum allowed size for this combo-box */ @Override public Dimension getMaximumSize() { return getPreferredSize(); } @SuppressWarnings("unchecked") public T getSelectedObject() { return (T) getSelectedItem(); } /* * This gets the size of the box in the toolbar. Generally, * the minimum and preferred sizes of this box are the same. * The only possible exception is if the caller has invoked * setPreferredSize and/or setMinimumSize(). * * The width of the box is limited if maxWidth is passed to * the constructor. * * @see javax.swing.JComponent#getPreferredSize() */ @Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); // Adjust box size if requested and not done yet. if (! isPreferredSizeSet() && size.width > maxBoxWidth) { size.width = maxBoxWidth; setPreferredSize (size); } return size; } /* * This gets the size of the box in the toolbar. Generally, * the minimum and preferred sizes of this box are the same. * The only possible exception is if the caller has invoked * setPreferredSize and/or setMinimumSize(). * * The original width of the toolbar box is saved so that it * can be used when setting the size of the drop-down list. * * @see javax.swing.JComponent#getMinimumSize() */ @Override public Dimension getMinimumSize() { Dimension size = super.getMinimumSize(); // Adjust box size if necessary, but not list size. if (! isMinimumSizeSet() && maxBoxWidth < Integer.MAX_VALUE) { // Minimum list width is original box width. minListWidth = size.width; setMinimumSize (getPreferredSize()); } return size; } /* * This gets the size of the drop-down list box. * * Normally, the width of the list is the same as the width * of the toolbar box. But when the width of the toolbar box * has been reduced, we want to restore the list width to its * original width. In that case, the list width will be wider * than the toolbar box. That way the complete text for each * item will be visible when changing the selection. * * @see java.awt.Component#getSize() */ @Override public Dimension getSize() { Dimension size = super.getSize(); // Ensure drop-down list width is wide enough for widest item. if (minListWidth > 0) { size.width = minListWidth; } return size; } @Override public void setSize (Dimension size) { if (minListWidth > 0 && minListWidth < size.width) { minListWidth = size.width; } super.setSize (size); } }