Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create listener for Calendar in thread
    primarykey
    data
    text
    <p>I've tried to find the solution for two days and still don't know what to do. </p> <p>Here's the situation: I have runnable class where variable of Calendar type doing some increment over time through infinite loop (there's a simplified model in a <em>source 1</em>). Next I have a GUI, where I'm starting new thread (<em>source 2</em>).</p> <p>When a variable 'time' changed, I want to to some math and change some labels on my GUI.</p> <p>As I understand, I should create PropertyChangeListener and this is a place, where I have a problem: I really don't understand how to do it. I've done following things: updated my TimeLoop class to <em>Source 3</em>. I've created listener listed on <em>Source 4</em> (also simplified). And here comes the question: how and where I should initialize listener? Or where am I wrong? Thanks for your answers.</p> <p>P.S. The only Idea I had was (Source 5) and that of course can't work.</p> <hr> <p><strong>Source 1 (example of my runnable class):</strong></p> <pre><code>public class TimeLoop implements Runnable { private Calendar time = Calendar.getInstance(); @Override public void run() { try { time.set(1997, 11, 27, 00, 00, 00); while (true) { time.add(time.HOUR, 1); Thread.sleep(1000); } } catch (InterruptedException ex) { System.out.println("Interrupted error"); } } } </code></pre> <p><strong>Source 2 (source from my GUI class):</strong></p> <pre><code>public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new GUI().setVisible(true); } }); Thread trTime = new Thread(new TimeLoop()); trTime.setName("TimeLoop"); trTime.start(); } </code></pre> <p><strong>Source 3 (Edited runnable class):</strong></p> <pre><code>public class TimeLoop implements Runnable { private Calendar time = Calendar.getInstance(); private PropertyChangeSupport support = new PropertyChangeSupport(this); public void updateTime() { Calendar oldValue = time; time.add(time.HOUR, 1); Calendar newValue = time; support.firePropertyChange("time", oldValue, newValue); } public void addListener(PropertyChangeListener listener) { support.addPropertyChangeListener(listener); } @Override public void run() { try { while (true) { time.set(1997, 11, 27, 00, 00, 00); updateTime(); Thread.sleep(1000); } } catch (InterruptedException ex) { System.out.println("Interrupted error"); } } } </code></pre> <p><strong>Source 4 (Listener):</strong></p> <pre><code>public class TimeListener implements PropertyChangeListener { @Override public void propertyChange(PropertyChangeEvent pce) { System.out.println(pce.getPropertyName() + " has new value); } } </code></pre> <p><strong>Source 5 (Wrong code):</strong></p> <pre><code>public static void main(String args[]) { Thread trTime = new Thread(new TimeLoop()); trTime.setName("TimeLoop"); trTime.start(); TimeLoop tLoop = new TimeLoop(); TimeListener tTistener = new TimeListener(); tLoop.addListener(tTistener); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload