JavaTM 2 Platform
Standard Ed. 6

javax.swing
类 JComboBox

java.lang.Object
  继承者 java.awt.Component
      继承者 java.awt.Container
          继承者 javax.swing.JComponent
              继承者 javax.swing.JComboBox
所有已实现的接口:
ActionListener, ImageObserver, ItemSelectable, MenuContainer, Serializable, EventListener, Accessible, ListDataListener

public class JComboBox
extends JComponent
implements ItemSelectable, ListDataListener, ActionListener, Accessible

将按钮或可编辑字段与下拉列表组合的组件。用户可以从下拉列表中选择值,下拉列表在用户请求时显示。如果使组合框处于可编辑状态,则组合框将包括用户可在其中键入值的可编辑字段。

警告:Swing 不是线程安全的。有关更多信息,请参阅 Swing's Threading Policy

警告:此类的序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder

有关更多信息,请参阅 The Java Tutorial 中的 How to Use Combo Boxes 一节。

另请参见:
ComboBoxModel, DefaultComboBoxModel

嵌套类摘要
protected  class JComboBox.AccessibleJComboBox
          此类实现 JComboBox 类的可访问性支持。
static interface JComboBox.KeySelectionManager
          定义 KeySelectionManager 的接口。
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
protected  String actionCommand
          此受保护字段是特定于实现的。
protected  ComboBoxModel dataModel
          此受保护字段是特定于实现的。
protected  ComboBoxEditor editor
          此受保护字段是特定于实现的。
protected  boolean isEditable
          此受保护字段是特定于实现的。
protected  JComboBox.KeySelectionManager keySelectionManager
          此受保护字段是特定于实现的。
protected  boolean lightWeightPopupEnabled
          此受保护字段是特定于实现的。
protected  int maximumRowCount
          此受保护字段是特定于实现的。
protected  ListCellRenderer renderer
          此受保护字段是特定于实现的。
protected  Object selectedItemReminder
          此受保护字段是特定于实现的。
 
从类 javax.swing.JComponent 继承的字段
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
JComboBox()
          创建具有默认数据模型的 JComboBox
JComboBox(ComboBoxModel aModel)
          创建一个 JComboBox,其项取自现有的 ComboBoxModel
JComboBox(Object[] items)
          创建包含指定数组中的元素的 JComboBox
JComboBox(Vector<?> items)
          创建包含指定 Vector 中的元素的 JComboBox
 
方法摘要
 void actionPerformed(ActionEvent e)
          此方法由于实现的副作用而存在的公共方法。
protected  void actionPropertyChanged(Action action, String propertyName)
          更新组合框的状态以响应关联动作中的属性更改。
 void addActionListener(ActionListener l)
          添加 ActionListener
 void addItem(Object anObject)
          为项列表添加项。
 void addItemListener(ItemListener aListener)
          添加 ItemListener
 void addPopupMenuListener(PopupMenuListener l)
          添加 PopupMenu 侦听器,该侦听器将侦听取自组合框弹出部分的通知消息。
 void configureEditor(ComboBoxEditor anEditor, Object anItem)
          利用指定项初始化编辑器。
protected  void configurePropertiesFromAction(Action a)
          在此组合框上设置属性以匹配指定 Action 中的属性。
 void contentsChanged(ListDataEvent e)
          此方法作为实现的副作用存在的公共方法。
protected  PropertyChangeListener createActionPropertyChangeListener(Action a)
          创建并返回一个 PropertyChangeListener,它负责侦听指定 Action 的更改并更新适当属性。
protected  JComboBox.KeySelectionManager createDefaultKeySelectionManager()
          返回默认键选择管理器的实例。
protected  void fireActionEvent()
          通知所有需要此事件类型的通知的已注册侦听器。
