Results 1 to 6 of 6
- 03-08-2009, 04:51 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
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 :( )
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?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("?"); } } }
- 03-08-2009, 05:29 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, what you have tried with swing timer?
- 03-08-2009, 05:37 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
I tried to include the following function in code:
But with no success. I mean some text apeared in console but button didn't refresh.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 ...?"); } }
- 03-08-2009, 05:49 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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(); }
- 03-08-2009, 06:12 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 3
- Rep Power
- 0
Thank you :)
- 03-09-2009, 02:17 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome.
Did you get the point in my code segment and yours. Basically how timer bond with the action. Do you know anything about inner classes?
Similar Threads
-
Inside a Timer thread loop,how to refresh a JTable in swing
By neha_negi in forum Threads and SynchronizationReplies: 3Last Post: 09-04-2009, 01:45 AM -
[SOLVED] Swing Timer issue
By Doctor Cactus in forum New To JavaReplies: 6Last Post: 03-03-2009, 12:25 PM -
strange refreshing behavior
By diggitydoggz in forum New To JavaReplies: 4Last Post: 12-27-2008, 04:51 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM -
Bug in refreshing jsp
By anki1234 in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-31-2007, 07:09 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks