Results 1 to 7 of 7
  1. #1
    the_rider9 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, I wonder if you're not going to reply to answers in this thread just you never did in your previous thread.

  5. #5
    the_rider9 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  6. #6
    the_rider9 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  7. #7
    the_rider9 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default

    I figured it out, I was passing a JFrame as a reference where a reference from the class which extends the JFrame should've been passed, TimerFrame. All is good thank you for your great aid.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2011, 04:37 AM
  2. how to separate this code in another class
    By Jhovarie in forum AWT / Swing
    Replies: 5
    Last Post: 03-01-2011, 05:08 PM
  3. Separate file for main and class
    By eel in forum New To Java
    Replies: 12
    Last Post: 09-18-2010, 08:24 AM
  4. How to make swing.Timer as a separate class
    By nethz13 in forum New To Java
    Replies: 9
    Last Post: 04-18-2010, 09:14 AM
  5. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 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
  •