获取模态对话框实例swing
Get modal dialog instance swing
我有一个应用程序,如果我单击 button1
,它会显示一个模式对话框 dialog1
(但我没有 dialog1
的实例),以及它将启动一个线程,在该线程中执行一些处理,并且在线程处理的中间,而在 dialog1
中线程想要显示一个警报,可能服务器未连接。但是重点不在dialog1
上面。它作为 dialog1
的同级加载。有什么方法可以向 dialog1
的 child 显示警报
这是示例。
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 的 child 模态实例?这样我就可以将该实例提供给 JOptionPane.showMessageDialog(this, "HEELO");
而不是 this
或任何其他方式来实现这一点?
如果我的理解有误,请纠正我。如果这是重复的,请原谅。
您的实施存在几个问题。在我看来,最主要的是您正在修改事件调度线程之外的 swing 组件的状态。这不是我说的,您可以在 Java 并发实践中阅读:"The Swing single-thread rule: Swing components and models should be created, modified, and queried only from the event-dispatching thread."
我的第一个技巧是阅读更多关于 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");
}
}
哇,终于搞定了。来自问题Get Any/All Active JFrames in Java Application?
的帮助
我从列表中得到了活动的 window 并将其作为父项
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");
}
我有一个应用程序,如果我单击 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 的 child 模态实例?这样我就可以将该实例提供给 JOptionPane.showMessageDialog(this, "HEELO");
而不是 this
或任何其他方式来实现这一点?
如果我的理解有误,请纠正我。如果这是重复的,请原谅。
您的实施存在几个问题。在我看来,最主要的是您正在修改事件调度线程之外的 swing 组件的状态。这不是我说的,您可以在 Java 并发实践中阅读:"The Swing single-thread rule: Swing components and models should be created, modified, and queried only from the event-dispatching thread."
我的第一个技巧是阅读更多关于 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");
}
}
哇,终于搞定了。来自问题Get Any/All Active JFrames in Java Application?
的帮助我从列表中得到了活动的 window 并将其作为父项
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");
}