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();
}
}