Jbutton’s actionperformed does not open the Frame before finishing all events

Asked

Viewed 85 times

0

I have the following problem:

By clicking on Jbutton I want to open a Waiting Frame that asks the user to wait a few moments and meanwhile the program will process the query methods that take some time. However when clicking on Jbutton it executes all methods in actionPerformed before opening the Frame with the "wait screen".

Here I put a generic example of code that represents the way I’m implementing.

here the class that mounts the Wait screen.

public class WaitSplash extends JFrame{
    public void showComponente() {
        JFrame frame =this;        
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        JLabel jLabel = new JLabel();
        jLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/aguarde.gif")));
        panel.add(jLabel);
        panel.setBackground(new  Color( 221, 236, 239 ));
        frame.add(panel);
        frame.setSize(350, 121);
        frame.setUndecorated(true);
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        centralizarComponente(frame);
    }
    public WaitSplash() throws HeadlessException {
        showComponente();
    }
    public void centralizarComponente(JFrame frame) {
        Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dw = frame.getSize();
        frame.setLocation((ds.width - dw.width) / 2, (ds.height - dw.height) / 2);
    }
    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
    }
}

and down here the class with Jbutton that should open the wait screen while performing the method .

public class NewClass {
  public void genericMethod(){
      JFrame frame = new JFrame("JFrame Example");
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());
      JButton button =
      button= new JButton(new AbstractAction("Botao") {
            @Override
            public void actionPerformed(ActionEvent e) {
                WaitSplash wait;
                wait = new WaitSplash();
                wait.setVisible(true);
                wait.requestFocus();
                metodoQualquer();
                wait.dispose();
            }
        });
      panel.add(button);
      frame.add(panel);
      frame.setSize(300, 300);
      frame.setVisible(true);
    }

  public void metodoQualquer(){
      try {
      Thread.sleep(10000);
     } catch (Exception e) {}

  }
    public static void main(String s[]) {
     new NewClass().genericMethod();
    }
}
  • You want to make a splashScreen?

  • a splashScreen that lasts while actionPerformed methods are running.

  • 1

    Try solution 2 of that answer: https://answall.com/a/120435/28595

  • It worked. Thank you!

1 answer

0


It worked. The modifications stayed like this:

public class NewClass {

    public void genericMethod() {
        JFrame frame = new JFrame("JFrame Example");
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        JButton button
                = button = new JButton(new AbstractAction("Botao") {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        ativarMetodos();
                    }
                });
        panel.add(button);
        frame.add(panel);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }

    public void metodoQualquer() {
        try {
            Thread.sleep(10000);
        } catch (Exception e) {
        }

    }

    public static void main(String s[]) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new NewClass1().genericMethod();
            }
        });

        //   new NewClass().genericMethod();
    }

    public void ativarMetodos() {
        SwingWorker worker = new SwingWorker() {
            @Override
            protected Object doInBackground() throws Exception {
                WaitSplash wait;
                wait = new WaitSplash();
                wait.setVisible(true);
                wait.requestFocus();
                metodoQualquer();
                wait.dispose();

                return null;
            }

            @Override
            protected void done() {

                super.done();
                try {
                    try {
                        get();
                    } catch (InterruptedException ex) {
                        Logger.getLogger(NewClass1.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ExecutionException ex) {
                        Logger.getLogger(NewClass1.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (Exception e) {
                }
            }
        };
        worker.execute();
    }

Browser other questions tagged

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