- java.lang.Object
-
- java.awt.Component
-
- java.awt.Container
-
- javax.swing.JComponent
-
- javax.swing.JComboBox<E>
-
- 参数类型
-
E- 此组合框的元素的类型
- All Implemented Interfaces:
-
ActionListener,ImageObserver,ItemSelectable,MenuContainer,Serializable,EventListener,Accessible,ListDataListener
@JavaBean(defaultProperty="UI", description="A combination of a text field and a drop-down list.") public class JComboBox<E> extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible
组合按钮或可编辑字段和下拉列表的组件。 用户可以根据用户的请求从下拉列表中选择一个值。 如果使组合框可编辑,则组合框包含用户可以键入值的可编辑字段。警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy 。
警告:此类的序列化对象与将来的Swing版本不兼容。 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 从1.4开始,对于所有JavaBeans的长期存储的支持已被添加到
java.beans包中。 请参阅XMLEncoder。有关详细信息,请参阅The Java Tutorial中的How to Use Combo Boxes 。
- 从以下版本开始:
- 1.2
- 另请参见:
-
ComboBoxModel,DefaultComboBoxModel, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class 描述 protected classJComboBox.AccessibleJComboBox该类实现JComboBox类的可访问性支持。static interfaceJComboBox.KeySelectionManager界面定义了一个KeySelectionManager。-
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
-
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
-
-
Field Summary
Fields Modifier and Type Field 描述 protected StringactionCommand此保护字段是实现特定的。protected ComboBoxModel<E>dataModel此保护字段是实现特定的。protected ComboBoxEditoreditor此保护字段是实现特定的。protected booleanisEditable此保护字段是实现特定的。protected JComboBox.KeySelectionManagerkeySelectionManager此保护字段是实现特定的。protected booleanlightWeightPopupEnabled此保护字段是实现特定的。protected intmaximumRowCount此保护字段是实现特定的。protected ListCellRenderer<? super E>renderer此保护字段是实现特定的。protected ObjectselectedItemReminder此保护字段是实现特定的。-
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
-
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
-
-
方法摘要
所有方法 接口方法 具体的方法 Modifier and Type 方法 描述 voidactionPerformed(ActionEvent e)这种方法是公共的,作为实现的副作用。protected voidactionPropertyChanged(Action action, String propertyName)响应关联操作中的属性更改更新组合框的状态。voidaddActionListener(ActionListener l)添加一个ActionListener。voidaddItem(E item)将项目添加到项目列表。voidaddItemListener(ItemListener aListener)添加一个ItemListener。voidaddPopupMenuListener(PopupMenuListener l)添加一个PopupMenu监听器,它将从组合框的弹出窗口中收听通知消息。voidconfigureEditor(ComboBoxEditor anEditor, Object anItem)用指定的项目初始化编辑器。protected voidconfigurePropertiesFromAction(Action a)设置此组合框上的属性,以匹配指定的Action中的Action。voidcontentsChanged(ListDataEvent e)这种方法是公共的,作为实现的副作用。protected PropertyChangeListenercreateActionPropertyChangeListener(Action a)创建并返回一个PropertyChangeListener,负责监听从指定的Action进行的更改并更新相应的属性。protected JComboBox.KeySelectionManagercreateDefaultKeySelectionManager()返回默认密钥选择管理器的一个实例。protected voidfireActionEvent()通知所有在此事件类型上通知有兴趣的听众。protected voidfireItemStateChanged(ItemEvent e)通知所有在此事件类型上通知有兴趣的听众。voidfirePopupMenuCanceled()通知PopupMenuListeners组合框的弹出部分已被取消。voidfirePopupMenuWillBecomeInvisible()通知PopupMenuListeners组合框的弹出部分已经变得不可见。voidfirePopupMenuWillBecomeVisible()通知PopupMenuListeners组合框的弹出部分将变得可见。AccessibleContextgetAccessibleContext()获取与此JComboBox关联的AccessibleContext。ActiongetAction()返回当前设置Action这个ActionEvent源,或者null如果没有Action设置。StringgetActionCommand()返回发送到动作侦听器的事件中包含的动作命令。ActionListener[]getActionListeners()返回使用addActionListener()添加到此JComboBox的所有ActionListener的数组。ComboBoxEditorgetEditor()返回用于在JComboBox字段中绘制和编辑所选项目的编辑器。EgetItemAt(int index)返回指定索引处的列表项。intgetItemCount()返回列表中的项目数。ItemListener[]getItemListeners()返回使用addItemListener()添加到此JComboBox的所有ItemListener的数组。JComboBox.KeySelectionManagergetKeySelectionManager()返回列表的密钥选择管理器。intgetMaximumRowCount()返回在没有滚动条的情况下组合框可以显示的最大项数ComboBoxModel<E>getModel()返回JComboBox当前使用的数据模型。PopupMenuListener[]getPopupMenuListeners()返回使用addPopupMenuListener()添加到此JComboBox的所有PopupMenuListener的数组。EgetPrototypeDisplayValue()返回“原型显示”值 - 用于计算显示高度和宽度的对象。ListCellRenderer<? super E>getRenderer()返回用于在JComboBox字段中显示所选项目的渲染器。intgetSelectedIndex()返回列表中与给定项目匹配的第一个项目。ObjectgetSelectedItem()返回当前所选项目。Object[]getSelectedObjects()返回一个包含所选项目的数组。ComboBoxUIgetUI()返回渲染此组件的L&F对象。StringgetUIClassID()返回呈现此组件的L&F类的名称。voidhidePopup()导致组合框关闭其弹出窗口。voidinsertItemAt(E item, int index)在给定索引的项目列表中插入项目。protected voidinstallAncestorListener()注册祖先监听器,以便它或它的任何祖先移动或使其可见或不可见时,它将接收AncestorEvents。voidintervalAdded(ListDataEvent e)这种方法是公共的,作为实现的副作用。voidintervalRemoved(ListDataEvent e)这种方法是公共的,作为实现的副作用。booleanisEditable()如果JComboBox可编辑,则返回true。booleanisLightWeightPopupEnabled()获取lightWeightPopupEnabled属性的值。booleanisPopupVisible()确定弹出窗口的可见性。protected StringparamString()返回此JComboBox的字符串表示JComboBox。protected booleanprocessKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)调用以处理键绑定为ks作为的结果KeyEvente。voidprocessKeyEvent(KeyEvent e)处理KeyEvents,寻找Tab键。voidremoveActionListener(ActionListener l)删除一个ActionListener。voidremoveAllItems()从项目列表中删除所有项目。voidremoveItem(Object anObject)从项目列表中删除一个项目。voidremoveItemAt(int anIndex)删除该项目anIndex此方法仅在JComboBox使用可变数据模型时有效。voidremoveItemListener(ItemListener aListener)删除一个ItemListener。voidremovePopupMenuListener(PopupMenuListener l)删除一个PopupMenuListener。protected voidselectedItemChanged()此保护方法是具体实现。booleanselectWithKeyChar(char keyChar)选择与指定键盘字符相对应的列表项,如果存在与该字符相对应的项,则返回true。voidsetAction(Action a)设置Action为ActionEvent源。voidsetActionCommand(String aCommand)设置应该包含在发送到action监听器的事件中的action命令。voidsetEditable(boolean aFlag)确定JComboBox字段是否可编辑。voidsetEditor(ComboBoxEditor anEditor)设置用于在JComboBox字段中绘制和编辑所选项目的编辑器。voidsetEnabled(boolean b)启用组合框,以便可以选择项目。voidsetKeySelectionManager(JComboBox.KeySelectionManager aManager)设置将键盘字符转换为列表选择的对象。voidsetLightWeightPopupEnabled(boolean aFlag)设置lightWeightPopupEnabled属性,其中提供了一个提示是否应使用轻量级Component来包含JComboBox,而重量级Component(如Panel或Window。voidsetMaximumRowCount(int count)设置JComboBox显示的最大行数。voidsetModel(ComboBoxModel<E> aModel)设置JComboBox用于获取项目列表的数据模型。voidsetPopupVisible(boolean v)设置弹出窗口的可见性。voidsetPrototypeDisplayValue(E prototypeDisplayValue)设置用于计算UI部分显示大小的原型显示值。voidsetRenderer(ListCellRenderer<? super E> aRenderer)设置绘制列表项目的渲染器和从JComboBox字段中的列表中选择的项目。voidsetSelectedIndex(int anIndex)选择索引号为anIndex的项目。voidsetSelectedItem(Object anObject)将组合框显示区域中的选定项目设置为参数中的对象。voidsetUI(ComboBoxUI ui)设置渲染此组件的L&F对象。voidshowPopup()导致组合框显示其弹出窗口。voidupdateUI()将UI属性重置为当前外观的值。-
Methods inherited from class 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, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Methods inherited from class 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, transferFocusDownCycle, validate, validateTree
-
Methods inherited from class 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, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, 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
-
-
-
-
字段详细信息
-
dataModel
protected ComboBoxModel<E> dataModel
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
renderer
protected ListCellRenderer<? super E> renderer
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
editor
protected ComboBoxEditor editor
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
maximumRowCount
protected int maximumRowCount
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
isEditable
protected boolean isEditable
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。- 另请参见:
-
isEditable,setEditable(boolean)
-
keySelectionManager
protected JComboBox.KeySelectionManager keySelectionManager
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
actionCommand
protected String actionCommand
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
lightWeightPopupEnabled
protected boolean lightWeightPopupEnabled
此保护字段是实现特定的。 不要直接访问或覆盖。 改用accessor方法。
-
selectedItemReminder
protected Object selectedItemReminder
此保护字段是实现特定的。 不要直接访问或覆盖。
-
-
构造方法详细信息
-
JComboBox
public JComboBox(ComboBoxModel<E> aModel)
创建一个JComboBox,从现有的ComboBoxModel获取其项目。 由于提供了ComboBoxModel,因此使用此构造函数创建的组合框不会创建默认组合框模型,并可能影响插入,删除和添加方法的行为。- 参数
-
aModel- 提供显示的项目列表的ComboBoxModel - 另请参见:
-
DefaultComboBoxModel
-
JComboBox
public JComboBox(E[] items)
创建一个JComboBox,其中包含指定数组中的元素。 默认情况下,数组中的第一个项目(因此数据模型)将被选中。- 参数
-
items- 要插入到组合框中的一组对象 - 另请参见:
-
DefaultComboBoxModel
-
JComboBox
public JComboBox(Vector<E> items)
创建一个JComboBox,其中包含指定向量中的元素。 默认情况下,向量中的第一个项目(因此数据模型)将被选中。- 参数
-
items- 插入到组合框中的向量数组 - 另请参见:
-
DefaultComboBoxModel
-
JComboBox
public JComboBox()
使用默认数据模型创建一个JComboBox。 默认数据模型是一个空的对象列表。 使用addItem添加项目。 默认情况下,数据模型中的第一个项目被选中。- 另请参见:
-
DefaultComboBoxModel
-
-
方法详细信息
-
installAncestorListener
protected void installAncestorListener()
注册祖先监听器,以便它或它的任何祖先移动或使其可见或不可见时将接收AncestorEvents。 当组件或其祖先从包含层次结构中添加或删除时,也会发送事件。
-
setUI
@BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(ComboBoxUI ui)
设置渲染此组件的L&F对象。- 参数
-
ui-ComboBoxUIL&F对象 - 另请参见:
-
UIDefaults.getUI(javax.swing.JComponent)
-
updateUI
public void updateUI()
将UI属性重置为当前外观的值。- 重写:
-
updateUI在JComponent - 另请参见:
-
JComponent.updateUI()
-
getUIClassID
@BeanProperty(bound=false) public String getUIClassID()
返回呈现此组件的L&F类的名称。- 重写:
-
getUIClassID在JComponent - 结果
- 字符串“ComboBoxUI”
- 另请参见:
-
JComponent.getUIClassID(),UIDefaults.getUI(javax.swing.JComponent)
-
getUI
public ComboBoxUI getUI()
返回渲染此组件的L&F对象。- 重写:
-
getUI在JComponent - 结果
- 呈现此组件的ComboBoxUI对象
-
setModel
@BeanProperty(description="Model that the combo box uses to get data to display.") public void setModel(ComboBoxModel<E> aModel)
设置JComboBox用于获取项目列表的数据模型。- 参数
-
aModel- 提供显示的项目列表的ComboBoxModel
-
getModel
public ComboBoxModel<E> getModel()
返回JComboBox当前使用的数据模型。- 结果
-
提供显示的项目列表的
ComboBoxModel
-
setLightWeightPopupEnabled
@BeanProperty(expert=true, description="Set to <code>false</code> to require heavyweight popups.") public void setLightWeightPopupEnabled(boolean aFlag)
设置lightWeightPopupEnabled属性,其中提供了一个提示是否应使用轻量级Component来包含JComboBox,而重量级Component(如Panel或Window。 轻量级与重量级的决定最终达到JComboBox。 轻量级的窗口比重型窗口更有效率,但轻量级和重型组件在GUI中不能很好的混合。 如果您的应用程序混合轻量级和重量级组件,则应禁用轻量级弹出窗口。lightWeightPopupEnabled属性的默认值为true,除非外观和外观另有规定。 一些看起来和感觉总是使用重量级的弹出窗口,无论这个属性的价值。请参阅文章Mixing Heavy and Light Components此方法触发属性更改事件。
- 参数
-
aFlag- 如果是true,则需要轻量级的弹出窗口
-
isLightWeightPopupEnabled
public boolean isLightWeightPopupEnabled()
获取lightWeightPopupEnabled属性的值。- 结果
-
该值为
lightWeightPopupEnabled属性 - 另请参见:
-
setLightWeightPopupEnabled(boolean)
-
setEditable
@BeanProperty(preferred=true, description="If true, the user can type a new value in the combo box.") public void setEditable(boolean aFlag)
确定JComboBox字段是否可编辑。 可编辑JComboBox允许用户键入字段或从列表中选择一个项目以初始化该字段,之后可以对其进行编辑。 (编辑仅影响字段,列表项目保持不变。)不可编辑JComboBox在该字段中显示所选项目,但不能修改该选项。- 参数
-
aFlag- 一个布尔值,其中true表示该字段是可编辑的
-
isEditable
public boolean isEditable()
如果JComboBox可编辑,则返回true。 默认情况下,组合框不可编辑。- 结果
-
如果
JComboBox是可编辑的,JComboBoxtrue,否则为false
-
setMaximumRowCount
@BeanProperty(preferred=true, description="The maximum number of rows the popup should have") public void setMaximumRowCount(int count)
设置JComboBox显示的最大行数。 如果模型中的对象数量大于count,组合框将使用滚动条。- 参数
-
count- 指定在使用滚动条之前在列表中显示的最大项目数的整数
-
getMaximumRowCount
public int getMaximumRowCount()
返回在没有滚动条的情况下组合框可以显示的最大项数- 结果
- 一个整数,指定在使用滚动条之前在列表中显示的最大项目数
-
setRenderer
@BeanProperty(expert=true, description="The renderer that paints the item selected in the list.") public void setRenderer(ListCellRenderer<? super E> aRenderer)
设置绘制列表项目的渲染器和从JComboBox字段中的列表中选择的项目。 如果JComboBox不可编辑,则使用渲染器。 如果可编辑,编辑器用于渲染和编辑所选项目。默认渲染器显示字符串或图标。 其他渲染器可以处理图形图像和复合项目。
要显示所选项目,
aRenderer.getListCellRendererComponent,传递列表对象和索引-1。- 参数
-
aRenderer- 显示所选项目的ListCellRenderer - 另请参见:
-
setEditor(javax.swing.ComboBoxEditor)
-
getRenderer
public ListCellRenderer<? super E> getRenderer()
返回在JComboBox字段中用于显示所选项目的渲染器。- 结果
-
显示所选项目的
ListCellRenderer。
-
setEditor
@BeanProperty(expert=true, description="The editor that combo box uses to edit the current value") public void setEditor(ComboBoxEditor anEditor)
设置用于在JComboBox字段中绘制和编辑所选项目的编辑器。 编辑器仅在接收JComboBox可编辑时使用。 如果不可编辑,组合框将使用渲染器来绘制所选项。- 参数
-
anEditor- 显示所选项目的ComboBoxEditor - 另请参见:
-
setRenderer(javax.swing.ListCellRenderer<? super E>)
-
getEditor
public ComboBoxEditor getEditor()
返回编辑器,用于在JComboBox字段中绘制和编辑所选项目。- 结果
-
显示所选项目的
ComboBoxEditor
-
setSelectedItem
@BeanProperty(bound=false, preferred=true, description="Sets the selected item in the JComboBox.") public void setSelectedItem(Object anObject)
将组合框显示区域中的选定项目设置为参数中的对象。 如果anObject是在列表中,显示区显示anObject选择。如果
anObject不在列表中且组合框不可编辑,则不会更改当前选择。 对于可编辑的组合框,选择将更改为anObject。如果这是所选项目的变化,则添加到组合框中的
ItemListener将通知一个或两个ItemEvents。 如果当前选择的项目有ItemEvent,则状态更改将为ItemEvent.DESELECTED。 如果anObject在列表中,当前未被选中,则ItemEvent将被触发,状态更改将为ItemEvent.SELECTED。ActionListener增补到组合框将与被通知ActionEvent当这个方法被调用。- 参数
-
anObject- 要选择的列表对象; 使用null清除选择
-
getSelectedItem
public Object getSelectedItem()
返回当前所选项目。如果组合框可编辑,则该值可能没有被添加到组合框
addItem,insertItemAt或数据构造。- 结果
- 当前选中的对象
- 另请参见:
-
setSelectedItem(java.lang.Object)
-
setSelectedIndex
@BeanProperty(bound=false, preferred=true, description="The item at index is selected.") public void setSelectedIndex(int anIndex)
选择索引号为anIndex的项目。- 参数
-
anIndex- 指定要选择的列表项的整数,其中0指定列表中的第一个项,-1表示不选择 - 异常
-
IllegalArgumentException- 如果anIndex<-1或anIndex大于或等于大小
-
getSelectedIndex
public int getSelectedIndex()
返回列表中与给定项目匹配的第一个项目。 如果JComboBox允许不在列表中的选定项目,结果并不总是被定义。 如果没有选定项目,或者用户指定了不在列表中的项目,则返回-1。- 结果
- 一个指定当前选择的列表项的整数,其中0指定列表中的第一个项; 或者如果没有选择项目,或者当前选择的项目不在列表中,则为-1
-
getPrototypeDisplayValue
public E getPrototypeDisplayValue()
返回“原型显示”值 - 用于计算显示高度和宽度的对象。- 结果
-
该值为
prototypeDisplayValue属性 - 从以下版本开始:
- 1.4
- 另请参见:
-
setPrototypeDisplayValue(E)
-
setPrototypeDisplayValue
@BeanProperty(visualUpdate=true, description="The display prototype value, used to compute display width and height.") public void setPrototypeDisplayValue(E prototypeDisplayValue)
设置用于计算UI部分显示大小的原型显示值。如果指定了原型显示值,则通过使用原型显示值配置渲染器并获得其首选大小来计算组合框的首选大小。 当组合框显示大量数据时,指定首选显示值通常很有用。 如果没有指定原型显示值,则必须为模型中的每个值配置渲染器和获取的首选尺寸,这可能相对昂贵。
- 参数
-
prototypeDisplayValue- 原型显示值 - 从以下版本开始:
- 1.4
- 另请参见:
-
getPrototypeDisplayValue()
-
addItem
public void addItem(E item)
将项目添加到项目列表。 此方法仅在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; } }; }- 参数
-
item- 要添加到列表中的项目 - 另请参见:
-
MutableComboBoxModel
-
insertItemAt
public void insertItemAt(E item, int index)
在给定索引的项目列表中插入项目。 此方法仅在JComboBox使用可变数据模型时有效。- 参数
-
item- 要添加到列表的项目 -
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)
设置弹出窗口的可见性。- 参数
-
v- 如果true显示弹出窗口,否则隐藏弹出窗口。
-
isPopupVisible
public boolean isPopupVisible()
确定弹出窗口的可见性。- 结果
- 如果弹出窗口可见,则返回true,否则返回false
-
addItemListener
public void addItemListener(ItemListener aListener)
添加一个ItemListener。aListener将在所选项目更改时收到一个或两个ItemEvent。- Specified by:
-
addItemListener在接口ItemSelectable - 参数
-
aListener- 要通知的ItemListener - 另请参见:
-
setSelectedItem(java.lang.Object)
-
removeItemListener
public void removeItemListener(ItemListener aListener)
删除一个ItemListener。- Specified by:
-
removeItemListener在接口ItemSelectable - 参数
-
aListener- 要删除的ItemListener - 另请参见:
-
ItemEvent
-
getItemListeners
@BeanProperty(bound=false) 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
@BeanProperty(bound=false) public ActionListener[] getActionListeners()
返回使用addActionListener()添加到此JComboBox的所有ActionListener的数组。- 结果
-
如果没有添加任何监听器,则添加所有
ActionListeners或一个空数组 - 从以下版本开始:
- 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
@BeanProperty(bound=false) public PopupMenuListener[] getPopupMenuListeners()
返回使用addPopupMenuListener()添加到此JComboBox的所有PopupMenuListener的数组。- 结果
-
如果没有添加任何监听器,则添加所有
PopupMenuListeners或一个空数组 - 从以下版本开始:
- 1.4
-
firePopupMenuWillBecomeVisible
public void firePopupMenuWillBecomeVisible()
通知PopupMenuListeners组合框的弹出部分将变得可见。这个方法是公开的,但不应该被UI委托以外的任何东西调用。
- 从以下版本开始:
- 1.4
- 另请参见:
-
addPopupMenuListener(javax.swing.event.PopupMenuListener)
-
firePopupMenuWillBecomeInvisible
public void firePopupMenuWillBecomeInvisible()
通知PopupMenuListeners组合框的弹出部分已经变得不可见。这个方法是公开的,但不应该被UI委托以外的任何东西调用。
- 从以下版本开始:
- 1.4
- 另请参见:
-
addPopupMenuListener(javax.swing.event.PopupMenuListener)
-
firePopupMenuCanceled
public void firePopupMenuCanceled()
通知PopupMenuListeners组合框的弹出部分已被取消。这个方法是公开的,但不应该被UI委托以外的任何东西调用。
- 从以下版本开始:
- 1.4
- 另请参见:
-
addPopupMenuListener(javax.swing.event.PopupMenuListener)
-
setActionCommand
public void setActionCommand(String aCommand)
设置应该包含在发送到action监听器的事件中的action命令。- 参数
-
aCommand- 包含发送到动作侦听器的“命令”的字符串; 同样的听众可以根据接收到的命令来做不同的事情
-
getActionCommand
public String getActionCommand()
返回发送到动作侦听器的事件中包含的动作命令。- 结果
- 该字符串包含发送到动作侦听器的“命令”。
-
setAction
@BeanProperty(visualUpdate=true, description="the Action instance connected with this ActionEvent source") public void setAction(Action a)
设置Action为ActionEvent源。 新的Action替换以前设置的任何Action但不影响ActionListeners独立添加addActionListener。 如果Action已经是ActionListener的ActionEvent的注册ActionListener,则不会重新注册。设置
Action结果立即改变中描述的所有属性Swing Components SupportingAction。 随后,随着Action的属性更改,组合框的属性将自动更新。该方法使用三种其他方法来设置和帮助跟踪
Action的属性值。 它使用configurePropertiesFromAction方法立即更改组合框的属性。 要跟踪Action的属性值中的更改,此方法注册PropertyChangeListener返回的createActionPropertyChangeListener。 默认PropertyChangeListener调用actionPropertyChanged方法时的属性Action变化。- 参数
-
a-Action为JComboBox,或null。 - 从以下版本开始:
- 1.3
- 另请参见:
-
Action,getAction(),configurePropertiesFromAction(javax.swing.Action),createActionPropertyChangeListener(javax.swing.Action),actionPropertyChanged(javax.swing.Action, java.lang.String)
-
getAction
public Action getAction()
返回当前设置Action这个ActionEvent源,或者null如果没有Action设置。- 结果
-
Action为这个ActionEvent来源; 或null - 从以下版本开始:
- 1.3
- 另请参见:
-
Action,setAction(javax.swing.Action)
-
configurePropertiesFromAction
protected void configurePropertiesFromAction(Action a)
设置此组合框上的属性,以匹配指定的Action中的Action。 有关此设置的属性的更多详细信息,请参阅Swing Components SupportingAction。- 参数
-
a-Action获取属性的null,或null - 从以下版本开始:
- 1.3
- 另请参见:
-
Action,setAction(javax.swing.Action)
-
createActionPropertyChangeListener
protected PropertyChangeListener createActionPropertyChangeListener(Action a)
创建并返回一个PropertyChangeListener,负责监听指定的Action更改并更新相应的属性。警告:如果你这个子类不创建一个匿名的内部类。 如果你一生的组合框将被绑定到
Action。- 参数
-
a- 组合框的动作 - 结果
-
PropertyChangeListener - 从以下版本开始:
- 1.3
- 另请参见:
-
Action,setAction(javax.swing.Action)
-
actionPropertyChanged
protected void actionPropertyChanged(Action action, String propertyName)
响应关联操作中的属性更改更新组合框的状态。 此方法从PropertyChangeListener返回的createActionPropertyChangeListener调用。 子类通常不需要调用它。 支持额外的Action属性的子类应该覆盖此和configurePropertiesFromAction。有关此方法设置的属性的列表,请参阅Swing Components Supporting
Action表。- 参数
-
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
@BeanProperty(bound=false) public Object[] getSelectedObjects()
返回一个包含所选项目的数组。 该方法是为兼容ItemSelectable而实现的。- Specified by:
-
getSelectedObjects在接口ItemSelectable - 结果
-
一个包含一个元素的
Objects数组 - 所选项
-
actionPerformed
public void actionPerformed(ActionEvent e)
这种方法是公共的,作为实现的副作用。 不要打电话或覆盖。- Specified by:
-
actionPerformed在接口ActionListener - 参数
-
e- 要处理的事件
-
contentsChanged
public void contentsChanged(ListDataEvent e)
这种方法是公共的,作为实现的副作用。 不要打电话或覆盖。- Specified by:
-
contentsChanged在接口ListDataListener - 参数
-
e-ListDataEvent封装事件信息
-
intervalAdded
public void intervalAdded(ListDataEvent e)
这种方法是公共的,作为实现的副作用。 不要打电话或覆盖。- Specified by:
-
intervalAdded在接口ListDataListener - 参数
-
e-ListDataEvent封装事件信息
-
intervalRemoved
public void intervalRemoved(ListDataEvent e)
这种方法是公共的,作为实现的副作用。 不要打电话或覆盖。- Specified by:
-
intervalRemoved在接口ListDataListener - 参数
-
e-ListDataEvent封装事件信息
-
selectWithKeyChar
public boolean selectWithKeyChar(char keyChar)
选择与指定键盘字符相对应的列表项,如果存在与该字符相对应的项,则返回true。 否则返回false。- 参数
-
keyChar- 一个char,通常这是用户键入的键盘键 - 结果
-
如果存在与该字符相对应的项目,
true。 否则返回false。
-
setEnabled
@BeanProperty(preferred=true, description="The enabled state of the component.") public void setEnabled(boolean b)
启用组合框,以便可以选择项目。 当禁用组合框时,无法选择项目,无法在其字段中输入值(如果可编辑)。- 重写:
-
setEnabled中的JComponent - 参数
-
b- 一个布尔值,其中true使组件和false禁用它 - 另请参见:
-
Component.isEnabled(),Component.isLightweight()
-
configureEditor
public void configureEditor(ComboBoxEditor anEditor, Object anItem)
用指定的项目初始化编辑器。- 参数
-
anEditor- 在组合框字段中显示列表项的ComboBoxEditor,并允许编辑它 -
anItem- 要在字段中显示和编辑的对象
-
processKeyEvent
public void processKeyEvent(KeyEvent e)
处理KeyEvents,寻找Tab键。 如果找到Tab键,弹出窗口关闭。- 重写:
-
processKeyEvent在JComponent - 参数
-
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()
-
processKeyBinding
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
调用以处理键绑定为ks作为的结果KeyEvente。 获得适当的InputMap,获取绑定,从ActionMap获取操作,然后(如果发现操作并且组件已启用)将调用notifyAction通知操作。- 重写:
-
processKeyBinding在JComponent - 参数
-
ks-KeyStroke查询 -
e-KeyEvent -
condition- 以下值之一:- JComponent.WHEN_FOCUSED
- JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
- JComponent.WHEN_IN_FOCUSED_WINDOW
-
pressed- 如果按下键,则为true - 结果
- 如果对操作有约束力,并且该操作已启用,则为true
-
setKeySelectionManager
@BeanProperty(bound=false, expert=true, description="The objects that changes the selection when a key is pressed.") public void setKeySelectionManager(JComboBox.KeySelectionManager aManager)
设置将键盘字符转换为列表选择的对象。 通常,具有匹配的第一个字符的第一个选择将成为所选项。- 参数
-
aManager- 一个关键选择经理
-
getKeySelectionManager
public JComboBox.KeySelectionManager getKeySelectionManager()
返回列表的密钥选择管理器。- 结果
-
目前正在使用的
KeySelectionManager
-
getItemCount
@BeanProperty(bound=false) public int getItemCount()
返回列表中的项目数。- 结果
- 一个等于列表中项目数的整数
-
getItemAt
public E getItemAt(int index)
返回指定索引处的列表项。 如果index超出范围(小于零或大于或等于大小),它将返回null。- 参数
-
index- 表示列表位置的整数,第一个项目从零开始 - 结果
-
该列表位置的项目;
如果超出范围,
null
-
createDefaultKeySelectionManager
protected JComboBox.KeySelectionManager createDefaultKeySelectionManager()
返回默认密钥选择管理器的一个实例。- 结果
-
目前使用的
KeySelectionManager列表 - 另请参见:
-
setKeySelectionManager(javax.swing.JComboBox.KeySelectionManager)
-
paramString
protected String paramString()
返回此JComboBox的字符串表示JComboBox。 该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。 返回的字符串可能为空,但可能不是null。- 重写:
-
paramString在JComponent - 结果
-
这个
JComboBox的字符串表示JComboBox
-
getAccessibleContext
@BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
获取与此JComboBox关联的AccessibleContext。 对于组合框,AccessibleContext采用AccessibleJComboBox的形式。 如有必要,将创建一个新的AccessibleJComboBox实例。- Specified by:
-
getAccessibleContext在接口Accessible - 重写:
-
getAccessibleContext在Component - 结果
- an AccessibleJComboBox that serves as the AccessibleContext of this JComboBox
-
-