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 - 此受保護欄位是實現特定的。

類建構函式

序號 建構函式 & 描述
1

JComboBox()

建立一個具有預設資料模型的 JComboBox。

2

JComboBox(ComboBoxModel aModel)

建立一個 JComboBox,其專案來自現有的 ComboBoxModel。

3

JComboBox(Object[] items)

建立一個包含指定陣列中元素的 JComboBox。

4

JComboBox(Vector<?> items)

建立一個包含指定 Vector 中元素的 JComboBox。

類方法

序號 方法 & 描述
1

void actionPerformed(ActionEvent e)

此方法作為實現副作用是公共的。

2

protected void actionPropertyChanged(Action action, String propertyName)

根據關聯操作中的屬性更改更新 ComboBox 的狀態。

3

void addActionListener(ActionListener l)

新增一個 ActionListener。

4

void addItem(Object anObject)

向專案列表新增一個專案。

5

void addItemListener(ItemListener aListener)

新增一個 ItemListener。

6

void addPopupMenuListener(PopupMenuListener l)

新增一個 PopupMenu 監聽器,它將監聽來自 ComboBox 彈出部分的通知訊息。

7

void configureEditor(ComboBoxEditor anEditor, Object anItem)

使用指定的專案初始化編輯器。

8

protected void configurePropertiesFromAction(Action a)

設定此 ComboBox 上的屬性以匹配指定 Action 中的屬性。

9

void contentsChanged(ListDataEvent e)

此方法作為實現副作用是公共的。

10

protected PropertyChangeListener createActionPropertyChangeListener(Action a)

建立並返回一個 PropertyChangeListener,負責監聽來自指定 Action 的更改並更新相應的屬性。

11

protected JComboBox.KeySelectionManager createDefaultKeySelectionManager()

返回預設鍵選擇管理器的一個例項。

12

protected void fireActionEvent()

通知所有已註冊對此事件型別進行通知的監聽器。

13

protected void fireItemStateChanged(ItemEvent e)

通知所有已註冊對此事件型別進行通知的監聽器。

14

void firePopupMenuCanceled()

通知 PopupMenuListeners ComboBox 的彈出部分已被取消。

15

void firePopupMenuWillBecomeInvisible()

通知 PopupMenuListeners ComboBox 的彈出部分已變為不可見。

16

void firePopupMenuWillBecomeVisible()

通知 PopupMenuListeners ComboBox 的彈出部分將變為可見。

17

AccessibleContext getAccessibleContext()

獲取與此 JComboBox 關聯的 AccessibleContext。

18

Action getAction()

返回為此 ActionEvent 源當前設定的操作,如果未設定任何操作,則返回 null。

19

String getActionCommand()

返回包含在傳送到操作監聽器的事件中的操作命令。

20

ActionListener[] getActionListeners()

返回使用 addActionListener() 新增到此 JComboBox 的所有 ActionListener 的陣列。

21

ComboBoxEditor getEditor()

返回用於繪製和編輯 JComboBox 欄位中所選專案的編輯器。

22

Object getItemAt(int index)

返回指定索引處的列表項。

23

int getItemCount()

返回列表中專案的數量。

24

ItemListener[] getItemListeners()

返回使用 addItemListener() 新增到此 JComboBox 的所有 ItemListener 的陣列。

25

JComboBox.KeySelectionManager getKeySelectionManager()

返回列表的鍵選擇管理器。

26

int getMaximumRowCount()

返回組合框在沒有捲軸的情況下可以顯示的最大專案數。

27

ComboBoxMode getModel()

返回 JComboBox 當前使用的模型。

28

PopupMenuListener[]getPopupMenuListeners()

返回使用 addPopupMenuListener() 新增到此 JComboBox 的所有 PopupMenuListeners 的陣列。

29

Object getPrototypeDisplayValue()

返回“原型顯示”值 - 用於計算顯示高度和寬度的物件。

30

ListCellRenderer getRenderer()

返回用於在 JComboBox 欄位中顯示所選專案的渲染器。

31

int getSelectedIndex()

返回列表中與給定專案匹配的第一個專案。

32

Object getSelectedItem()

返回當前選定的專案。

33

Object[] getSelectedObjects()

返回一個包含所選專案的陣列。

