TITLE:JOptionPaneのデフォルトフォーカス
Posted by aterai at 2009-11-23

JOptionPaneのデフォルトフォーカス

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

概要

JOptionPaneにデフォルトでフォーカスをもつコンポーネントを追加します。
OptionPaneDefaultFocus.png

サンプルコード

#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
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) {}
#spanadd

#spanend
  @Override public void ancestorMoved(AncestorEvent e) {
    /* not needed */
  }
#spanadd

#spanend
  @Override public void ancestorRemoved(AncestorEvent e) {
    /* not needed */
  }
});

解説

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

解説

上記のサンプルでは、JOptionPane.showConfirmDialogで表示するJTextFieldにデフォルトのフォーカスがあたるように設定しています。
  • 左上
    • デフォルト
  • 左上: Default
    • デフォルトのConfirmDialogの場合、初期フォーカスは入力欄ではなくOKボタンにある
      #spandel
      int result = JOptionPane.showConfirmDialog(frame, textField, "Input Text",
      #spanend
                       JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      #spandel
      if(result==JOptionPane.OK_OPTION) textArea.setText(textField.getText());
      #spanend
      #spanadd
      int result = JOptionPane.showConfirmDialog(
      #spanend
          frame, textField, "Input Text",
          JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      #spanadd
      if (result == JOptionPane.OK_OPTION) {
      #spanend
        textArea.setText(textField.getText());
      #spanadd
      }
      #spanend
      
  • 右上
    • JOptionPane#createDialog(...)でJDialogを取得し、WindowListener#windowOpenedで、textField.requestFocusInWindow()
  • 右上: WindowListener
    • JOptionPane#createDialog(...)JDialogを取得しWindowListener#windowOpenedtextField.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;
      #spandel
      if(selectedValue != null && selectedValue instanceof Integer) {
      #spanend
        result = ((Integer)selectedValue).intValue();
      #spanadd
      if (selectedValue != null && selectedValue instanceof Integer) {
      #spanend
        result = ((Integer) selectedValue).intValue();
      }
      #spandel
      result==JOptionPane.OK_OPTION) textArea.setText(textField.getText());
      #spanend
      #spanadd
      if (result == JOptionPane.OK_OPTION) {
      #spanend
        textArea.setText(textField.getText());
      #spanadd
      }
      #spanend
      
  • 左下
    • textFieldにHierarchyListenerを追加し、hierarchyChangedが呼ばれたときに、textField.requestFocusInWindow()
  • 左下: HierarchyListener
    • textFieldHierarchyListenerを追加し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(){
          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);
      #spandel
      if(result==JOptionPane.OK_OPTION) textArea.setText(textField3.getText());
      #spanend
      #spanadd
      int result = JOptionPane.showConfirmDialog(
      #spanend
          frame, textField3, "Input Text",
          JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      #spanadd
      if (result == JOptionPane.OK_OPTION) {
      #spanend
        textArea.setText(textField3.getText());
      #spanadd
      }
      #spanend
      
  • 右下
    • textFieldにaddAncestorListenerを追加し、ancestorAddedが呼ばれたときに、textField.requestFocusInWindow()
    • Swing - Input focus
  • 右下: AncestorListener
    • textFieldaddAncestorListenerを追加しancestorAddedが呼ばれたときにtextField.requestFocusInWindow();を実行
    • Swing - Input focus

参考リンク

参考リンク

コメント

コメント