• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JOptionPaneのデフォルトフォーカス
#navi(../)
RIGHT:Posted by [[aterai]] at 2009-11-23
#tags()
RIGHT:Posted by &author(aterai); at 2009-11-23
*JOptionPaneのデフォルトフォーカス [#p8003f6b]
JOptionPaneにデフォルトでフォーカスをもつコンポーネントを追加します。

-&jnlp;
-&jar;
-&zip;

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTQoUaDrDI/AAAAAAAAAf4/nUnrCrmb5io/s800/OptionPaneDefaultFocus.png)

**サンプルコード [#nd2fa665]
#code{{
#code(link){{
textField4.addAncestorListener(new AncestorListener() {
  @Override public void ancestorAdded(AncestorEvent event) {
    textField4.requestFocusInWindow();
  @Override public void ancestorAdded(AncestorEvent e) {
    e.getComponent().requestFocusInWindow();
    //or textField4.requestFocusInWindow();
  }
  @Override public void ancestorMoved(AncestorEvent event) {}
  @Override public void ancestorRemoved(AncestorEvent event) {}
  @Override public void ancestorMoved(AncestorEvent e) {}
  @Override public void ancestorRemoved(AncestorEvent e) {}
});
}}

**解説 [#k9a64d2f]
上記のサンプルでは、JOptionPane.showConfirmDialogで表示するJTextFieldにデフォルトのフォーカスがあたるように設定しています。

-左上
-- デフォルト
#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());
}}

-右上
-- 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);
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();
}
result==JOptionPane.OK_OPTION) textArea.setText(textField.getText());
}}

-左下
-- 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(){
        @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());
}}

-右下
-- textFieldにaddAncestorListenerを追加し、ancestorAddedが呼ばれたときに、textField.requestFocusInWindow()
-- [http://forums.sun.com/thread.jspa?threadID=777887 Swing - Input focus]

**参考リンク [#u3c4a8af]
- [[Windowを開いたときのフォーカスを指定>Swing/DefaultFocus]]
- [http://forums.sun.com/thread.jspa?threadID=777887 Swing - Input focus]

**コメント [#nef3f20f]
#comment