Swing
Swing教學首頁
Swing介紹
Swing開發環境安裝
Swing控件
Swing Component類
Swing Container類
Swing JComponent類
Swing JLabel
Swing JButton
Swing JColorChooser
Swing JCheckBox
Swing JRadioButton
Swing JList
Swing JComboBox
Swing JTextField
Swing JTextArea
Swing ImageIcon
Swing JScrollBar
Swing JOptionPane
Swing JFileChooser
Swing JProgressBar
Swing JSlider
Swing JSpinner
Swing事件處理
SWING Event事件類
SWING AWTEvent事件類
SWING ActionEvent事件類
SWING InputEvent事件類
SWING KeyEvent事件類
SWING MouseEvent事件類
SWING WindowEvent事件類
SWING AdjustmentEvent事件處理
SWING ComponentEvent處理類
SWING ContainerEvent事件處理類
SWING MouseMotionEvent事件處理
SWING PaintEvent事件類
Swing事件監聽器
SWING ActionListener接口
SWING ComponentListener接口
Swing ItemListener接口
Swing KeyListener接口
Swing MouseListener Interface
Swing WindowListener接口
Swing AdjustmentListener接口
Swing ContainerListener接口
Swing MouseMotionListener接口
Swing FocusListener接口
Swing事件適配器
Swing FocusAdapter類
Swing KeyAdapter類
Swing MouseAdapter類
Swing WindowAdapter類
Swing MouseMotionAdapter類
Swing Layout佈局
Swing LayoutManager接口
Swing LayoutManager2接口
Swing BorderLayout佈局
Swing CardLayout佈局
Swing FlowLayout佈局類
Swing GridLayout佈局類
Swing GridBagLayout佈局類
Swing GroupLayout佈局類
Swing SpringLayout佈局類
Swing Menu菜單類
Swing JMenuBar類
Swing JMenuItem類
Swing JMenu類
Swing JCheckboxMenuItem類及例子
Swing JRadioButtonMenuItem類及例子
Swing JPopupMenu類及實例
Swing容器
Swing JPanel類及實例
Swing JFrame類和實例
Swing JWindow類及實例

Swing JComboBox

JComboBox類是一個組件,它結合了一個按鈕或可編輯字段與下拉列表。

類聲明

以下是聲明的 javax.swing.JComboBox類:

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

字段域

以下是javax.swing.JList類字段:

  • 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 -- 這個受保護的字段具體實現。

類構造函數

S.N.

構造函數 & 描述

1

JComboBox()
Creates a JComboBox with a default data model.

2

JComboBox(ComboBoxModel aModel)
Creates a JComboBox that takes its items from an existing ComboBoxModel.

3

JComboBox(Object[] items)
Creates a JComboBox that contains the elements in the specified array.

4

JComboBox(Vector<?> items)
Creates a JComboBox that contains the elements in the specified Vector.

類方法

S.N.

方法 & 描述

1

void actionPerformed(ActionEvent e)
This method is public as an implementation side effect.

2

protected void actionPropertyChanged(Action action, String propertyName)
Updates the combobox's state in response to property changes in associated action.

3

void addActionListener(ActionListener l)
Adds an ActionListener.

4

void addItem(Object anObject)
Adds an item to the item list.

5

void addItemListener(ItemListener aListener)
Adds an ItemListener.

6

void addPopupMenuListener(PopupMenuListener l)
Adds a PopupMenu listener which will listen to notification messages from the popup portion of the combo box.

7

void configureEditor(ComboBoxEditor anEditor, Object anItem)
Initializes the editor with the specified item.

8

protected void configurePropertiesFromAction(Action a)
Sets the properties on this combobox to match those in the specified Action.

9

void contentsChanged(ListDataEvent e)
This method is public as an implementation side effect.

10

protected PropertyChangeListener createActionPropertyChangeListener(Action a)
Creates and returns a PropertyChangeListener that is responsible for listening for changes from the specified Action and updating the appropriate properties.

11

protected JComboBox.KeySelectionManager createDefaultKeySelectionManager()
Returns an instance of the default key-selection manager.

12

protected void fireActionEvent()
Notifies all listeners that have registered interest for notification on this event type.

13

protected void fireItemStateChanged(ItemEvent e)
Notifies all listeners that have registered interest for notification on this event type.

14

void firePopupMenuCanceled()
Notifies PopupMenuListeners that the popup portion of the combo box has been canceled.

15

void firePopupMenuWillBecomeInvisible()
Notifies PopupMenuListeners that the popup portion of the combo box has become invisible.

16

void firePopupMenuWillBecomeVisible()
Notifies PopupMenuListeners that the popup portion of the combo box will become visible.

17

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JComboBox.

18

Action getAction()
Returns the currently set Action for this ActionEvent source, or null if no Action is set.

19

String getActionCommand()
Returns the action command that is included in the event sent to action listeners.

20

ActionListener[] getActionListeners()
Returns an array of all the ActionListeners added to this JComboBox with addActionListener().

21

ComboBoxEditor getEditor()
Returns the editor used to paint and edit the selected item in the JComboBox field.

22

Object getItemAt(int index)
Returns the list item at the specified index.

23

int getItemCount()
Returns the number of items in the list.

24

ItemListener[] getItemListeners()
Returns an array of all the ItemListeners added to this JComboBox with addItemListener().

25

JComboBox.KeySelectionManager getKeySelectionManager()
Returns the list's key-selection manager.

26

int getMaximumRowCount()
Returns the maximum number of items the combo box can display without a scrollbar.

27

ComboBoxModel getModel()
Returns the data model currently used by the JComboBox.

28

PopupMenuListener[] getPopupMenuListeners()
Returns an array of all the PopupMenuListeners added to this JComboBox with addPopupMenuListener().

