Results 1 to 6 of 6
  1. #1
    qoqosz is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default Timer in Swing app - refreshing label

    Hello,
    my problem is very simple - I'd like to write an app with label text and start and quit buttons. When pressing the start button the label should update it's content every second. Here's my code (really poor :( )

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.Timer;
    
    import java.lang.*;
    
    public class TimerExample extends JFrame implements ActionListener
    {
    	JButton startButton = new JButton("Start");
    	JButton quitButton 	= new JButton("Quit");
    	JLabel 	myText 		= new JLabel("Timer");
    	JPanel 	bottomPanel = new JPanel();
    	JPanel 	holdAll 	= new JPanel();
    
    
    	public TimerExample()
    	{
    		bottomPanel.setLayout(new FlowLayout());
    		bottomPanel.add(startButton);
    		bottomPanel.add(quitButton);
     
    		holdAll.setLayout(new BorderLayout());
    		holdAll.add(bottomPanel, BorderLayout.SOUTH);
    		holdAll.add(myText, BorderLayout.CENTER);
     
    		getContentPane().add(holdAll, BorderLayout.CENTER);
     
    		startButton.addActionListener(this);
     
    		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		
            quitButton.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				System.exit(0);
    			}
    		}
    		);
    	}
    
    	public static void main(String[] args)
    	{
    		TimerExample myApplication = new TimerExample();
     
    		myApplication.setLocation(400, 300);
    		myApplication.setSize(250, 120);
    
    		myApplication.setVisible(true);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		if (e.getSource() == startButton)
    		{
    			myText.setText("new text");
    		} else
    		{
    			myText.setText("?");
    			System.out.println("?");
    		}
    	}
    }
    I'm new to java and simply don't know where and how to put some code here to update myText label every 1 second. Could you help?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    qoqosz is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    I tried to include the following function in code:
    Java Code:
    public void actionPerformed(ActionEvent event)
    	{
    		if (event.getSource() == startButton)
    		{
    			for (int i = 0; i < 10; i++)
    			{
    				Timer timer = new Timer(10000, this);
    				timer.setInitialDelay(10000);
    				timer.start();
    				System.out.println(i);
    				myText.setText("Lolo" + i);
    			}
    		}
    		else
    		{
    			myText.setText("E ...?");
    		}
    	}
    But with no success. I mean some text apeared in console but button didn't refresh.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    This is wrong. You have to perform the action within the timer. Look at the following example code segment.

    And also you have start the timer in a loop 1o times. What the point of starting the timer in that way.

    Java Code:
        public void labelRefresh(){
            // Initialize the timer, delay as you specified
            Timer timer = new Timer(10000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                      // Do your process here.
                      // Refreshing the label or whatever you want
                }
            });
            timer.start();
        }

  5. #5
    qoqosz is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you :)

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  2. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 12:25 PM
  3. strange refreshing behavior
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 12-27-2008, 04:51 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 07:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •