Swing-jcombobox
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 -この保護されたフィールドは実装固有です。
- 保護されたComboBoxEditorエディタ-この保護されたフィールドは実装固有です。
- protected boolean isEditable -この保護フィールドは実装固有です。
- protected JComboBox.KeySelectionManager keySelectionManager -この保護フィールドは実装固有です。
- protected boolean lightWeightPopupEnabled -この保護フィールドは実装固有です。
- protected int maximumRowCount -この保護フィールドは実装固有です。
- 保護されたListCellRendererレンダラー-この保護されたフィールドは実装固有です。
- protected Object selectedItemReminder -この保護フィールドは実装固有です。
クラスコンストラクター
| Sr.No. | Constructor & Description |
|---|---|
| 1 |
JComboBox() デフォルトのデータモデルでJComboBoxを作成します。 |
| 2 |
JComboBox(ComboBoxModel aModel) 既存のComboBoxModelからアイテムを取得するJComboBoxを作成します。 |
| 3 |
JComboBox(Object[] items) 指定された配列の要素を含むJComboBoxを作成します。 |
| 4 |
JComboBox(Vector<?> items) 指定されたベクター内の要素を含むJComboBoxを作成します。 |
クラスメソッド
Swing JComboBoxクラスのメソッドのリストを以下に示します。
| Sr.No. | Method & Description |
|---|---|
| 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) ComboBoxのポップアップ部分からの通知メッセージをリッスンするPopupMenuリスナーを追加します。 |
| 7 |
void configureEditor(ComboBoxEditor anEditor, Object anItem) 指定した項目でエディターを初期化します。 |
| 8 |
protected void configurePropertiesFromAction(Action a) このComboBoxのプロパティを、指定されたアクションのプロパティと一致するように設定します。 |
| 9 |
void contentsChanged(ListDataEvent e) このメソッドは、実装の副作用として公開されています。 |
| 10 |
protected PropertyChangeListener createActionPropertyChangeListener(Action a) 指定されたActionからの変更をリッスンし、適切なプロパティを更新するPropertyChangeListenerを作成して返します。 |
| 11 |
protected JComboBox.KeySelectionManager createDefaultKeySelectionManager() デフォルトのキー選択マネージャーのインスタンスを返します。 |
| 12 |
protected void fireActionEvent() このイベントタイプの通知に関心を登録したすべてのリスナーに通知します。 |
| 13 |
protected void fireItemStateChanged(ItemEvent e) このイベントタイプの通知に関心を登録したすべてのリスナーに通知します。 |
| 14 |
void firePopupMenuCanceled() ComboBoxのポップアップ部分がキャンセルされたことをPopupMenuListenersに通知します。 |
| 15 |
void firePopupMenuWillBecomeInvisible() ComboBoxのポップアップ部分が見えなくなったことをPopupMenuListenersに通知します。 |
| 16 |
void firePopupMenuWillBecomeVisible() ComboBoxのポップアップ部分が表示されることをPopupMenuListenerに通知します。 |
| 17 |
AccessibleContext getAccessibleContext() このJComboBoxに関連付けられているAccessibleContextを取得します。 |
| 18 |
Action getAction() このActionEventソースに現在設定されているActionを返します。Actionが設定されていない場合は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 |
このメソッドは、実装の副作用として公開されています。 |
| 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) TabEventを探してKeyEventsを処理します。 |
| 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の例
たとえば、 D:/> SWING> com> finddevguides> gui> の任意のエディターを使用して、次のJavaプログラムを作成します。
SwingControlDemo.java
package com.finddevguides.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\finddevguides\gui\SwingControlDemo.java
エラーが発生しない場合、コンパイルが成功したことを意味します。 次のコマンドを使用してプログラムを実行します。
D:\SWING>java com.finddevguides.gui.SwingControlDemo
次の出力を確認します。