I need to create a simple delay (3-5 seconds) so that a window can be read before being disposed of.
I've found a million examples on line, but they all seem to give me the same issue. My code:
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.JLabel;
public class GrantedPane extends JPanel {
private static final long serialVersionUID = 1L;
public GrantedPane() {
JFrame frame = new JFrame();
frame.setUndecorated(true);
frame.setSize(400, 150);
frame.setContentPane(this);
frame.setLocationRelativeTo(null);
frame.setSize(400, 80);
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
setBorder(new CompoundBorder(BorderFactory.createLineBorder(Color.WHITE), new EmptyBorder(20, 20, 20, 20)));
JLabel grantTitle = new JLabel(" Key Accepted ");
grantTitle.setForeground(Color.YELLOW);
grantTitle.setFont(new Font("Enter The Grid", Font.PLAIN, 32));
grantTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
add(grantTitle);
frame.setVisible(true);
delay(3);
frame.dispose();
}
@Override
protected void paintComponent(Graphics g) {
int w = getWidth(), h = getHeight();
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
GradientPaint gp = new GradientPaint(0, 0, Color.BLACK, 0, h, Color.GRAY);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
void delay (int sec){
try {
TimeUnit.SECONDS.sleep(sec);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
So the problem with this (and every other type of technique I've tried, including nested FOR loops in an act of desperation..) is that the windows never appears.
Im assuming it's because the sleep is taking place before the frame.setVisible()
can take effect. I've tried wait()
also with the same effect.
Can anybody help me out on this one?
EDIT--
Here's how I've instantiated it (within another Class). The screen which precedes this goes green then launches the "Accepted" window.The program has 8 Classes otherwise I'd just throw the whole thing up. Point to note, I am getting an 'unused' warning on the instantiation, but I'm not sure why.
void validCode() {
color1 = Color.GREEN;
color2 = Color.GREEN;
repaint();
GrantedPane granted = new GrantedPane();
}