Reputation: 1216
I am trying to create a simple GUI form which has only 2 elements - a simple label and a button. The text displayed on button is 'Start'. The label is displaying 0 by default.
When I click Start button following actions shall take place:
I am developing my application in Netbeans.
As shown in the above diagram, there are 2 .java files
Contents of AGC.java are:
public class AGC extends javax.swing.JFrame
{
public AGC()
{
initComponents();
}
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new AGC().setVisible(true);
}
});
}
private javax.swing.JButton btnStartStop; // name of start stop button
private javax.swing.JLabel lblCounter; // name of the label
}
Contents of Main.java are:
public class Main
{
public static int count = 0;
public static boolean started = false;
}
I want to implement following logic:
private void btnStartStopMouseClicked(java.awt.event.MouseEvent evt)
{
if (Main.stared == true)
{
// logic to start counting
}
else
{
// logic to stop counting
}
}
My problem is this:
Kindly help. A working code would be very highly appreciated. Thanks in advance.
Jay
Upvotes: 3
Views: 2993
Reputation: 24626
Simply use a javax.swing.Timer, and make one ActionListener, to do this thing for you . Give me ten mins for a working code example :-)
Here is a sample program for further help :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UpdateWithTimer extends JFrame
{
private Timer timer;
private JButton startStopButton;
private JLabel changingLabel;
private int counter = 0;
private boolean flag = false;
private ActionListener timerAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
counter++;
changingLabel.setText("" + counter);
}
};
private ActionListener buttonAction = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (!flag)
{
startStopButton.setText("STOP TIMER");
timer.start();
flag = true;
}
else if (flag)
{
startStopButton.setText("START TIMER");
timer.stop();
flag = false;
}
}
};
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
JPanel contentPane = new JPanel();
changingLabel = new JLabel("" + counter);
contentPane.add(changingLabel);
startStopButton = new JButton("START TIMER");
startStopButton.addActionListener(buttonAction);
add(contentPane, BorderLayout.CENTER);
add(startStopButton, BorderLayout.PAGE_END);
timer = new Timer(1000, timerAction);
setSize(300, 300);
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new UpdateWithTimer().createAndDisplayGUI();
}
});
}
}
If you want the counter to again revert back to 0, on Stopping the Timer, simply add
else if (flag)
{
startStopButton.setText("START TIMER");
timer.stop();
flag = false;
counter = 0;
changingLabel.setText("" + counter);
}
this part to the buttonAction
's actionPerformed(...)
method.
Upvotes: 6
Reputation: 47627
Ok, so what I would suggest is to have a look at SwingWorker. You can extend SwingWorker and in the doBackground() execute a while(!isCancelled())
loop with a Thread.sleep(1000);
After the sleep execution, you can simply fire a property change that increments the value of your label.
Whenever you press the stop button, just cancel the current swing worker. When you press the start button, just execute()
the swing worker
Upvotes: 2