2016-07-13 7 views
0

私のアプリケーションは、別のスレッドでJOptionPaneダイアログボックスを開くサードパーティのスタンドアロンアプリケーションと統合されています。開いているダイアログボックスをすべて閉じるためにスレッドを実行しています。閉じる前に、ダイアログボックスに書かれたメッセージ。プログラムで取得する方法JOptionPaneメッセージの内容

私が達成しようとしていたと

私のサンプルのメインプログラム:

public static void main(String[] args)throws Exception{ 
    ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); 
    executor.scheduleAtFixedRate(() -> { 
     Window[] possibleWindow = Window.getWindows(); 
     if (possibleWindow != null && possibleWindow.length > 0) { 
      System.out.println("Found " + possibleWindow.length + "Window(s) " + possibleWindow[0].getClass().getSuperclass()); 
      for (int i = possibleWindow.length - 1; i >= 0; i--) { 
       try { 
        Window window = possibleWindow[i]; 
        //here where I need to get the dialog box message before closing it. 
        window.dispose(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }, 1, 1, TimeUnit.SECONDS); 

    JOptionPane.showMessageDialog(null, "test !!!!"); 
} 
+0

あなたはpossibleWindow配列内のすべての参照がのJOptionPaneのインスタンスになることを確信していますか?もしそうなら、アプリケーションの作成時にPaneの内容を登録する新しいクラスを作成するためにJOptionPaneを拡張(直接拡張またはカプセル化によって)する方が適していますか?あなたは最後にクリーンアップしようとしていないだろうそのように。 – Teto

+1

JOptionPaneはWindowのサブクラスではありません。だから私はそれが可能なウィンドウの配列でどのように表示されるのか見ていない。しかし、他の方法で既存のJOptionPaneへの参照を取得できる場合は、JOptionPane.getMessage()を呼び出してメッセージオブジェクトを取得できます。 – Teto

+0

私のアプリケーションはサードパーティのスイングスタンドアロンアプリケーションと統合されており、メッセージダイアログを表示するためにJOptionPaneオブジェクトを作成する必要はありません。 –

答えて

1

を私が正しくあなたの質問を取得する場合、あなたクレートJOptionPaneのオブジェクトとそれらにメッセージを与えます。後で、あなたは彼らに与えたメッセージを知りたいのですか?

もしそうなら、簡単な解決策はMap<JOptionPane, String>のような中央の地図を作成することです。新しいJOptionPaneを作成するたびに、そのJOptionPaneとそのメッセージを覚えています。そして清掃時に。まだ稼動しているJOptionPaneオブジェクトのメッセージを取得するだけです。

+0

私のアプリケーションはサードパーティのスイングスタンドアロンアプリケーションと統合されており、メッセージダイアログを表示するためのJOptionPaneオブジェクトを作成していません –

0

ウィンドウのすべてのコンポーネントが再帰的に必要です。 このソリューションは、あなたのケースで動作します:

public static String getMess(Container w){    
    for (Component component : w.getComponents()) { 
     if (component instanceof JLabel) { 
      return ((JLabel) component).getText(); 
     } 
     else if (component instanceof JTextField){ 
      return ((JTextField) component).getText(); 
     } 
     else if (component instanceof Container){ 
      String s = getMess((Container) component); 
      if (!s.isEmpty()){ 
       return s; 
      } 
     } 
    } 
    return ""; 
} 
+0

解決方法を試しました。私はサンプルのメインメソッドプログラムで試してみました。 –

0

このソリューションは、私の仕事:

if (window instanceof JDialog) { 
     System.out.println("text : " + ((JOptionPane)((JDialog) window).getContentPane().getComponents()[0]).getMessage()); 
    } 
関連する問題