2015-09-09 10 views
0

我有一個應用程序,如果我點擊button1它會顯示一個模態對話框dialog1(但我沒有dialog1的實例。),它會啓動一個線程,在線程中的哪裏做了一些處理,並且在線程處理的中間,而在dialog1線程想要顯示一個警報,也許服務器沒有連接。但重點不在dialog1之上。它作爲兄弟加載到dialog1。有沒有什麼方法可以讓孩子看到提示dialog1獲取模態對話框實例揮杆

下面是示例。

public class Main1 extends javax.swing.JFrame { 

Thread show = null; 
private javax.swing.JButton jButton1; 

public Main1() { 
    setBounds(0, 0, 500, 500); 
    jButton1 = new javax.swing.JButton("Button1"); 
    jButton1.setBounds(10, 10, 100, 30); 
    jButton1.setVisible(true); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      show.start(); 
      //For Example I'm having this. I have a function call which will create and show a dialog similar to this. 
      java.awt.Frame frame = new java.awt.Frame(); 
      JDialog dialog1 = new JDialog(frame, true); 
      dialog1.setBounds(20, 20, 300, 300); 

      dialog1.show(); 
     } 
    }); 
    getContentPane().add(jButton1); 
    getContentPane().setLayout(null); 
    show = new Thread(new Runnable() { 

     @Override 
     public void run() { 
      try { 
       Thread.sleep(3000); 
       shoe(); 
      } catch (InterruptedException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    }); 
    setDefaultCloseOperation(2); 

} 

private void shoe() { 
    JOptionPane.showMessageDialog(this, "HEELO"); 
} 

public static void main(String... f) { 
    new Main1().setVisible(true); 
} 
} 

是否有無論如何獲取Main1的子模態實例?所以我可以提供該實例JOptionPane.showMessageDialog(this, "HEELO");而不是this或任何其他方式來實現這一目標?

如果我的理解錯誤,請糾正我。如果這是重複的,請原諒我。

+0

閱讀甲骨文步道 - 事件調度線程和初始線程 – mKorbel

回答

0

哇,終於整理出來了。從問題Get Any/All Active JFrames in Java Application?

的幫助下,我從列表中的活動窗口,給了作爲父母

private void shoe() { 
    Component parent = this; 
    Window[] allWindows = Window.getWindows(); 
    for(Window window : allWindows){ 
     //Validate and find the appropriate window by its instance name or someother mean 
     parent = window; 
    } 

    JOptionPane.showMessageDialog(parent, "HEELO"); 
} 
0

您的實施有幾個問題。在我看來,最主要的是你正在修改事件分派線程外的swing組件的狀態。這不是我,你可以在Java中的併發實踐中讀到:「Swing單線程規則:Swing組件和模型只能從事件分派線程創建,修改和查詢。」

我的第一個提示將被讀取更多關於Concurrency in Swing。這看起來正是你想要做的。

我的第二個技巧是嘗試改進程序的模塊化。通過改進你的應用程序的模塊化,你可以達到你想要的。

public class Application extends JFrame { 

    Thread show = null; 

    public Application() { 
     setBounds(0, 0, 500, 500); 
     getContentPane().add(createActionButton()); 
     getContentPane().setLayout(null); 
     setDefaultCloseOperation(2); 
    } 

    private createActionButton() { 
     button = new javax.swing.JButton("Action Button"); 
     button.setBounds(10, 10, 100, 30); 
     button.setVisible(true); 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent evt) { 
       JDialog actionDialog = new ActionDialog(); 
       new ConnectionWatchdogTask(actionDialog).execute(); 
       actionDialog.show(); 
      } 
     }); 
     return button; 
    } 

    public static void main(String... args) { 
     new Application().setVisible(true); 
    } 
} 

public class ActionDialog extends JDialog { 

    public ActionDialog() { 
     super(new Frame(), true); 
     setBounds(20, 20, 300, 300); 
    } 
} 

public class ConnectionWatchdogTask extends SwingWorker<String, Object> { 

    private JDialog parentDialog; 

    public ConnectionWatchdogTask(JDialog dialog) { 
     parentDialog = dialog; 
    } 

    @Override 
    public String doInBackground() { 
     try { 
      Thread.sleep(3000); 
      return findConnectionStateOfServer(); 
     } catch (InterruptedException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    @Override 
    protected void done() { 
     JOptionPane.showMessageDialog(parentDialog, "Warning of some sort"); 
    } 
} 
+0

感謝您的提示。 –