protected  void fireItemStateChanged(ItemEvent e)
          通知所有需要此事件类型的通知的已注册侦听器。
 void firePopupMenuCanceled()
          通知 PopupMenuListener 组合框的弹出部分已被取消。
 void firePopupMenuWillBecomeInvisible()
          通知 PopupMenuListener 组合框的弹出部分已变得不可见。
 void firePopupMenuWillBecomeVisible()
          通知 PopupMenuListener 组合框的弹出部分将变得可见。
 AccessibleContext getAccessibleContext()
          获取与此 JComboBox 关联的 AccessibleContext。
 Action getAction()
          返回此 ActionEvent 源当前设置的 Action,如果没有设置任何 Action,则返回 null
 String getActionCommand()
          返回发送到动作侦听器的事件中包括的动作命令。
 ActionListener[] getActionListeners()
          返回使用 addActionListener() 添加到此 JComboBox 的所有 ActionListener 组成的数组。
 ComboBoxEditor getEditor()
          返回用于绘制和编辑 JComboBox 字段中所选项的编辑器。
 Object getItemAt(int index)
          返回指定索引处的列表项。
 int getItemCount()
          返回列表中的项数。
 ItemListener[] getItemListeners()
          返回使用 addItemListener() 添加到此 JComboBox 中的所有 ItemListener 组成的数组。
 JComboBox.KeySelectionManager getKeySelectionManager()
          返回列表的键选择管理器。
 int getMaximumRowCount()
          返回组合框不使用滚动条可以显示的最大项数
 ComboBoxModel getModel()
          返回 JComboBox 当前使用的数据模型。
 PopupMenuListener[] getPopupMenuListeners()
          返回利用 addPopupMenuListener() 添加到此 JComboBox 的所有 PopupMenuListener 组成的数组。
 Object getPrototypeDisplayValue()
          返回“原型显示”值,即用于计算显示高度和宽度的 Object。
 ListCellRenderer getRenderer()
          返回用于显示 JComboBox 字段中所选项的渲染器。
 int getSelectedIndex()
          返回列表中与给定项匹配的第一个选项。
 Object getSelectedItem()
          返回当前所选项。
 Object[] getSelectedObjects()
          返回包含所选项的数组。
 ComboBoxUI getUI()
          返回呈现此组件的 L&F 对象。
 String getUIClassID()
          返回呈现此组件的 L&F 类的名称。
 void hidePopup()
          促使组合框关闭其弹出窗口。
 void insertItemAt(Object anObject, int index)
          在项列表中的给定索引处插入项。
protected  void installAncestorListener()
           
 void intervalAdded(ListDataEvent e)
          此方法作为实现的副作用存在的公共方法。
 void intervalRemoved(ListDataEvent e)
          此方法作为实现的副作用存在的公共方法。
 boolean isEditable()
          如果 JComboBox 可编辑,则返回 true。
 boolean isLightWeightPopupEnabled()
          获取 lightWeightPopupEnabled 属性的值。
 boolean isPopupVisible()
          确定弹出窗口的可见性。
protected  String paramString()
          返回此 JComboBox 的字符串表示形式。
 void processKeyEvent(KeyEvent e)
          处理 KeyEvent,查找 Tab 键。
 void removeActionListener(ActionListener l)
          移除 ActionListener
 void removeAllItems()
          从项列表中移除所有项。
 void removeItem(Object anObject)
          从项列表中移除项。
 void removeItemAt(int anIndex)
          移除 anIndex 处的项。
 void removeItemListener(ItemListener aListener)
          移除 ItemListener
 void removePopupMenuListener(PopupMenuListener l)
          移除 PopupMenuListener
protected  void selectedItemChanged()
          此受保护方法是特定于实现的。
 boolean selectWithKeyChar(char keyChar)
          如果存在与指定键盘字符相对应的项,则选择该列表项并返回 true。
 void setAction(Action a)
          设置 ActionEvent 源的 Action
 void setActionCommand(String aCommand)
          设置发送到动作侦听器的事件中应该包括的动作命令。
 void setEditable(boolean aFlag)
          确定 JComboBox 字段是否可编辑。
 void setEditor(ComboBoxEditor anEditor)
          设置用于绘制和编辑 JComboBox 字段中所选项的编辑器。
 void setEnabled(boolean b)
          启用组合框以便可以选择项。
 void setKeySelectionManager(JComboBox.KeySelectionManager aManager)
          设置将键盘字符转换为列表选择的对象。
 void setLightWeightPopupEnabled(boolean aFlag)
          设置 lightWeightPopupEnabled 属性,该属性提供一个提示:是应该使用重量级 Component(如 PanelWindow)还是轻量级 Component 来包含 JComboBox
 void setMaximumRowCount(int count)
          设置 JComboBox 显示的最大行数。
 void setModel(ComboBoxModel aModel)
          设置 JComboBox 用于获取项列表的数据模型。
 void setPopupVisible(boolean v)
          设置弹出窗口的可见性。
 void setPrototypeDisplayValue(Object prototypeDisplayValue)
          设置用于计算 UI 部分的显示大小的原型显示值。
 void setRenderer(ListCellRenderer aRenderer)
          设置渲染器,该渲染器用于绘制列表项和从 JComboBox 字段的列表中选择的项。
 void setSelectedIndex(int anIndex)
          选择索引 anIndex 处的项。
 void setSelectedItem(Object anObject)
          将组合框显示区域中所选项设置为参数中的对象。
 void setUI(ComboBoxUI ui)
          设置呈现此组件的 L&F 对象。
 void showPopup()
          促使组合框显示其弹出窗口。
 void updateUI()
          将 UI 属性重置为当前外观的值。
 
