Swing/ItemListenerActionListener のバックアップの現在との差分(No.1)
- category: swing folder: ItemListenerActionListener title: ItemListenerとActionListenerの動作の違いを比較する tags: [ItemListener, ActionListener, JComboBox, JCheckBox, JRadioButton, ButtonGroup] author: aterai pubdate: 2018-04-23T18:28:12+09:00 description: JComboBoxやJCheckBoxなどに設定したItemListenerとActionListenerの動作を比較テストします。 image: https://drive.google.com/uc?id=1xpBmSl-frNjI1eCiUe2pzY2vo6Zp_FpKjA
概要
JComboBox
やJCheckBox
などに設定したItemListener
とActionListener
の動作を比較テストします。
Screenshot
Advertisement
サンプルコード
JComboBox<DayOfWeek> combo = new JComboBox<>(DayOfWeek.values());
combo.addItemListener(e -> {
ItemSelectable c = e.getItemSelectable();
DayOfWeek dow = (DayOfWeek) e.getItem();
boolean b = e.getStateChange() == ItemEvent.SELECTED;
print(textArea, "ItemListener", c.getClass(), b, dow);
});
combo.addActionListener(e -> {
Object c = e.getSource();
DayOfWeek dow = combo.getItemAt(combo.getSelectedIndex());
boolean b = Objects.equals("comboBoxChanged", e.getActionCommand());
print(textArea, "ActionListener", c.getClass(), b, dow);
});
View in GitHub: Java, Kotlin解説
-
JComboBox
-
ItemListener
-
ItemEvent#getItemSelectable()
でイベント元のJComboBox
が取得可能 -
ItemEvent#getItem()
でJComboBox
で選択されたアイテムが取得可能 -
ItemEvent#getStateChange()
でJComboBox
のアイテムが選択、選択解除されたかを取得可能 -
JComboBox#setSelectedIndex(...)
メソッドなどを使用してJComboBox
を直接マウスやキー入力で操作しなくても、ItemEvent
は発生する -
JComboBox
の同じアイテムを選択してもItemEvent
は発生しない
-
-
ActionListener
-
ActionEvent#getSource()
でイベント元のJComboBox
が取得可能 - デフォルトの
JComboBox
は、ActionEvent#getActionCommand()
で"comboBoxChanged"
を返す -
JComboBox#setSelectedIndex(...)
メソッドなどを使用してJComboBox
を直接マウスやキー入力で操作しなくても、ActionEvent
は発生する -
JComboBox
の同じアイテムを選択してもActionEvent
は発生する
-
-
-
JCheckBox
-
ItemListener
-
ItemEvent#getItemSelectable()
、またはItemEvent#getItem()
でイベント元のJCheckBox
が取得可能 -
ItemEvent#getStateChange()
でJCheckBox
が選択されたかを取得可能 -
JCheckBox#setSelected(...)
メソッドなどを使用してJCheckBox
を直接マウスやキー入力で操作しなくても、ItemEvent
は発生する -
JCheckBox#setSelected(...)
メソッドを使用しても選択状態が変化しない場合、ItemEvent
は発生しない
-
-
ActionListener
-
ActionEvent#getSource()
でイベント元のJCheckBox
が取得可能 - デフォルトの
JCheckBox
は、ActionEvent#getActionCommand()
でJCheckBox#getText()
と同じ値を返す -
JCheckBox#setSelected(...)
メソッドなどを使用してJCheckBox
の状態を変更しても、ActionEvent
は発生しない
-
-
-
JRadioButton
(ButtonGroup
)-
ItemListener
-
ItemEvent#getItemSelectable()
、またはItemEvent#getItem()
でイベント元のJCheckBox
が取得可能 -
ItemEvent#getStateChange()
でJRadioButton
が選択されたかを取得可能 -
JRadioButton#setSelected(...)
メソッドなどを使用してJRadioButton
を直接マウスやキー入力で操作しなくても、ItemEvent
は発生する -
JRadioButton#setSelected(...)
メソッドを使用しても選択状態が変化しない場合、ItemEvent
は発生しない - 注:
ButtonGroup#getSelection()
は、JRadioButton#setSelected(...)
メソッドを使用して選択変更した場合はnull
、マウスなどで選択変更した場合はそのButtonModel
を返す
-
-
ActionListener
-
ActionEvent#getSource()
でイベント元のJRadioButton
が取得可能 - デフォルトの
JRadioButton
は、ActionEvent#getActionCommand()
でJRadioButton#getText()
と同じ値を返す -
JRadioButton#setSelected(...)
メソッドなどを使用してJRadioButton
の状態を変更しても、ActionEvent
は発生しないJComboBox
-
-
-
ItemListener
-
ItemEvent#getItemSelectable()
でイベント元のJComboBox
が取得可能 -
ItemEvent#getItem()
でJComboBox
で選択されたアイテムが取得可能 -
ItemEvent#getStateChange()
でJComboBox
のアイテムが選択、選択解除されたかを取得可能 -
JComboBox#setSelectedIndex(...)
メソッドなどを使用してJComboBox
を直接マウスやキー入力で操作しなくてもItemEvent
は発生する -
JComboBox
の同じアイテムを選択してもItemEvent
は発生しない
-
-
ActionListener
-
ActionEvent#getSource()
でイベント元のJComboBox
が取得可能 - デフォルトの
JComboBox
はActionEvent#getActionCommand()
でcomboBoxChanged
を返す -
JComboBox#setSelectedIndex(...)
メソッドなどを使用してJComboBox
を直接マウスやキー入力で操作しなくても、ActionEvent
は発生する -
JComboBox
の同じアイテムを選択してもActionEvent
は発生する
-
JCheckBox
-
ItemListener
-
ItemEvent#getItemSelectable()
、またはItemEvent#getItem()
でイベント元のJCheckBox
が取得可能 -
ItemEvent#getStateChange()
でJCheckBox
が選択されたかを取得可能 -
JCheckBox#setSelected(...)
メソッドなどを使用してJCheckBox
を直接マウスやキー入力で操作しなくてもItemEvent
は発生する -
JCheckBox#setSelected(...)
メソッドを使用しても選択状態が変化しない場合、ItemEvent
は発生しない
-
-
ActionListener
-
ActionEvent#getSource()
でイベント元のJCheckBox
が取得可能 - デフォルトの
JCheckBox
はActionEvent#getActionCommand()
でJCheckBox#getText()
と同じ値を返す -
JCheckBox#setSelected(...)
メソッドなどを使用してJCheckBox
の状態を変更しても、ActionEvent
は発生しない
-
JRadioButton
(ButtonGroup
)
-
ItemListener
-
ItemEvent#getItemSelectable()
、またはItemEvent#getItem()
でイベント元のJCheckBox
が取得可能 -
ItemEvent#getStateChange()
でJRadioButton
が選択されたかを取得可能 -
JRadioButton#setSelected(...)
メソッドなどを使用してJRadioButton
を直接マウスやキー入力で操作しなくても、ItemEvent
は発生する -
JRadioButton#setSelected(...)
メソッドを使用しても選択状態が変化しない場合、ItemEvent
は発生しない -
ButtonGroup#getSelection()
はJRadioButton#setSelected(...)
メソッドを使用して選択変更した場合はnull
、マウスなどで選択変更した場合はそのButtonModel
を返す
-
-
ActionListener
-
ActionEvent#getSource()
でイベント元のJRadioButton
が取得可能 - デフォルトの
JRadioButton
はActionEvent#getActionCommand()
でJRadioButton#getText()
と同じ値を返す -
JRadioButton#setSelected(...)
メソッドなどを使用してJRadioButton
の状態を変更してもActionEvent
は発生しない
-