29

Object getPrototypeDisplayValue()
Returns the "prototypical display" value - an Object used for the calculation of the display height and width.

30

ListCellRenderer getRenderer()
Returns the renderer used to display the selected item in the JComboBox field.

31

int getSelectedIndex()
Returns the first item in the list that matches the given item.

32

Object getSelectedItem()
Returns the current selected item.

33

Object[] getSelectedObjects()
Returns an array containing the selected item.

34

ComboBoxUI getUI()
Returns the L&F object that renders this component.

35

String getUIClassID()
Returns the name of the L&F class that renders this component.

36

void hidePopup()
Causes the combo box to close its popup window.

37

void insertItemAt(Object anObject, int index)
Inserts an item into the item list at a given index.

38

protected void installAncestorListener()

39

void intervalAdded(ListDataEvent e)
This method is public as an implementation side effect.

40

void intervalRemoved(ListDataEvent e)
This method is public as an implementation side effect.

41

boolean isEditable()
Returns true if the JComboBox is editable.

42

boolean isLightWeightPopupEnabled()
Gets the value of the lightWeightPopupEnabled property.

43

boolean isPopupVisible()
Determines the visibility of the popup.

44

protected String paramString()
Returns a string representation of this JComboBox.

45

void processKeyEvent(KeyEvent e)
Handles KeyEvents, looking for the Tab key.

46

void removeActionListener(ActionListener l)
Removes an ActionListener.

47

void removeAllItems()
Removes all items from the item list.

48

void removeItem(Object anObject)
Removes an item from the item list.

49

void removeItemAt(int anIndex)
Removes the item at anIndex This method works only if the JComboBox uses a mutable data model.

50

void removeItemListener(ItemListener aListener)
Removes an ItemListener.

51

void removePopupMenuListener(PopupMenuListener l)
Removes a PopupMenuListener.

52

protected void selectedItemChanged()
This protected method is implementation specific.

53

boolean selectWithKeyChar(char keyChar)
Selects the list item that corresponds to the specified keyboard character and returns true, if there is an item corresponding to that character.

54

void setAction(Action a)
Sets the Action for the ActionEvent source.

55

void setActionCommand(String aCommand)
Sets the action command that should be included in the event sent to action listeners.

56

void setEditable(boolean aFlag)
Determines whether the JComboBox field is editable.

57

void setEditor(ComboBoxEditor anEditor)
Sets the editor used to paint and edit the selected item in the JComboBox field.

58

void setEnabled(boolean b)
Enables the combo box so that items can be selected.

59

void setKeySelectionManager(JComboBox.KeySelectionManager aManager)
Sets the object that translates a keyboard character into a list selection.

60

void setLightWeightPopupEnabled(boolean aFlag)
Sets the lightWeightPopupEnabled property, which provides a hint as to whether or not a lightweight Component should be used to contain the JComboBox, versus a heavyweight Component such as a Panel or a Window.

61

void setMaximumRowCount(int count)
Sets the maximum number of rows the JComboBox displays.

62

void setModel(ComboBoxModel aModel)
Sets the data model that the JComboBox uses to obtain the list of items.

63

void setPopupVisible(boolean v)
Sets the visibility of the popup.

64

void setPrototypeDisplayValue(Object prototypeDisplayValue)
Sets the prototype display value used to calculate the size of the display for the UI portion.

65

void setRenderer(ListCellRenderer aRenderer)
Sets the renderer that paints the list items and the item selected from the list in the JComboBox field.

66

void setSelectedIndex(int anIndex)
Selects the item at index anIndex.

67

void setSelectedItem(Object anObject)
Sets the selected item in the combo box display area to the object in the argument.

68

void setUI(ComboBoxUI ui)
Sets the L&F object that renders this component.

69

void showPopup()
Causes the combo box to display its popup window.

70

void updateUI()
Resets the UI property to a value from the current look and feel.

方法繼承

這個類從以下類繼承的方法:

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JComboBox 例子

選擇使用任何編輯器創建以下java程序在 D:/ > SWING > com > yiibai > gui >

SwingControlDemo.java

package com.yiibai.gui; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwingControlDemo { private JFrame mainFrame; private JLabel headerLabel; private JLabel statusLabel; private JPanel controlPanel; public SwingControlDemo(){ prepareGUI(); } public static void main(String[] args){ SwingControlDemo swingControlDemo = new SwingControlDemo(); swingControlDemo.showComboboxDemo(); } private void prepareGUI(){ mainFrame = new JFrame("Java Swing Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new JLabel("", JLabel.CENTER); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showComboboxDemo(){ headerLabel.setText("Control in action: JComboBox"); final DefaultComboBoxModel fruitsName = new DefaultComboBoxModel(); fruitsName.addElement("Apple"); fruitsName.addElement("Grapes"); fruitsName.addElement("Mango"); fruitsName.addElement("Peer"); final JComboBox fruitCombo = new JComboBox(fruitsName); fruitCombo.setSelectedIndex(0); JScrollPane fruitListScrollPane = new JScrollPane(fruitCombo); JButton showButton = new JButton("Show"); showButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = ""; if (fruitCombo.getSelectedIndex() != -1) { data = "Fruits Selected: " + fruitCombo.getItemAt (fruitCombo.getSelectedIndex()); } statusLabel.setText(data); } }); controlPanel.add(fruitListScrollPane); controlPanel.add(showButton); mainFrame.setVisible(true); } }

編譯程序,使用命令提示符。到 D:/ > SWING 然後輸出以下命令。

D:SWING>javac comyiibaiguiSwingControlDemo.java

如果沒有錯誤出現,這意味着編譯成功。使用下面的命令來運行程序。

D:SWING>java com.yiibai.gui.SwingControlDemo

驗證下面的輸出

Swing