从类 javax.swing.JComponent 继承的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
从类 java.awt.Container 继承的方法
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
从类 java.awt.Component 继承的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

字段详细信息

dataModel

protected ComboBoxModel dataModel
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
getModel(), setModel(javax.swing.ComboBoxModel)

renderer

protected ListCellRenderer renderer
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
getRenderer(), setRenderer(javax.swing.ListCellRenderer)

editor

protected ComboBoxEditor editor
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
getEditor(), setEditor(javax.swing.ComboBoxEditor)

maximumRowCount

protected int maximumRowCount
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
getMaximumRowCount(), setMaximumRowCount(int)

isEditable

protected boolean isEditable
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
isEditable, setEditable(boolean)

keySelectionManager

protected JComboBox.KeySelectionManager keySelectionManager
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
setKeySelectionManager(javax.swing.JComboBox.KeySelectionManager), getKeySelectionManager()

actionCommand

protected String actionCommand
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
setActionCommand(java.lang.String), getActionCommand()

lightWeightPopupEnabled

protected boolean lightWeightPopupEnabled
此受保护字段是特定于实现的。不要直接访问或重写。要使用存取方法。

另请参见:
setLightWeightPopupEnabled(boolean), isLightWeightPopupEnabled()

selectedItemReminder

protected Object selectedItemReminder
此受保护字段是特定于实现的。不要直接访问或重写。

构造方法详细信息

JComboBox

public JComboBox(ComboBoxModel aModel)
创建一个 JComboBox,其项取自现有的 ComboBoxModel。由于提供了 ComboBoxModel,使用此构造方法创建的组合框不创建默认组合框模型,这可能影响插入、移除和添加方法的行为方式。

参数:
aModel - 提供显示的项列表的 ComboBoxModel
另请参见:
DefaultComboBoxModel

JComboBox

public JComboBox(Object[] items)
创建包含指定数组中的元素的 JComboBox。默认情况下,选择数组中的第一项(因而也选择了该项的数据模型)。

参数:
items - 要插入到组合框的对象数组
另请参见:
DefaultComboBoxModel

JComboBox

public JComboBox(Vector<?> items)
创建包含指定 Vector 中的元素的 JComboBox。默认情况下,选择数组中的第一项(因而也选择了该项的数据模型)。

参数:
items - 要插入到组合框的向量数组
另请参见:
DefaultComboBoxModel

JComboBox

public JComboBox()
创建具有默认数据模型的 JComboBox。默认的数据模型为空对象列表。使用 addItem 添加项。默认情况下,选择数据模型中的第一项。

另请参见:
DefaultComboBoxModel
方法详细信息

installAncestorListener

protected void installAncestorListener()

setUI

public void setUI(ComboBoxUI ui)
设置呈现此组件的 L&F 对象。

参数:
ui - ComboBoxUI L&F 对象
另请参见:
UIDefaults.getUI(javax.swing.JComponent)

updateUI

public void updateUI()
将 UI 属性重置为当前外观的值。

覆盖:
JComponent 中的 updateUI
另请参见:
JComponent.updateUI()

getUIClassID

public String getUIClassID()
返回呈现此组件的 L&F 类的名称。

覆盖:
JComponent 中的 getUIClassID
返回:
字符串“ComboBoxUI”
另请参见:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

getUI

public ComboBoxUI getUI()
返回呈现此组件的 L&F 对象。

