📄 combobox.html
字号:
class ComboBoxRenderer extends JLabel implements ListCellRenderer { . . . public ComboBoxRenderer() { setOpaque(true); setHorizontalAlignment(CENTER); setVerticalAlignment(CENTER); } /* * This method finds the image and text corresponding * to the selected value and returns the label, set up * to display the text and image. */ public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { //Get the selected index. (The index param isn't //always valid, so just use the value.) int selectedIndex = ((Integer)value).intValue(); if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } //Set the icon and text. If icon was null, say so. ImageIcon icon = images[selectedIndex]; String pet = petStrings[selectedIndex]; setIcon(icon); if (icon != null) { setText(pet); setFont(list.getFont()); } else { setUhOhText(pet + " (no image available)", list.getFont()); } return this; } . . .}</pre></blockquote>As a <code>ListCellRenderer</code>,<code>ComboBoxRenderer</code> implements a methodcalled <code>getListCellRendererComponent</code>,which returns a component whose <code>paintComponent</code>method is used to display the combo box and each of its items.The easiest way to display an image and an iconis to use a label.So <code>ComboBoxRenderer</code> is a subclass of labeland returns itself.The implementation of <code>getListCellRendererComponent</code>configures the renderer to display the currently selectedicon and its description.<p>These arguments are passed to <code>getListCellRendererComponent</code>:<ul><li> <code>JList list</code> — a list object used behind the scenes to display the items. The example uses this object's colors to set up foreground and background colors.<li> <code>Object value</code> — the object to render. An <code>Integer</code> in this example.<li> <code>int index</code> — the index of the object to render.<li> <code>boolean isSelected</code> — indicates whether the object to render is selected. Used by the example to determine which colors to use.<li> <code>boolean cellHasFocus</code> — indicates whether the object to render has the focus.</ul>Note that combo boxes and <a href="list.html">lists</a>use the same type of renderer —<code>ListCellRenderer</code>.You can save yourself some timeby sharing renderers betweencombo boxes and lists,if it makes sense for your program.</blockquote><h3><a name="api">The Combo Box API</a></h3><blockquote>The following tables list the commonly used<code>JComboBox</code> constructors and methods.Other methods you are most likely to invoke ona <code>JComboBox</code> object are thoseit inherits from its superclasses,such as <code>setPreferredSize</code>.See<a href="jcomponent.html#api">The JComponent API</a>for tables of commonly used inherited methods.<p>The API for using combo boxes falls into two categories:<ul><li><a href="#list">Setting or Getting the Items in the Combo Box's Menu</a><li><a href="#configuring">Customizing the Combo Box's Operation</a></ul><p><table border=1><caption><a name="list">Setting or Getting the Items in the Combo Boxes's Menu</a></caption><tr><th>Method</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#JComboBox()">JComboBox()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#JComboBox(javax.swing.ComboBoxModel)">JComboBox(ComboBoxModel)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#JComboBox(java.lang.Object[])">JComboBox(Object[])</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#JComboBox(java.util.Vector)">JComboBox(Vector)</a> </td> <td>Create a combo box with the specified items in its menu. A combo box created with the default constructor has no items in the menu initially. Each of the other constructors initializes the menu from its argument: a model object, an array of objects, or a <code>Vector</code> of objects. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#addItem(java.lang.Object)">void addItem(Object)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#insertItemAt(java.lang.Object, int)">void insertItemAt(Object, int)</a> </td> <td>Add or insert the specified object into the combo box's menu. The insert method places the specified object <em>at</em> the specified index, thus inserting it before the object currently at that index. These methods require that the combo box's data model be an instance of <code>MutableComboBoxModel</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getItemAt(int)">Object getItemAt(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getSelectedItem()">Object getSelectedItem()</a> </td> <td>Get an item from the combo box's menu. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#removeAllItems()">void removeAllItems()</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#removeItemAt(int)">void removeItemAt(int)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#removeItem(java.lang.Object)">void removeItem(Object)</a> </td> <td>Remove one or more items from the combo box's menu. These methods require that the combo box's data model be an instance of <code>MutableComboBoxModel</code>. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getItemCount()">int getItemCount()</a> </td> <td>Get the number of items in the combo box's menu. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setModel(javax.swing.ComboBoxModel)">void setModel(ComboBoxModel)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getModel()">ComboBoxModel getModel()</a> </td> <td>Set or get the data model that provides the items in the combo box's menu. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setAction(javax.swing.Action)">void setAction(Action)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getAction()">Action getAction()</a> </td> <td>Set or get the <code>Action</code> associated with the combo box. For further information, see<a class="TutorialLink" target="_top" href="../misc/action.html">How to Use Actions</a>. </td> </tr></table><p><table border=1><caption><a name="configuring">Customizing the Combo Box's Operation</a></caption><tr><th>Method or Constructor</th><th>Purpose</th></tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#addActionListener(java.awt.event.ActionListener)">void addActionListener(ActionListener)</a> </td> <td>Add an action listener to the combo box. The listener's <code>actionPerformed</code> method is called when the user selects an item from the combo box's menu or, in an editable combo box, when the user presses Enter. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#addItemListener(java.awt.event.ItemListener)">void addItemListener(ItemListener)</a> </td> <td>Add an item listener to the combo box. The listener's <code>itemStateChanged</code> method is called when the selection state of any of the combo box's items change. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setEditable(boolean)">void setEditable(boolean)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#isEditable()">boolean isEditable()</a> </td> <td>Set or get whether the user can type in the combo box. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setRenderer(javax.swing.ListCellRenderer)">void setRenderer(ListCellRenderer)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getRenderer()">ListCellRenderer getRenderer()</a> </td> <td>Set or get the object responsible for painting the selected item in the combo box. The renderer is used only when the combo box is uneditable. If the combo box is editable, the editor is used to paint the selected item instead. </td> </tr> <tr> <td><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#setEditor(javax.swing.ComboBoxEditor)">void setEditor(ComboBoxEditor)</a> <br><a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/JComboBox.html#getEditor()">ComboBoxEditor getEditor()</a> </td> <td>Set or get the object responsible for painting and editing the selected item in the combo box. The editor is used only when the combo box is editable. If the combo box is uneditable, the renderer is used to paint the selected item instead. </td> </tr></table></blockquote><h3><a name="eg">Examples that Use Combo Boxes</a></h3><blockquote>This table shows the examples that use <code>JComboBox</code>and where those examples are described.<p><table><tr><th align=left> Example</th><th align=left> Where Described</th><th align=left> Notes</th></tr><tr><td> <a href="examples/index.html#ComboBoxDemo"><code>ComboBoxDemo</code></a></td><td> This section</td><td> Uses an uneditable combo box.</td></tr><tr><td> <a href="examples/index.html#ComboBoxDemo2"><code>ComboBoxDemo2</code></a></td><td> This section</td><td> Uses an editable combo box.</td></tr><tr><td> <a href="examples/index.html#CustomComboBoxDemo"><code>CustomComboBoxDemo</code></a></td><td> This section</td><td> Provides a custom renderer for a combo box.</td></tr><tr><td><a href="examples/index.html#TableRenderDemo"><code>TableRenderDemo</code></a></td><td>How to Use Tables (<a href="table.html#combobox">Using a Combo Box as anEditor</a>)</td><td>Shows how to use a combo box as a table cell editor.</td></tr></table></blockquote> </blockquote> <div class=NavBit> <a target=_top href=colorchooser.html>« Previous</a> • <a target=_top href=../TOC.html>Trail</a> • <a target=_top href=dialog.html>Next »</a> </div> </div> <div id=Footer><div id=TagNotes> Problems with the examples? Try <a target="_blank" href=../../information/run-examples.html>Compiling and Running the Examples: FAQs</a>. <br> Complaints? Compliments? Suggestions? <a target="_blank" href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give us your feedback</a>.<br><br> <a target="_blank" href="../../information/copyright.html">Copyright</a> 1995-2006 Sun Microsystems, Inc. All rights reserved. <span id=Download></span></div> </div> <div class=PrintHeaders> <b>Previous page:</b> How to Use Color Choosers <br><b>Next page:</b> How to Make Dialogs </div> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -