JS_VIPER
JS_VIPER

Reputation: 141

How to dynamically change JLabel's contents

The below program, basically calls a JFrame's frame.repaint() to fill inside the frame dynamically. On the same lines, I would want to have 2 labels( West & East) of the frame and have the labels dynamically changing. I have tried lotta things like, Jlabel label.repaint(),label.removeAll(),etc, But it doesn't work. I have left the code, clean so that you can fill in...

JFrame frame=new JFrame();
frame.setSize(512, 512);
frame.add(image1);
frame.setVisible(true); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


start_sum=0;

while(true)
{
    frame.repaint();
    try {
        Thread.sleep(sleep_for_each_rotation);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    start_sum+=radian/divide_size;             //Some calculation stuff
    if(start_sum>=360)
        start_sum=0;

}

Upvotes: 0

Views: 3176

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347204

From the looks of you code, you are blocking the Event Dispatching Thread (EDT).

The EDT is responsible for (amongst other things), processing repaint events. This means that if you block the EDT, nothing can be repainted.

The other problem you have is, you should never create or modify any UI component from any thread other then the EDT.

Take a look at Concurrency in Swing for more details.

The following example simply uses a javax.swing.Timer, but from the sounds of things, you'll probably find Swing Worker more useful

public class TestLabelAnimation {

    public static void main(String[] args) {
        new TestLabelAnimation();
    }

    public TestLabelAnimation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel left;
        private JLabel right;

        public TestPane() {
            setLayout(new BorderLayout());

            left = new JLabel("0");
            right = new JLabel("0");

            add(left, BorderLayout.WEST);
            add(right, BorderLayout.EAST);

            Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    left.setText(Integer.toString((int)Math.round(Math.random() * 100)));
                    right.setText(Integer.toString((int)Math.round(Math.random() * 100)));
                }
            });

            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();

        }

    }

}

Upvotes: 2

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

Just use JLabel.setText (String) method like in the following example:

public static void main (String[] args) throws Exception {
    final JLabel label = new JLabel (String.valueOf (System.currentTimeMillis()));

    JFrame frame = new JFrame ();
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout (new BorderLayout ());
    frame.getContentPane().add (label, BorderLayout.CENTER);
    frame.pack ();
    frame.setVisible(true);

    while (true)
    {
        final String newText = String.valueOf (System.currentTimeMillis ());

        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override
            public void run() {
                label.setText (newText);
            }
        });

        Thread.sleep (100L);
    }
}

Upvotes: -1

Related Questions