返回:
呈现此组件的 ComboBoxUI 对象。

setModel

public void setModel(ComboBoxModel aModel)
设置 JComboBox 用于获取项列表的数据模型。

参数:
aModel - 提供显示的项列表的 ComboBoxModel

getModel

public ComboBoxModel getModel()
返回 JComboBox 当前使用的数据模型。

返回:
提供显示的项列表的 ComboBoxModel

setLightWeightPopupEnabled

public void setLightWeightPopupEnabled(boolean aFlag)
设置 lightWeightPopupEnabled 属性,该属性提供一个提示:是应该使用重量级 Component(如 PanelWindow)还是轻量级 Component 来包含 JComboBox。使用轻量级还是重量级组件,最终由 JComboBox 决定。轻量级窗口比重量级窗口更有效,但是轻量级和重量级组件在 GUI 中不能很好地混合使用。如果应用程序混合使用轻量级和重量级组件,则应该禁用轻量级弹出窗口。除非由外观另行指定,否则 lightWeightPopupEnabled 属性的默认值为 true。某些外观始终使用重量级弹出窗口,不管此属性的值如何。

请参阅 The Swing Connection 上的文章 Mixing Heavy and Light Components。此方法触发属性更改事件。

参数:
aFlag - 如果为 true,则需要轻量级弹出窗口

isLightWeightPopupEnabled

public boolean isLightWeightPopupEnabled()
获取 lightWeightPopupEnabled 属性的值。

返回:
lightWeightPopupEnabled 属性的值
另请参见:
setLightWeightPopupEnabled(boolean)

setEditable

public void setEditable(boolean aFlag)
确定 JComboBox 字段是否可编辑。可编辑的 JComboBox 允许用户在字段中键入内容或者从列表中选择项来初始化字段,执行此操作后,该字段就可编辑了。(编辑仅影响字段,列表项保持原样。)不可编辑的 JComboBox 在字段中显示所选项,但不能修改该选择。

参数:
aFlag - 一个 boolean 值,其中 true 指示字段可编辑。

isEditable

public boolean isEditable()
如果 JComboBox 可编辑,则返回 true。默认情况下,组合框不可编辑。

返回:
如果 JComboBox 可编辑,则返回 true;否则返回 false

setMaximumRowCount

public void setMaximumRowCount(int count)
设置 JComboBox 显示的最大行数。如果模型中的对象数大于 count,则组合框使用滚动条。

参数:
count - 一个整数,它指定使用滚动条前要在列表中显示的最大项数

getMaximumRowCount

public int getMaximumRowCount()
返回组合框不使用滚动条可以显示的最大项数

返回:
一个整数,它指定使用滚动条前列表中显示的最大项数

setRenderer

public void setRenderer(ListCellRenderer aRenderer)
设置渲染器,该渲染器用于绘制列表项和从 JComboBox 字段的列表中选择的项。该渲染器在 JComboBox 不可编辑时使用。如果其可编辑,则使用编辑器呈现和编辑所选项。

默认渲染器显示字符串或图标。其他渲染器可处理图形图像和复合选项。

要显示所选项,请调用 aRenderer.getListCellRendererComponent,其中传递列表对象和索引 -1。

参数:
aRenderer - 显示所选项的 ListCellRenderer
另请参见:
setEditor(javax.swing.ComboBoxEditor)

getRenderer

public ListCellRenderer getRenderer()
返回用于显示 JComboBox 字段中所选项的渲染器。

返回:
显示所选项的 ListCellRenderer

setEditor

public void setEditor(ComboBoxEditor anEditor)
设置用于绘制和编辑 JComboBox 字段中所选项的编辑器。仅当接收的 JComboBox 可编辑时才使用编辑器。如果不可编辑,则组合框使用渲染器绘制所选项。

参数:
anEditor - 显示所选项的 ComboBoxEditor
另请参见:
setRenderer(javax.swing.ListCellRenderer)

getEditor

public ComboBoxEditor getEditor()
返回用于绘制和编辑 JComboBox 字段中所选项的编辑器。

返回:
显示所选项的 ComboBoxEditor

setSelectedItem

public void setSelectedItem(Object anObject)
将组合框显示区域中所选项设置为参数中的对象。如果 anObject 在列表中,则显示区域显示所选的 anObject

