I am working on a little project, which in this example is made of 2 classes (and windows). Basically, when I click on a button placed on the JFrame 1, it open a JDialog, and then start the treatment function, which is very long to execute (in the example, I just put thousand of hash). Then, when the treatment is finished, I want to close the JDialog (or if it's not possible, I want the program to open a new JFrame or JDialog, but only when the treatment is finished). Is there a way to achieve one of these goals ?
Here is my main class, which contains the JFrame :
public class test2 {
private static JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
frame = new JFrame("JDialog Test");
frame.setSize(650, 550);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.setLocationByPlatform(true);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
private static JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(150, 100, 150, 100));
panel.setPreferredSize(new Dimension(400, 400));
JButton button = new JButton("Open JDialog");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(new Dialog());
new Thread(new Runnable() {
@Override
public void run() {
Treatment();
}
}).start();
}
});
panel.add(button);
return panel;
}
public static void Treatment() {
String str = "a";
for(int i = 0; i < 4000000; i++) {
Thread.yield();
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
md.update(str.getBytes());
byte[] digest = md.digest();
String myHash = DatatypeConverter.printHexBinary(digest).toUpperCase();
if (i % 400000 == 0) {
System.out.println("Processing " + i / 400000 + "0 %");
}
}
System.out.println("Processing 100 %");
}
And here is my second class, which contains the Runnable JDialog :
public class Dialog implements Runnable {
private JDialog dialog;
public Dialog() {
this.dialog = new JDialog();
}
@Override
public void run() {
this.dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
this.dialog.setTitle("Veuillez patienter");
this.dialog.setSize(300, 200);
this.dialog.setLocationRelativeTo(null);
this.dialog.setVisible(true);
}
}
I hope I explain it well, thanks everyone !