2016-06-28 3 views
0

私のプログラムでは、私はこのMAINウィンドウとHELPウィンドウを持っています。 HELPウィンドウ(開かれているとき)は、焦点が合っているかどうかにかかわらず常に上にとどまります。しかし、私がrequestFocusInWindow()のMAINウインドウ内のコンポーネントをHELPウインドウから起動するアクションリスナーで試してみると、問題は解決しません。バックグラウンドウィンドウのコンポーネントのフォーカスを維持するには?

これを実行する適切な方法は何ですか? TY :)

編集:要求されたよう

、ここで私が達成しようとしているものの短い例です。基本的には、メインウィンドウ内のTextFieldへのフォーカスをトリガーするには、HELPウィンドウ内のボタンが必要です。

import java.awt.*; 
    import java.awt.event.*; 

    import javax.swing.*; 

    public class Main { 

     public static void initGUI() { 
      mainFrame = new JFrame("Main"); 
      helpFrame = new JFrame("Help"); 

      mainFrame.setPreferredSize(new Dimension(500, 200)); 
      helpFrame.setPreferredSize(new Dimension(500, 200)); 

      mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      helpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

      mainFrame.setLayout(new FlowLayout()); 
      helpFrame.setLayout(new FlowLayout()); 

      mainTextView = new JTextField("", 20); 

      mainButton = new JButton("Open Help"); 
      helpButton = new JButton("Request Focus"); 

      mainButton.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (e.getSource().equals(mainButton)) { 
         helpFrame.pack(); 
         helpFrame.setVisible(true); 
        } 
       } 
      }); 

      helpButton.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        if (e.getSource().equals(helpButton)) 
         System.out.println("Focus requested:" + mainTextView.requestFocusInWindow()); 
       } 
      }); 

      helpFrame.add(helpButton); 

      mainFrame.add(mainTextView); 
      mainFrame.add(mainButton); 

      mainFrame.pack(); 
      mainFrame.setVisible(true); 
     } 

     public static void main(String[] args) { 
      initGUI(); 
     } 

     static JFrame mainFrame, helpFrame; 

     static JTextField mainTextView; 

     static JButton mainButton, helpButton; 
    } 
+1

ポストあなたの[SSCCE](http://sscce.org/)問題を示しています。 SSCCEではMAINフレームにはHELPフレームを表示するボタンがあり、ヘルプフレームにはメインフレームボタンにフォーカスを戻すボタンがあります。まずそれを働かせてください。 – camickr

答えて

0

だから、修正はかなり簡単でした。 の代わりにrequestFocus()が使用されていると、正常に動作しているようです。

ちょっと私はこれに費やさどのくらいの時間のために愚かな感じ:P

helpButton.addActionListener(new ActionListener() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getSource().equals(helpButton)) 
      mainTextView.requestFocus(); 
    } 
}); 
関連する問題