如果 anObject 在列表中,且组合框不可编辑,则不会更改显示区域中的当前选择。对于可编辑的组合框,选择将更改为 anObject

如果此操作造成所选项发生更改,则添加到组合框的 ItemListener 将获得一个或两个 ItemEvent 通知。如果有当前所选项,则将触发 ItemEvent,而状态更改将为 ItemEvent.DESELECTED。如果 anObject 在列表中,但不是当前选择的项,则将触发 ItemEvent,而状态更改将为 ItemEvent.SELECTED

调用此方法时,添加到组合框的 ActionListener 将获得 ActionEvent 通知。

参数:
anObject - 要选择的列表对象;使用 null 清除选择

getSelectedItem

public Object getSelectedItem()
返回当前所选项。

如果组合框可编辑,则可能尚未使用 addIteminsertItemAt 或数据构造方法将此值添加到组合框中。

返回:
当前选择的 Object
另请参见:
setSelectedItem(java.lang.Object)

setSelectedIndex

public void setSelectedIndex(int anIndex)
选择索引 anIndex 处的项。

参数:
anIndex - 指定要选择的列表项的整数,其中 0 指定列表中的第一项,-1 指示没有作出选择
抛出:
IllegalArgumentException - 如果 anIndex < -1 或者 anIndex 大于等于列表大小

getSelectedIndex

public int getSelectedIndex()
返回列表中与给定项匹配的第一个选项。如果 JComboBox 允许选择不在列表中的项,则结果并非不总是确定的。如果不存在所选项或者用户指定的项不在列表中,则返回 -1。

返回:
指定当前选择的列表项的整数,其中 0 指定列表中的第一项;如果没有选择任何项或者当前所选项不在列表中,则返回 -1

getPrototypeDisplayValue

public Object getPrototypeDisplayValue()
返回“原型显示”值,即用于计算显示高度和宽度的 Object。

返回:
prototypeDisplayValue 属性的值
从以下版本开始:
1.4
另请参见:
setPrototypeDisplayValue(java.lang.Object)

setPrototypeDisplayValue

public void setPrototypeDisplayValue(Object prototypeDisplayValue)
设置用于计算 UI 部分的显示大小的原型显示值。

如果指定了原型显示值,则组合框首选大小的计算方式是:利用原型显示值配置渲染器,然后获取其首选大小。指定首选显示值在组合框将显示大量数据时往往很有用。如果没有指定原型显示值,则必须为模型中的每个值及其获取的首选大小配置渲染器,其花费相对昂贵。

参数:
prototypeDisplayValue -
从以下版本开始:
1.4
另请参见:
getPrototypeDisplayValue()

addItem

public void addItem(Object anObject)
为项列表添加项。仅当 JComboBox 使用可变数据模型时此方法才有效。

警告:如果添加复制的 String 对象,可能会发生焦点和键盘导航问题。解决方法是,添加新对象而不是 String 对象,并且确保定义了 toString() 方法。例如:

   comboBox.addItem(makeObj("Item 1"));
   comboBox.addItem(makeObj("Item 1"));
   ...
   private Object makeObj(final String item)  {
     return new Object() { public String toString() { return item; } };
   }
 

参数:
anObject - 要添加到列表的 Object
另请参见:
MutableComboBoxModel

insertItemAt

public void insertItemAt(Object anObject,
                         int index)
在项列表中的给定索引处插入项。仅当 JComboBox 使用可变数据模型时此方法才有效。

参数:
anObject - 要添加到列表的 Object
index - 指定添加项的位置的整数
另请参见:
MutableComboBoxModel

removeItem

public void removeItem(Object anObject)
从项列表中移除项。仅当 JComboBox 使用可变数据模型时此方法才有效。

参数:
anObject - 要从项列表中移除的对象
另请参见:
MutableComboBoxModel

removeItemAt

public void removeItemAt(int anIndex)
移除 anIndex 处的项。仅当 JComboBox 使用可变数据模型时此方法才有效。

参数:
anIndex - 指定要移除的项索引的 int,其中 0 指示列表中的第一项
另请参见:
MutableComboBoxModel

removeAllItems

public void removeAllItems()
从项列表中移除所有项。


showPopup

public void showPopup()
促使组合框显示其弹出窗口。

