JButton ActionPerformed does not open the Frame before ending all events

0

I have the following problem:

When I click on the JButton I want to open a Waiting Frame that prompts the user to wait for a while and in the meantime the program will process the query methods that take some time. However, by clicking on Jbutton it executes all methods in the actionPerformed before opening the Frame with the "standby screen."

Here I put a generic code example that represents the form I'm implementing.

here is 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 here below is the class with Jbutton that should open the standby screen while executing 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();
    }
}
    
asked by anonymous 22.02.2018 / 13:27

1 answer

0

It worked. The changes look 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();
    }
    
22.02.2018 / 13:43