src/share/classes/javax/swing/plaf/metal/MetalComboBoxButton.java

Print this page




  37  * JButton subclass to help out MetalComboBoxUI
  38  * <p>
  39  * <strong>Warning:</strong>
  40  * Serialized objects of this class will not be compatible with
  41  * future Swing releases. The current serialization support is
  42  * appropriate for short term storage or RMI between applications running
  43  * the same version of Swing.  As of 1.4, support for long term storage
  44  * of all JavaBeans&trade;
  45  * has been added to the <code>java.beans</code> package.
  46  * Please see {@link java.beans.XMLEncoder}.
  47  *
  48  * @see MetalComboBoxButton
  49  * @author Tom Santos
  50  */
  51 @SuppressWarnings("serial") // Same-version serialization only
  52 public class MetalComboBoxButton extends JButton {
  53 
  54     /**
  55      * The instance of {@code JComboBox}.
  56      */
  57     protected JComboBox comboBox;
  58 
  59     /**
  60      * The instance of {@code JList}.
  61      */
  62     protected JList listBox;
  63 
  64     /**
  65      * The instance of {@code CellRendererPane}.
  66      */
  67     protected CellRendererPane rendererPane;
  68 
  69     /**
  70      * The icon.
  71      */
  72     protected Icon comboIcon;
  73 
  74     /**
  75      * The {@code iconOnly} value.
  76      */
  77     protected boolean iconOnly = false;
  78 
  79     /**
  80      * Returns the {@code JComboBox}.
  81      *
  82      * @return the {@code JComboBox}
  83      */
  84     public final JComboBox getComboBox() { return comboBox;}
  85 
  86     /**
  87      * Sets the {@code JComboBox}.
  88      *
  89      * @param cb the {@code JComboBox}
  90      */
  91     public final void setComboBox( JComboBox cb ) { comboBox = cb;}
  92 
  93     /**
  94      * Returns the icon of the {@code JComboBox}.
  95      *
  96      * @return the icon of the {@code JComboBox}
  97      */
  98     public final Icon getComboIcon() { return comboIcon;}
  99 
 100     /**
 101      * Sets the icon of the {@code JComboBox}.
 102      *
 103      * @param i the icon of the {@code JComboBox}
 104      */
 105     public final void setComboIcon( Icon i ) { comboIcon = i;}
 106 
 107     /**
 108      * Returns the {@code isIconOnly} value.
 109      *
 110      * @return the {@code isIconOnly} value
 111      */


 119     public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
 120 
 121     MetalComboBoxButton() {
 122         super( "" );
 123         DefaultButtonModel model = new DefaultButtonModel() {
 124             public void setArmed( boolean armed ) {
 125                 super.setArmed( isPressed() ? true : armed );
 126             }
 127         };
 128         setModel( model );
 129     }
 130 
 131     /**
 132      * Constructs a new instance of {@code MetalComboBoxButton}.
 133      *
 134      * @param cb an instance of {@code JComboBox}
 135      * @param i an icon
 136      * @param pane an instance of {@code CellRendererPane}
 137      * @param list an instance of {@code JList}
 138      */
 139     public MetalComboBoxButton( JComboBox cb, Icon i,
 140                                 CellRendererPane pane, JList list ) {
 141         this();
 142         comboBox = cb;
 143         comboIcon = i;
 144         rendererPane = pane;
 145         listBox = list;
 146         setEnabled( comboBox.isEnabled() );
 147     }
 148 
 149     /**
 150      * Constructs a new instance of {@code MetalComboBoxButton}.
 151      *
 152      * @param cb an instance of {@code JComboBox}
 153      * @param i an icon
 154      * @param onlyIcon if {@code true} only icon is painted
 155      * @param pane an instance of {@code CellRendererPane}
 156      * @param list an instance of {@code JList}
 157      */
 158     public MetalComboBoxButton( JComboBox cb, Icon i, boolean onlyIcon,
 159                                 CellRendererPane pane, JList list ) {
 160         this( cb, i, pane, list );
 161         iconOnly = onlyIcon;
 162     }
 163 
 164     public boolean isFocusTraversable() {
 165         return false;
 166     }
 167 
 168     public void setEnabled(boolean enabled) {
 169         super.setEnabled(enabled);
 170 
 171         // Set the background and foreground to the combobox colors.
 172         if (enabled) {
 173             setBackground(comboBox.getBackground());
 174             setForeground(comboBox.getForeground());
 175         } else {
 176             setBackground(UIManager.getColor("ComboBox.disabledBackground"));
 177             setForeground(UIManager.getColor("ComboBox.disabledForeground"));
 178         }
 179     }


 221                 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
 222             }
 223 
 224             comboIcon.paintIcon( this, g, iconLeft, iconTop );
 225 
 226             // Paint the focus
 227             if ( comboBox.hasFocus() && (!MetalLookAndFeel.usingOcean() ||
 228                                          comboBox.isEditable())) {
 229                 g.setColor( MetalLookAndFeel.getFocusColor() );
 230                 g.drawRect( left - 1, top - 1, width + 3, height + 1 );
 231             }
 232         }
 233 
 234         if (MetalLookAndFeel.usingOcean()) {
 235             // With Ocean the button only paints the arrow, bail.
 236             return;
 237         }
 238 
 239         // Let the renderer paint
 240         if ( ! iconOnly && comboBox != null ) {
 241             ListCellRenderer renderer = comboBox.getRenderer();
 242             Component c;
 243             boolean renderPressed = getModel().isPressed();
 244             c = renderer.getListCellRendererComponent(listBox,
 245                                                       comboBox.getSelectedItem(),
 246                                                       -1,
 247                                                       renderPressed,
 248                                                       false);
 249             c.setFont(rendererPane.getFont());
 250 
 251             if ( model.isArmed() && model.isPressed() ) {
 252                 if ( isOpaque() ) {
 253                     c.setBackground(UIManager.getColor("Button.select"));
 254                 }
 255                 c.setForeground(comboBox.getForeground());
 256             }
 257             else if ( !comboBox.isEnabled() ) {
 258                 if ( isOpaque() ) {
 259                     c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
 260                 }
 261                 c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));




  37  * JButton subclass to help out MetalComboBoxUI
  38  * <p>
  39  * <strong>Warning:</strong>
  40  * Serialized objects of this class will not be compatible with
  41  * future Swing releases. The current serialization support is
  42  * appropriate for short term storage or RMI between applications running
  43  * the same version of Swing.  As of 1.4, support for long term storage
  44  * of all JavaBeans&trade;
  45  * has been added to the <code>java.beans</code> package.
  46  * Please see {@link java.beans.XMLEncoder}.
  47  *
  48  * @see MetalComboBoxButton
  49  * @author Tom Santos
  50  */
  51 @SuppressWarnings("serial") // Same-version serialization only
  52 public class MetalComboBoxButton extends JButton {
  53 
  54     /**
  55      * The instance of {@code JComboBox}.
  56      */
  57     protected JComboBox<Object> comboBox;
  58 
  59     /**
  60      * The instance of {@code JList}.
  61      */
  62     protected JList<Object> listBox;
  63 
  64     /**
  65      * The instance of {@code CellRendererPane}.
  66      */
  67     protected CellRendererPane rendererPane;
  68 
  69     /**
  70      * The icon.
  71      */
  72     protected Icon comboIcon;
  73 
  74     /**
  75      * The {@code iconOnly} value.
  76      */
  77     protected boolean iconOnly = false;
  78 
  79     /**
  80      * Returns the {@code JComboBox}.
  81      *
  82      * @return the {@code JComboBox}
  83      */
  84     public final JComboBox<Object> getComboBox() { return comboBox;}
  85 
  86     /**
  87      * Sets the {@code JComboBox}.
  88      *
  89      * @param cb the {@code JComboBox}
  90      */
  91     public final void setComboBox( JComboBox<Object> cb ) { comboBox = cb;}
  92 
  93     /**
  94      * Returns the icon of the {@code JComboBox}.
  95      *
  96      * @return the icon of the {@code JComboBox}
  97      */
  98     public final Icon getComboIcon() { return comboIcon;}
  99 
 100     /**
 101      * Sets the icon of the {@code JComboBox}.
 102      *
 103      * @param i the icon of the {@code JComboBox}
 104      */
 105     public final void setComboIcon( Icon i ) { comboIcon = i;}
 106 
 107     /**
 108      * Returns the {@code isIconOnly} value.
 109      *
 110      * @return the {@code isIconOnly} value
 111      */


 119     public final void setIconOnly( boolean isIconOnly ) { iconOnly = isIconOnly;}
 120 
 121     MetalComboBoxButton() {
 122         super( "" );
 123         DefaultButtonModel model = new DefaultButtonModel() {
 124             public void setArmed( boolean armed ) {
 125                 super.setArmed( isPressed() ? true : armed );
 126             }
 127         };
 128         setModel( model );
 129     }
 130 
 131     /**
 132      * Constructs a new instance of {@code MetalComboBoxButton}.
 133      *
 134      * @param cb an instance of {@code JComboBox}
 135      * @param i an icon
 136      * @param pane an instance of {@code CellRendererPane}
 137      * @param list an instance of {@code JList}
 138      */
 139     public MetalComboBoxButton( JComboBox<Object> cb, Icon i,
 140                                 CellRendererPane pane, JList<Object> list ) {
 141         this();
 142         comboBox = cb;
 143         comboIcon = i;
 144         rendererPane = pane;
 145         listBox = list;
 146         setEnabled( comboBox.isEnabled() );
 147     }
 148 
 149     /**
 150      * Constructs a new instance of {@code MetalComboBoxButton}.
 151      *
 152      * @param cb an instance of {@code JComboBox}
 153      * @param i an icon
 154      * @param onlyIcon if {@code true} only icon is painted
 155      * @param pane an instance of {@code CellRendererPane}
 156      * @param list an instance of {@code JList}
 157      */
 158     public MetalComboBoxButton( JComboBox<Object> cb, Icon i, boolean onlyIcon,
 159                                 CellRendererPane pane, JList<Object> list ) {
 160         this( cb, i, pane, list );
 161         iconOnly = onlyIcon;
 162     }
 163 
 164     public boolean isFocusTraversable() {
 165         return false;
 166     }
 167 
 168     public void setEnabled(boolean enabled) {
 169         super.setEnabled(enabled);
 170 
 171         // Set the background and foreground to the combobox colors.
 172         if (enabled) {
 173             setBackground(comboBox.getBackground());
 174             setForeground(comboBox.getForeground());
 175         } else {
 176             setBackground(UIManager.getColor("ComboBox.disabledBackground"));
 177             setForeground(UIManager.getColor("ComboBox.disabledForeground"));
 178         }
 179     }


 221                 iconTop = (top + ((bottom - top) / 2)) - (iconHeight / 2);
 222             }
 223 
 224             comboIcon.paintIcon( this, g, iconLeft, iconTop );
 225 
 226             // Paint the focus
 227             if ( comboBox.hasFocus() && (!MetalLookAndFeel.usingOcean() ||
 228                                          comboBox.isEditable())) {
 229                 g.setColor( MetalLookAndFeel.getFocusColor() );
 230                 g.drawRect( left - 1, top - 1, width + 3, height + 1 );
 231             }
 232         }
 233 
 234         if (MetalLookAndFeel.usingOcean()) {
 235             // With Ocean the button only paints the arrow, bail.
 236             return;
 237         }
 238 
 239         // Let the renderer paint
 240         if ( ! iconOnly && comboBox != null ) {
 241              ListCellRenderer<Object> renderer = comboBox.getRenderer();
 242             Component c;
 243             boolean renderPressed = getModel().isPressed();
 244             c = renderer.getListCellRendererComponent(listBox,
 245                                                       comboBox.getSelectedItem(),
 246                                                       -1,
 247                                                       renderPressed,
 248                                                       false);
 249             c.setFont(rendererPane.getFont());
 250 
 251             if ( model.isArmed() && model.isPressed() ) {
 252                 if ( isOpaque() ) {
 253                     c.setBackground(UIManager.getColor("Button.select"));
 254                 }
 255                 c.setForeground(comboBox.getForeground());
 256             }
 257             else if ( !comboBox.isEnabled() ) {
 258                 if ( isOpaque() ) {
 259                     c.setBackground(UIManager.getColor("ComboBox.disabledBackground"));
 260                 }
 261                 c.setForeground(UIManager.getColor("ComboBox.disabledForeground"));