另请参见:
setPopupVisible(boolean)

hidePopup

public void hidePopup()
促使组合框关闭其弹出窗口。

另请参见:
setPopupVisible(boolean)

setPopupVisible

public void setPopupVisible(boolean v)
设置弹出窗口的可见性。


isPopupVisible

public boolean isPopupVisible()
确定弹出窗口的可见性。

返回:
如果弹出窗口可见,则返回 true;否则返回 false

addItemListener

public void addItemListener(ItemListener aListener)
添加 ItemListener

当所选项更改时,aListener 将接收一个或两个 ItemEvent

指定者:
接口 ItemSelectable 中的 addItemListener
参数:
aListener - 要通知的 ItemListener
另请参见:
setSelectedItem(java.lang.Object)

removeItemListener

public void removeItemListener(ItemListener aListener)
移除 ItemListener

指定者:
接口 ItemSelectable 中的 removeItemListener
参数:
aListener - 要移除的 ItemListener
另请参见:
ItemEvent

getItemListeners

public ItemListener[] getItemListeners()
返回使用 addItemListener() 添加到此 JComboBox 中的所有 ItemListener 组成的数组。

返回:
添加的所有 ItemListener,如果没有添加侦听器,则返回一个空数组
从以下版本开始:
1.4

addActionListener

public void addActionListener(ActionListener l)
添加 ActionListener

当作出选择时,ActionListener 将接收一个 ActionEvent。如果组合框可编辑,则在编辑停止时将触发 ActionEvent

参数:
l - 要通知的 ActionListener
另请参见:
setSelectedItem(java.lang.Object)

removeActionListener

public void removeActionListener(ActionListener l)
移除 ActionListener

参数:
l - 要移除的 ActionListener

getActionListeners

public ActionListener[] getActionListeners()
返回使用 addActionListener() 添加到此 JComboBox 的所有 ActionListener 组成的数组。

返回:
所有添加的 ActionListener;如果没有添加侦听器,则返回一个空数组
从以下版本开始:
1.4

addPopupMenuListener

public void addPopupMenuListener(PopupMenuListener l)
添加 PopupMenu 侦听器,该侦听器将侦听取自组合框弹出部分的通知消息。

对于 Java 附带的所有标准外观,组合框的弹出列表部分将被实现为 JPopupMenu。自定义外观可能不以这种方式实现组合框的弹出列表部分,因此不接收通知。

参数:
l - 要添加的 PopupMenuListener
从以下版本开始:
1.4

removePopupMenuListener

public void removePopupMenuListener(PopupMenuListener l)
移除 PopupMenuListener

参数:
l - 要移除的 PopupMenuListener
从以下版本开始:
1.4
另请参见:
addPopupMenuListener(javax.swing.event.PopupMenuListener)

getPopupMenuListeners

public PopupMenuListener[] getPopupMenuListeners()
返回利用 addPopupMenuListener() 添加到此 JComboBox 的所有 PopupMenuListener 组成的数组。

返回:
所有添加的 PopupMenuListener,如果没有添加侦听器,则返回一个空数组
从以下版本开始:
1.4

firePopupMenuWillBecomeVisible

public void firePopupMenuWillBecomeVisible()
通知 PopupMenuListener 组合框的弹出部分将变得可见。

此方法是公共方法,但只能由 UI 委托调用。

从以下版本开始:
1.4
另请参见:
addPopupMenuListener(javax.swing.event.PopupMenuListener)

firePopupMenuWillBecomeInvisible

public void firePopupMenuWillBecomeInvisible()
通知 PopupMenuListener 组合框的弹出部分已变得不可见。

此方法是公共方法,但只能由 UI 委托调用。

从以下版本开始:
1.4
另请参见:
addPopupMenuListener(javax.swing.event.PopupMenuListener)

firePopupMenuCanceled

public void firePopupMenuCanceled()
通知 PopupMenuListener 组合框的弹出部分已被取消。

此方法是公共方法,但只能由 UI 委托调用。

从以下版本开始:
1.4
另请参见:
addPopupMenuListener(javax.swing.event.PopupMenuListener)

setActionCommand

public void setActionCommand(String aCommand)
设置发送到动作侦听器的事件中应该包括的动作命令。

