Swing/OptionPaneDefaultFocus のバックアップの現在との差分(No.3)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/OptionPaneDefaultFocus へ行く。
- 1 (2009-11-23 (月) 19:41:03)
- 2 (2011-06-02 (木) 02:46:26)
- 3 (2012-02-10 (金) 16:58:01)
- 4 (2013-01-04 (金) 15:58:09)
- 5 (2013-08-30 (金) 01:35:26)
- 6 (2013-12-24 (火) 20:21:54)
- 7 (2014-11-25 (火) 03:03:31)
- 8 (2015-02-10 (火) 15:29:03)
- 9 (2015-06-17 (水) 16:40:18)
- 10 (2017-03-16 (木) 21:41:31)
- 11 (2018-01-19 (金) 13:58:35)
- 12 (2020-01-15 (水) 18:35:09)
- 13 (2021-07-14 (水) 23:43:21)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JOptionPaneのデフォルトフォーカス #navi(../) RIGHT:Posted by [[aterai]] at 2009-11-23 *JOptionPaneのデフォルトフォーカス [#p8003f6b] JOptionPaneにデフォルトでフォーカスをもつコンポーネントを追加します。 --- 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: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTQoUaDrDI/AAAAAAAAAf4/nUnrCrmb5io/s800/OptionPaneDefaultFocus.png --- * 概要 [#summary] `JOptionPane`にデフォルトでフォーカスをもつコンポーネントを追加します。 -&jnlp; -&jar; -&zip; #download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTQoUaDrDI/AAAAAAAAAf4/nUnrCrmb5io/s800/OptionPaneDefaultFocus.png) //#screenshot #ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTQoUaDrDI/AAAAAAAAAf4/nUnrCrmb5io/s800/OptionPaneDefaultFocus.png) **サンプルコード [#nd2fa665] #code{{ * サンプルコード [#sourcecode] #code(link){{ textField4.addAncestorListener(new AncestorListener() { @Override public void ancestorAdded(AncestorEvent event) { textField4.requestFocusInWindow(); @Override public void ancestorAdded(AncestorEvent e) { e.getComponent().requestFocusInWindow(); } @Override public void ancestorMoved(AncestorEvent event) {} @Override public void ancestorRemoved(AncestorEvent event) {} @Override public void ancestorMoved(AncestorEvent e) { /* not needed */ } @Override public void ancestorRemoved(AncestorEvent e) { /* not needed */ } }); }} **解説 [#k9a64d2f] 上記のサンプルでは、JOptionPane.showConfirmDialogで表示するJTextFieldにデフォルトのフォーカスがあたるように設定しています。 * 解説 [#explanation] 上記のサンプルでは、`JOptionPane.showConfirmDialog`で表示する`JTextField`にデフォルトのフォーカスがあたるように設定しています。 -左上 -- デフォルト - 左上: `Default` -- デフォルトの`ConfirmDialog`の場合、初期フォーカスは入力欄ではなく`OK`ボタンにある #code{{ int result = JOptionPane.showConfirmDialog(frame, textField, "Input Text", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION) textArea.setText(textField.getText()); int result = JOptionPane.showConfirmDialog( 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を開いたときのフォーカスを指定>Swing/DefaultFocus]]など #code{{ JOptionPane pane = new JOptionPane(textField, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null); JOptionPane pane = new JOptionPane( 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 (selectedValue != null && selectedValue instanceof Integer) { result = ((Integer) selectedValue).intValue(); } result==JOptionPane.OK_OPTION) textArea.setText(textField.getText()); if (result == JOptionPane.OK_OPTION) { textArea.setText(textField.getText()); } }} -左下 -- textFieldにHierarchyListenerを追加し、hierarchyChangedが呼ばれたときに、textField.requestFocusInWindow() - 左下: `HierarchyListener` -- `textField`に`HierarchyListener`を追加し`hierarchyChanged`が呼ばれたときに`textField.requestFocusInWindow();`を実行 #code{{ textField3.addHierarchyListener(new HierarchyListener() { @Override public void hierarchyChanged(HierarchyEvent e) { if((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED)!=0 && textField3.isShowing()) { EventQueue.invokeLater(new Runnable(){ if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && textField3.isShowing()) { EventQueue.invokeLater(new Runnable() { @Override public void run() { textField3.requestFocusInWindow(); } }); } } }); int result = JOptionPane.showConfirmDialog(frame, textField3, "Input Text", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if(result==JOptionPane.OK_OPTION) textArea.setText(textField3.getText()); int result = JOptionPane.showConfirmDialog( 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() -- [http://forums.sun.com/thread.jspa?threadID=777887 Swing - Input focus] - 右下: `AncestorListener` -- `textField`に`addAncestorListener`を追加し`ancestorAdded`が呼ばれたときに`textField.requestFocusInWindow();`を実行 -- [https://community.oracle.com/thread/1354218 Swing - Input focus] **参考リンク [#u3c4a8af] * 参考リンク [#reference] - [[Windowを開いたときのフォーカスを指定>Swing/DefaultFocus]] - [http://forums.sun.com/thread.jspa?threadID=777887 Swing - Input focus] - [https://community.oracle.com/thread/1354218 Swing - Input focus] **コメント [#nef3f20f] * コメント [#comment] #comment #comment