I want a text to be displayed in a blinking label, but nothing works for me.
So I tried something easier: a button that starts an account from 0 to 10 with a for
when i
is 5
shows the text in the label.
But it is that even that does not work. Displays the text when i
reaches its end that is 9
. In the debug if it enters the if i ==5
but does not display the text in the label until the for
ends.
This is my code:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class pruebaText {
private JFrame frame;
private JLabel lblText;
int cont = 0;
private JLabel lblText5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
pruebaText window = new pruebaText();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public pruebaText() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
lblText = new JLabel("");
lblText.setBounds(89, 66, 46, 14);
frame.getContentPane().add(lblText);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
while (cont<10) {
Thread.sleep(1000);
cont++;
System.out.println(cont);
if (cont==5) {
lblText.setText("hola");
}
}
}catch (Exception e1) {
// TODO: handle exception
System.out.println(e1);
}
}
});
btnNewButton.setBounds(89, 197, 89, 23);
frame.getContentPane().add(btnNewButton);
lblText5 = new JLabel("");
lblText5.setBounds(341, 38, 46, 14);
frame.getContentPane().add(lblText5);
}
}
This is the real method that I'm trying to work on a button
public static void mostarTexto() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
//Para ver la evulocion de i en Console de eclipe
System.out.println(i);
if (i / 2 == 0) {
lblTexto.setText("hola");
} else {
lblTexto.setText(" ");
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}