参数:
aCommand - 包含发送到动作侦听器的“命令”的字符串;根据其所接受的命令,相同的侦听器可以做不同的事情

getActionCommand

public String getActionCommand()
返回发送到动作侦听器的事件中包括的动作命令。

返回:
包含发送到动作侦听器的“命令”的字符串。

setAction

public void setAction(Action a)
设置 ActionEvent 源的 Action。新的 Action 替换以前设置的所有 Action,而不影响通过 addActionListener 独立添加的 ActionListener。如果 Action 已经是一个注册到 ActionEvent 源的 ActionListener,则不对它进行重新注册。

设置 Action 将导致立即更改支持 Action 的 Swing 组件中所描述的所有属性。随后,组合框的属性将随 Action 属性的更改自动更新。

此方法使用三个其他方法来设置和帮助跟踪 Action 的属性值。它使用 configurePropertiesFromAction 方法立即更改组合框的属性。要跟踪 Action 属性值的更改,此方法应注册 createActionPropertyChangeListener 所返回的 PropertyChangeListener。当 Action 中的属性更改时,默认 PropertyChangeListener 调用 actionPropertyChanged 方法。

参数:
a - JComboBoxAction,或者 null
从以下版本开始:
1.3
另请参见:
Action, getAction(), configurePropertiesFromAction(javax.swing.Action), createActionPropertyChangeListener(javax.swing.Action), actionPropertyChanged(javax.swing.Action, java.lang.String)

getAction

public Action getAction()
返回此 ActionEvent 源当前设置的 Action,如果没有设置任何 Action,则返回 null

返回:
ActionEvent 源的 Action;或者 null
从以下版本开始:
1.3
另请参见:
Action, setAction(javax.swing.Action)

configurePropertiesFromAction

protected void configurePropertiesFromAction(Action a)
在此组合框上设置属性以匹配指定 Action 中的属性。有关设置何种属性的更多信息,请参阅支持 Action 的 Swing 组件

参数:
a - 从其获取属性的 Action,或者 null
从以下版本开始:
1.3
另请参见:
Action, setAction(javax.swing.Action)

createActionPropertyChangeListener

protected PropertyChangeListener createActionPropertyChangeListener(Action a)
创建并返回一个 PropertyChangeListener,它负责侦听指定 Action 的更改并更新适当属性。

警告:如果子类化此方法,则不要创建匿名内部类。如果执行组合框的生存期,则它将被绑定到 Action 的生存期。

参数:
a - 组合框的动作
从以下版本开始:
1.3
另请参见:
Action, setAction(javax.swing.Action)

actionPropertyChanged

protected void actionPropertyChanged(Action action,
                                     String propertyName)
更新组合框的状态以响应关联动作中的属性更改。从 createActionPropertyChangeListener 所返回的 PropertyChangeListener 中调用此方法。子类通常不需要调用此方法。支持其他 Action 属性的子类应该重写此方法和 configurePropertiesFromAction

有关此方法设置的属性列表,请参阅支持的 Action 的 Swing 组件下的表。

参数:
action - 与此组合框关联的 Action
propertyName - 更改的属性名称
从以下版本开始:
1.6
另请参见:
Action, configurePropertiesFromAction(javax.swing.Action)

fireItemStateChanged

protected void fireItemStateChanged(ItemEvent e)
通知所有需要此事件类型的通知的已注册侦听器。

参数:
e - 需要的事件
另请参见:
EventListenerList

fireActionEvent

protected void fireActionEvent()
通知所有需要此事件类型的通知的已注册侦听器。

另请参见:
EventListenerList

selectedItemChanged

protected void selectedItemChanged()
此受保护方法是特定于实现的。不要直接访问或重写。


getSelectedObjects

public Object[] getSelectedObjects()
返回包含所选项的数组。实现此方法是为了与 ItemSelectable 兼容。

指定者:
接口 ItemSelectable 中的 getSelectedObjects
返回:
包含一个元素(所选项)的 Object 数组

actionPerformed

public void actionPerformed(ActionEvent e)
此方法由于实现的副作用而存在的公共方法。不要调用或重写。

指定者:
接口 ActionListener 中的 actionPerformed

contentsChanged

public void contentsChanged(ListDataEvent e)
此方法作为实现的副作用存在的公共方法。不要调用或重写。

