1

I'm working on this program and I ran into another issue. I have a Jframe with a JLabel that I wish for it to change text from one thing to another. However, when I try to do that it doesnt show me the text changing, rather the last text I set it to.

How do I get my JLabel to cycle through text SLOWLY?

I'm trying a wait method to make the program go slowly so I can see if I can make it cycle through, but that doesnt seem to be working.

it would be helpful if someone could edit my code or make their own example of how to do this, THANKS!

public class CreditGraphics {

    public String cardNum;
    public JFrame frame;
    public JPanel panel;
    public JLabel label;
    public JTextField text;

    public CreditGraphics() {
    synchronized(this){
    try {


        frame = new JFrame("HI");
        panel = new JPanel();
        label = new JLabel();

        text = new JTextField(16);

        panel.add(label);
        panel.add(text);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(500, 500));
        frame.getContentPane().add(panel);
        frame.pack();
        frame.setVisible(true); 

        wait(4000);
        label.setText("Hi");
        wait(4000);
        frame.revalidate();
        frame.repaint();
        label.setText("Hello");
        frame.revalidate();
        frame.repaint();


        text.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cardNum = text.getText();

            }

        });

         }
         catch(InterruptedException e) {
    e.printStackTrace();
    }}

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {


                new CreditGraphics();

               }




        });
    }

    public void checkCard(){


    }

}
NissimL
  • 148
  • 8
SoloSpirit
  • 87
  • 12

2 Answers2

3

As suggested by @trashgod use Swing Timer that is more suitable for swing application to perform a task once, after a delay or to perform a task repeatedly.

sample code:

private Timer timer;
...
label.setText("Hi");
// delay of 4 seconds
timer=new Timer(4000,new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        label.setText("Hello");
        // timer.stop(); // stop the timer if repeated mode is on
    }
});
timer.setRepeats(false); // you can turn-on it if needed
timer.start();

Note:

  • There is no need to call frame.repaint() and frame.revalidate() in this case.
  • Override getPreferredSize() to set the preferred size of the JPanel in case of custom painting.

sample code:

JPanel panel = new JPanel() {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(..., ...);
    }
};

read more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • thanks, this and what david posted look very helpful – SoloSpirit Jul 29 '14 at 17:15
  • So to clarify, I can use these methods to make a jlabel start by saying "Hello, welcome to my program" and then after a few seconds cycle to "Please enter in your data" (something along those lines without giving away my program ;)) – SoloSpirit Jul 29 '14 at 17:17
2

Do not use Thread.sleep() or wait() as it will freeze your Swing application.

Instead you should use a javax.swing.Timer

See the Java tutorial How to Use Swing Timers and Lesson: Concurrency in Swing for more information and examples.

DavidPostill
  • 7,734
  • 9
  • 41
  • 60