Results 1 to 7 of 7
- 07-05-2011, 03:00 AM #1
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Modifying JLabel within separate class
I have been searching the net for an answer with no answer found.
Basically, I am trying to display a chronometer by displaying it on a JLabel which is on a JFrame.
I have a JFrame class named TimerFrame, and a separate class Timer, which holds all the methods and variables needed for the chronometer.
The GUI has two buttons, start/stop and lap/reset and a JLabel which displays the current time.
I want a method to update the JLabel so to constantly display the current time (chronometer) and to stop when the user presses stop.
The place where I am stuck, is how to constantly update the JLabel from within the Timer Class. A thread is running but the access to the JLabel is unclear.
Please help out, I don't need code just an explanation on how to access the TimerFrame from another class.
Thanks in advance.
- 07-05-2011, 03:02 AM #2
Your Timer class needs a reference to the GUI class. Then all it has to do is call a method in the GUI to update the label.
-
Give the class with the JLabel a public method, say public void setTime(String timeString) and inside the method set the label with the String. Then the class with the Timer needs a reference to the GUI class, perhaps passed in its constructor.
-
Hm, I wonder if you're not going to reply to answers in this thread just you never did in your previous thread.
- 07-06-2011, 05:19 PM #5
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Should the reference be a reference to the frame or to specifically the JLabel? I need to access the the JFrame class within a run() method located in a separate class, which cannot accept parameters.
Its unclear how the JLabel is modified without accessing the JFrame itself.
- 07-06-2011, 05:20 PM #6
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Should the reference be a reference to the frame or to specifically the JLabel? I need to access the the JFrame class within a run() method located in a separate class, which cannot accept parameters.
Its unclear how the JLabel is modified without accessing the JFrame itself.
- 07-06-2011, 06:07 PM #7
Member
- Join Date
- May 2011
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Drawing an object in my canvas class, the object is created in a separate class
By Hornfreak in forum AWT / SwingReplies: 3Last Post: 05-02-2011, 04:37 AM -
how to separate this code in another class
By Jhovarie in forum AWT / SwingReplies: 5Last Post: 03-01-2011, 05:08 PM -
Separate file for main and class
By eel in forum New To JavaReplies: 12Last Post: 09-18-2010, 08:24 AM -
How to make swing.Timer as a separate class
By nethz13 in forum New To JavaReplies: 9Last Post: 04-18-2010, 09:14 AM -
Should I separate my code into separate files?
By Inks in forum New To JavaReplies: 0Last Post: 03-26-2009, 12:12 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks