Jlabel setText when i loop for = 5

0

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

    }

}
    
asked by Piter Don 25.04.2018 в 16:21
source

1 answer

0

If you're looking to modify the interface in a loop, and see the changes as that loop is executed. Then the code does not have to be in the same handler since everything will be executed first and then the user interface will be updated.

To achieve what you want you can start a new thread that is responsible for iterating the state of text visibility, and every iteration that invokes the text change in the event dispatch thread .

For example:

btnNewButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        new Thread(new Runnable() {
            public void run() {
                boolean b = true;
                while(true) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                    }
                    b = !b;
                    final boolean mostrar = b;
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            if(mostrar) {
                                lblText.setText("Hola");
                            } else {
                                lblText.setText("");
                            }
                        }
                    });
                }
            }
        }).start();
    }
});

Note that if you leave it as handler of a button, a new thread will be created each time it is used.

    
answered by 25.04.2018 в 17:01