Java Swing In two classes, where to put if statements and new executors of actions?

A clean beginner question is here. I am changing the code and am now stuck in the following issues:

My timer is called in the upper class. But my int class is called in the next class.

Whenever I add an if statement:

if (count == 2) { t.stop();} 

I get errors because int is in the class below and t (timer) in the class above.

  • How can I add an if statement if there are two classes?

  • And if I want to add a new actionlistener for the button to stop the count, do I put this in a class higher or lower?

Code here

Thanks in advance

+2
java if-statement swing actionlistener where
source share
2 answers

Since ClockListener is a nested class (below), the incoming instance (top) can access the private fields of the listener. If you have a link to an instance of ClockListener ,

 ClockListener cl = new ClockListener(); 

you can use it to initialize the timer

 Timer t = new Timer(1000, cl); 

and you can use it in your test:

 if (cl.count == 2) { t.stop(); } 

Appendix: For reference, here is a variant of your program that uses JToggleButton to control the timer. As suggested earlier , you used Calendar to minimize Timer drift. Like you, I abandoned the approach as inconsequential in a low-resolution application.

 import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JToggleButton; import javax.swing.Timer; /** @see https://stackoverflow.com/questions/5528939*/ class ClockExample extends JFrame { private static final int N = 60; private static final String stop = "Stop"; private static final String start = "Start"; private final ClockListener cl = new ClockListener(); private final Timer t = new Timer(1000, cl); private final JTextField tf = new JTextField(3); public ClockExample() { t.setInitialDelay(0); JPanel panel = new JPanel(); tf.setHorizontalAlignment(JTextField.RIGHT); tf.setEditable(false); panel.add(tf); final JToggleButton b = new JToggleButton(stop); b.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if (b.isSelected()) { t.stop(); b.setText(start); } else { t.start(); b.setText(stop); } } }); panel.add(b); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.setTitle("Timer"); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } public void start() { t.start(); } private class ClockListener implements ActionListener { private int count; @Override public void actionPerformed(ActionEvent e) { count %= N; tf.setText(String.valueOf(count)); count++; } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { ClockExample clock = new ClockExample(); clock.start(); } }); } } 
+8
source share

Resolution of his area. Make t public (not recommended) or create another timer in the second class, which you will go through the constructor of the second class when it is called. It will refer to the original timer.

+1
source share

All Articles