My application is integrated with 3rd party standalone application which will open JOptionPane dialog boxes in separate thread and I am running the thread to close all the opened dialog boxes.So before closing I need to get the message written on dialog box.
My sample main program with which I am trying to achieve:
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 !!!!");
}
If I get your question correctly, you crate JOptionPane objects and give them a message; and later on, you want to know the message you gave to them?
If so, a simple solution would be to create a central map, like Map<JOptionPane, String>
. Each time you create a new JOptionPane, you remember it (and its message); and upon cleanup; you simply fetch the messages for those JOptionPane objects that are still up.
You need recursively all components of window. This solution will work in your case :
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 "";
}
This solution worked for me :
if (window instanceof JDialog) {
System.out.println("text : " + ((JOptionPane)((JDialog) window).getContentPane().getComponents()[0]).getMessage());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.