Swing/OptionPaneDefaultFocus のバックアップの現在との差分(No.10)
- category: swing folder: OptionPaneDefaultFocus title: JOptionPaneのデフォルトフォーカス tags: [JOptionPane, Focus, JComponent, WindowListener, HierarchyListener, AncestorListener] author: aterai pubdate: 2009-11-23T19:41:03+09:00 description: JOptionPaneにデフォルトでフォーカスをもつコンポーネントを追加します。 image:
概要
JOptionPane
にデフォルトでフォーカスをもつコンポーネントを追加します。
Screenshot
Advertisement
サンプルコード
textField4.addAncestorListener(new AncestorListener() {
@Override public void ancestorAdded(AncestorEvent e) {
e.getComponent().requestFocusInWindow();
}
@Override public void ancestorMoved(AncestorEvent e) {}
@Override public void ancestorRemoved(AncestorEvent e) {}
#spanadd
#spanend
@Override public void ancestorMoved(AncestorEvent e) {
/* not needed */
}
#spanadd
#spanend
@Override public void ancestorRemoved(AncestorEvent e) {
/* not needed */
}
});
View in GitHub: Java, Kotlin解説
上記のサンプルでは、JOptionPane.showConfirmDialog
で表示するJTextField
にデフォルトのフォーカスがあたるように設定しています。
- 左上
- デフォルト
- 左上:
Default
- デフォルトの
ConfirmDialog
の場合、初期フォーカスは入力欄ではなくOK
ボタンにある#spandel int result = JOptionPane.showConfirmDialog(frame, textField, "Input Text", #spanend JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); #spanadd int result = JOptionPane.showConfirmDialog( #spanend frame, textField, "Input Text", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { textArea.setText(textField.getText()); }
- デフォルトの
- 右上
-
JOptionPane#createDialog(...)
でJDialog
を取得し、WindowListener#windowOpened
で、textField.requestFocusInWindow();
-
- 右上:
WindowListener
-
JOptionPane#createDialog(...)
でJDialog
を取得しWindowListener#windowOpened
でtextField.requestFocusInWindow();
を実行 - Windowを開いたときのフォーカスを指定など
-
#spandel
JOptionPane pane = new JOptionPane(textField, JOptionPane.PLAIN_MESSAGE,
#spanend
JOptionPane.OK_CANCEL_OPTION, null, null, null);
#spanadd
JOptionPane pane = new JOptionPane(
#spanend
textField, JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION, null, null, null);
JDialog dialog = pane.createDialog(frame, "Input Text");
dialog.addWindowListener(new WindowAdapter() {
@Override public void windowOpened(WindowEvent e) {
textField.requestFocusInWindow();
}
});
dialog.setVisible(true);
Object selectedValue = pane.getValue();
int result = JOptionPane.CLOSED_OPTION;
if (selectedValue != null && selectedValue instanceof Integer) {
result = ((Integer) selectedValue).intValue();
}
if (result == JOptionPane.OK_OPTION) {
textArea.setText(textField.getText());
}
- 左下
-
textField
にHierarchyListener
を追加し、hierarchyChanged
が呼ばれたときに、textField.requestFocusInWindow();
-
- 左下:
HierarchyListener
-
textField
にHierarchyListener
を追加しhierarchyChanged
が呼ばれたときにtextField.requestFocusInWindow();
を実行textField3.addHierarchyListener(new HierarchyListener() { @Override public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && textField3.isShowing()) { EventQueue.invokeLater(new Runnable() { @Override public void run() { textField3.requestFocusInWindow(); } }); } } }); #spandel int result = JOptionPane.showConfirmDialog(frame, textField3, "Input Text", #spanend JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); #spanadd int result = JOptionPane.showConfirmDialog( #spanend frame, textField3, "Input Text", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) { textArea.setText(textField3.getText()); }
-
- 右下
-
textField
にaddAncestorListener
を追加し、ancestorAdded
が呼ばれたときに、textField.requestFocusInWindow();
-
- 右下:
AncestorListener
-
textField
にaddAncestorListener
を追加しancestorAdded
が呼ばれたときにtextField.requestFocusInWindow();
を実行 - Swing - Input focus
-