How to thread to wait for another class to be completed and continue the code?

Asked

Viewed 210 times

1

As well as described in the code I need a solution to give a pause in main until it waits for some call to continue the code of the class main. I know this will be solved with thread, but so far I haven’t found a way to use it for that. I also think it has to do with the EDT, but I don’t know much about thread, then I can’t do it.


import javax.swing.JFrame;

public class Main extends JFrame{

    public Main(){
        setSize(800,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }



    public static void main(String[] args) {
        Main frame = new Main();
        Painel painel = new Painel();
        painel.setVisible(true);

        frame.add(painel);

        // Aguardar comando do Painel e continuar o código seguinte:

        System.out.println("OK");
        frame.remove(painel);

    }

}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Painel extends JPanel implements ActionListener {

    JButton button = new JButton("Prosseguir main");

    public Painel(){
        button.setVisible(true);
        add(button);
        button.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==button) {
            System.out.println("Escrever agora o OK no console e remover painel pela Main");
        }

    }
}
  • Please provide a [mcve] so you can run the code and test the problem.

  • Edited article

  • 1

    I’d wear something like Countdownlatch or a Lock shared. main creates an object of the type CountDownLatch, passes it to the builder of Painel and waiting (await) for Countdown executed in the ActionListener. Moreover it is good practice (see that question) use SwingUtilities.invokeLater to create / display / modify AWT / Swing components in EDT.

  • I like this Countdownlatch, it worked. Referring to invokelater so far I did not understand the exact function of it, I read that it has something to do with Eventqueue, but at the moment for me it is a bit confusing, because I have not yet had problems in the way I am writing the code, each panel has its actionListener and its Keylistener and so far I am not merging 2 panels in the same window and I am using the remove(Component) to remove the panel when leaving it.

  • The point is, just because it didn’t cause any trouble doesn’t mean it’s right. Swing needs to be relaxed, and for this, you need to start the application inside the EDT, through this method. It may not be a problem right now, but even more considering you’re starting to implement threads, you should change your code to the right shape, because there ahead you will have a problem and may not be able to solve precisely because you did not want to dispose the application.

  • This amendment would not even be drastic in its code, just by all that is in its method main within the invokeLater(). I’ve referred you a lot of good links to read in this answer, including in one of them demonstrates how to use this thread.

  • Yes, I understand, but the fact that I haven’t done that so far was because invokelater was bugging the Drawings, when I put it in the main, then I try to apply it again and try to find out where the bug comes from.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.