34

ComboBoxUI getUI()

返回呈現此元件的 L&F 物件。

35

String getUIClassID()

返回呈現此元件的 L&F 類的名稱。

36

void hidePopup()

使 ComboBox 關閉其彈出視窗。

37

void insertItemAt(Object anObject, int index)

在給定索引處的專案列表中插入一個專案。

38

protected void installAncestorListener()

39

void intervalAdded(ListDataEvent e)

此方法作為實現副作用是公共的。

40

void intervalRemoved(ListDataEvent e)

此方法作為實現副作用是公共的。

41

boolean isEditable()

如果 JComboBox 可編輯,則返回 true。

42

boolean isLightWeightPopupEnabled()

獲取 lightWeightPopupEnabled 屬性的值。

43

boolean isPopupVisible()

確定彈出視窗的可見性。

44

protected String paramString()

返回此 JComboBox 的字串表示形式。

45

void processKeyEvent(KeyEvent e)

處理 KeyEvents,查詢 Tab 鍵。

46

void removeActionListener(ActionListener l)

刪除一個 ActionListener。

47

void removeAllItems()

從專案列表中刪除所有專案。

48

void removeItem(Object anObject)

從專案列表中刪除一個專案。

49

void removeItemAt(int anIndex)

刪除 anIndex 處的專案。此方法僅在 JComboBox 使用可變資料模型時才有效。

50

void removeItemListener(ItemListener aListener)

刪除一個 ItemListener。

51

void removePopupMenuListener(PopupMenuListener l)

刪除一個 PopupMenuListener。

52

protected void selectedItemChanged()

此受保護方法是實現特定的。

53

boolean selectWithKeyChar(char keyChar)

選擇與指定鍵盤字元對應的列表項並返回 true,如果存在與該字元對應的專案。

54

void setAction(Action a)

設定 ActionEvent 源的操作。

55

void setActionCommand(String aCommand)

設定應包含在傳送到操作監聽器的事件中的操作命令。

56

void setEditable(boolean aFlag)

確定 JComboBox 欄位是否可編輯。

57

void setEditor(ComboBoxEditor anEditor)

設定用於繪製和編輯 JComboBox 欄位中所選專案的編輯器。

58

void setEnabled(boolean b)

啟用 ComboBox 以便可以選擇專案。

59

void setKeySelectionManager(JComboBox.KeySelectionManager aManager)

設定物件將鍵盤字元轉換為列表選擇。

60

void setLightWeightPopupEnabled(boolean aFlag)

設定 lightWeightPopupEnabled 屬性,該屬性提供一個提示,指示是否應使用輕量級元件來包含 JComboBox,而不是重量級元件(如面板或視窗)。

61

void setMaximumRowCount(int count)

設定 JComboBox 顯示的最大行數。

62

void setModel(ComboBoxModel aModel)

設定 JComboBox 用於獲取專案列表的資料模型。

63

void setPopupVisible(boolean v)

設定彈出視窗的可見性。

64

void setPrototypeDisplayValue(Object prototypeDisplayValue)

設定用於計算 UI 部分顯示大小的原型顯示值。

65

void setRenderer(ListCellRenderer aRenderer)

設定渲染器,該渲染器繪製列表項以及 JComboBox 欄位中從列表中選擇的專案。

66

void setSelectedIndex(int anIndex)

選擇 anIndex 索引處的專案。

67

void setSelectedItem(Object anObject)

將 ComboBox 顯示區域中的所選專案設定為引數中的物件。

68

void setUI(ComboBoxUI ui)

設定呈現此元件的 L&F 物件。

69

void showPopup()

使 ComboBox 顯示其彈出視窗。

70

void updateUI()

將 UI 屬性重置為當前外觀和風格中的值。

繼承的方法

此類繼承自以下類:

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JComboBox 示例

使用您選擇的任何編輯器建立以下 Java 程式,例如在 D:/ > SWING > com > tutorialspoint > gui >

SwingControlDemo.java

package com.tutorialspoint.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 com\tutorialspoint\gui\SwingControlDemo.java

如果未發生錯誤,則表示編譯成功。使用以下命令執行程式。

D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

驗證以下輸出。

Swing JComboBox
swing_controls.htm
廣告