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?
– user28595
a splashScreen that lasts while actionPerformed methods are running.
– Vinicius Silva
Try solution 2 of that answer: https://answall.com/a/120435/28595
– user28595
It worked. Thank you!
– Vinicius Silva