开发者

Communication between frames, windows and components in Java Swing

开发者 https://www.devze.com 2023-02-17 09:58 出处:网络
I have a Main app window (JFrame) with JPanel. On JPanel I have some buttons. After click one of buttons I create JDialog window where user can choose two options: \"do it\" or \"cancel\". After click

I have a Main app window (JFrame) with JPanel. On JPanel I have some buttons. After click one of buttons I create JDialog window where user can choose two options: "do it" or "cancel". After clicking "do it" the dialog is closed and then I create some kind of progress window (another JDialog) with progress bar. In progress window I execute swingworker with long task. When task is done, I show message dialog (JOptionPane.showOptionDialog) with information about result of task ("success" or "fail"). The result of this task determines the state of main app window (I change label on button, for example “Login”/”Logout”). How can I "send" the result from this swingworker (in method done()) to main app window in order to refresh the state of main app window (change the label of button)? Do I need some kind of controller, maybe listener开发者_如何转开发 or only reference to main app window?

Thank you in advance, seishin


Pass a reference to the main window down the constructor stack to whatever object you want to have notify it, and then add a method to the main window to receive such a notification.

For example, in your main window JFrame:

public void taskComplete(boolean succeeded) { ... }
...
JDialog dialog = new JDialog(..., this);

Then in the dialog constructor save the reference to your main window, and when the task is complete, call mainWindow.taskComplete(true/false).

0

精彩评论

暂无评论...
验证码 换一张
取 消