0

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 !

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
VassiliKan
  • 51
  • 1
  • 7
  • See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details of using a `SwingWorker`. – Andrew Thompson Feb 11 '21 at 04:59
  • Please remove the parts of the sample code that aren't relevant to the question you're asking; the cosmetic changes you make to the frames, the loops, etc. This helps those trying to help you =) – Charlie Feb 11 '21 at 07:09
  • You're so close; after treatment returns, use another swingutils.invokelater to close the window. By the way, the actionlistener happens on the EDT so there's no need to invokelater to createa the dialog. – Charlie Feb 11 '21 at 07:13
  • @Charlie I'm not sure to understand what you mean, how could you close a window using invokelater ? – VassiliKan Feb 11 '21 at 09:18
  • invokelater gets you on to the EDT; once there, its as simple as setting the window visibility to false then disposing of it. https://stackoverflow.com/questions/1234912/how-to-programmatically-close-a-jframe – Charlie Feb 13 '21 at 04:20

0 Answers0