指定者:
接口 ListDataListener 中的 contentsChanged
参数:
e - 封装事件信息的 ListDataEvent

intervalAdded

public void intervalAdded(ListDataEvent e)
此方法作为实现的副作用存在的公共方法。不要调用或重写。

指定者:
接口 ListDataListener 中的 intervalAdded
参数:
e - 封装事件信息的 ListDataEvent

intervalRemoved

public void intervalRemoved(ListDataEvent e)
此方法作为实现的副作用存在的公共方法。不要调用或重写。

指定者:
接口 ListDataListener 中的 intervalRemoved
参数:
e - 封装事件信息的 ListDataEvent

selectWithKeyChar

public boolean selectWithKeyChar(char keyChar)
如果存在与指定键盘字符相对应的项,则选择该列表项并返回 true。否则,返回 false。

参数:
keyChar - 一个 char,它通常是用户键入的键盘键

setEnabled

public void setEnabled(boolean b)
启用组合框以便可以选择项。禁用组合框时,无法选择项并且无法在其字段中键入值(如果它是可编辑的)。

覆盖:
JComponent 中的 setEnabled
参数:
b - 一个 boolean 值,为 true 时,启用该组件,为 false 时,禁用该组件
另请参见:
Component.isEnabled(), Component.isLightweight()

configureEditor

public void configureEditor(ComboBoxEditor anEditor,
                            Object anItem)
利用指定项初始化编辑器。

参数:
anEditor - 显示组合框字段中的列表项,并可以对其进行编辑的 ComboBoxEditor
anItem - 要在字段中显示和编辑的对象

processKeyEvent

public void processKeyEvent(KeyEvent e)
处理 KeyEvent,查找 Tab 键。如果找不到 Tab 键,则关闭弹出窗口。

覆盖:
JComponent 中的 processKeyEvent
参数:
e - 包含按下的键盘键的 KeyEvent
另请参见:
KeyEvent, KeyListener, KeyboardFocusManager, DefaultKeyboardFocusManager, Component.processEvent(java.awt.AWTEvent), Component.dispatchEvent(java.awt.AWTEvent), Component.addKeyListener(java.awt.event.KeyListener), Component.enableEvents(long), Component.isShowing()

setKeySelectionManager

public void setKeySelectionManager(JComboBox.KeySelectionManager aManager)
设置将键盘字符转换为列表选择的对象。通常,第一个字符匹配的第一个选择将成为所选项。


getKeySelectionManager

public JComboBox.KeySelectionManager getKeySelectionManager()
返回列表的键选择管理器。

返回:
当前使用的 KeySelectionManager

getItemCount

public int getItemCount()
返回列表中的项数。

返回:
等于列表中项数的整数

getItemAt

public Object getItemAt(int index)
返回指定索引处的列表项。如果 index 超出范围(小于零或者大于等于列表大小),则返回 null

参数:
index - 指示列表位置的整数,其中第一项从零开始
返回:
该列表位置上的 Object;如果超出范围,则返回 null

createDefaultKeySelectionManager

protected JComboBox.KeySelectionManager createDefaultKeySelectionManager()
返回默认键选择管理器的实例。

返回:
列表当前使用的 KeySelectionManager
另请参见:
setKeySelectionManager(javax.swing.JComboBox.KeySelectionManager)

paramString

protected String paramString()
返回此 JComboBox 的字符串表示形式。此方法仅在进行调试时使用,对于各个实现,所返回字符串的内容和格式可能有所不同。返回的字符串可以为空,但不可以为 null

覆盖:
JComponent 中的 paramString
返回:
JComboBox 的字符串表示形式

getAccessibleContext

public AccessibleContext getAccessibleContext()
获取与此 JComboBox 关联的 AccessibleContext。对于组合框,AccessibleContext 采用 AccessibleJComboBox 的形式。如有必要,可创建一个新的 AccessibleJComboBox 实例。

指定者:
接口 Accessible 中的 getAccessibleContext
覆盖:
JComponent 中的 getAccessibleContext
返回:
充当此 JComboBox 的 AccessibleContext 的 AccessibleJComboBox

JavaTM 2 Platform
Standard Ed. 6

提交错误或意见

版权所有 2008 Sun Microsystems, Inc. 保留所有权利。请遵守GNU General Public License, version 2 only