Java - updating a GUI done in Swing
I am trying to create a simple GUI form that only has 2 elements - a simple label and a button. The text displayed on the button is "Start". By default, the label displays 0.
When I click the Start button, the following should be done:
- The counter starts increasing by 1 s 0 every 1 second.
- The text displayed on the Start button changes to Stop.
- When I click on the same button again (now you see the caption as Stop), the increment stops.
- The text on the button changes to "Start". Etc...
I am developing my application in Netbeans.
As shown in the diagram above, there is a 2.java file
AGC.java content:
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
}
Main.java content:
public class Main
{
public static int count = 0;
public static boolean started = false;
}
I want to implement the 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:
- How to update lblCounter every 1 second?
- What logic should I implement to start the 1 second timer and how to access the lblCounter in this method?
Kindly help. Working code would be very much appreciated. Thanks in advance.
Jay
source to share
Just use javax.swing.Timer and make an ActionListener to do it for you. Give me ten minutes for a working code example :-)
Here's a sample program for further assistance:
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 go back to 0 again, when you stop the timer just add
else if (flag)
{
startStopButton.setText("START TIMER");
timer.stop();
flag = false;
counter = 0;
changingLabel.setText("" + counter);
}
this part to the method buttonAction
actionPerformed(...)
.
source to share
Ok, so I would suggest looking at SwingWorker . You can extend SwingWorker and in doBackground () loop a while(!isCancelled())
using Thread.sleep(1000);
After sleep you can simply trigger a property change that increments your label value.
Whenever you press the stop button, simply cancel the current working swing. When you press the start button, just execute()
a swing worker
source to share