Use windowFocusListener on a countdown timer - java

I'm trying to make a countdown timer that only run when the window is on top of my screen.
I tried with this :
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TimerVisible extends JFrame implements WindowFocusListener{
static TimerVisible frame = new TimerVisible("chrono",2,1,3);//I set a random time
JTextArea display;
private Counter counter;
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane();
frame.pack();
frame.setVisible(true);
}
private void addComponentsToPane() {
display = new JTextArea();
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(500, 450));
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowFocusListener(this);
}
public TimerVisible(String name, int hours, int minutes, int secondes) {
super(name);
counter=new Counter(hours, minutes, secondes); //Counter is in secondes but is created with hours, minutes and seconds
}
public void windowGainedFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowGainFocus.");
try{
while(counter.getCounter()!=0){
Thread.sleep(1000);
displayMessage(counter.toString());
counter.decrement();
}
}
catch(InterruptedException exc){
System.exit(-1);
}
}
public void windowLostFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowLostFocus.");
}
private void displayMessage(String msg) {
display.append(msg+"\n");
System.out.println(msg);
}
}
When I run this program, it display the messages and the countdown on my terminal and not the window, but if I set the while loop under comment, it display correctly the message on the window. Is anybody got an idea why I got this difference?
Thank you

Your while loop is running on the Swing event thread, blocking it and preventing it from painting to the GUI or interacting with the user. Use a Swing Timer instead. Note that with a Swing Timer you won't have a while loop, but instead the actionPerformed will be called repeatedly until you stop the Timer.
Something like this could be close to working (code not tested)
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
public class TimerVisible extends JFrame implements WindowFocusListener{
private static final int TIMER_DELAY = 1000;
static TimerVisible frame = new TimerVisible("chrono",2,1,3);//I set a random time
JTextArea display;
private Counter counter;
Timer timer = null;
public static void main(String[] args) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addComponentsToPane();
frame.pack();
frame.setVisible(true);
}
private void addComponentsToPane() {
display = new JTextArea();
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(500, 450));
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowFocusListener(this);
}
public TimerVisible(String name, int hours, int minutes, int secondes) {
super(name);
counter=new Counter(hours, minutes, secondes); //Counter is in secondes but is created with hours, minutes and seconds
}
public void windowGainedFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowGainFocus.");
if (timer != null && timer.isRunning()) {
return;
}
timer = new Timer(TIMER_DELAY, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (counter.getCounter() <= 0) {
timer.stop();
} else {
displayMessage(counter.toString());
counter.decrement();
}
}
});
timer.start();
}
public void windowLostFocus(WindowEvent e) {
displayMessage("WindowFocusListener method called: windowLostFocus.");
}
private void displayMessage(String msg) {
display.append(msg+"\n");
System.out.println(msg);
}
}

Related

Java Event Dispatch Thread blocked

I'm trying to change text of JLabel during program execution. I know that program execution blocks EDT so I use timer to do this work. But timer starts only after the cycle finishes it's execution although timer.start() is located before cycle in source code.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
#SuppressWarnings("serial")
public class TestUpdate extends JFrame {
JLabel lab;
JButton btn;
public TestUpdate() {
super("Update test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
btn = new JButton();
btn.setText("Start test");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Timer tm = new Timer(1, new ActionListener() {
public void actionPerformed(ActionEvent e) {
lab.setText("Text is successfully changed");
lab.repaint();
}
});
tm.setRepeats(false);
tm.setInitialDelay(0);
tm.start();
long startTime=System.currentTimeMillis();
while (true) {
if (System.currentTimeMillis()-startTime >= 3000) break;
try {Thread.sleep(500);} catch (InterruptedException e1) {}
}
lab.setText("Three seconds elapsed");
btn.setEnabled(false);
}
});
add(btn,"South");
lab = new JLabel("Should be changed before 3 seconds elapsed");
lab.setHorizontalAlignment(SwingConstants.CENTER);
add(lab);
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {new TestUpdate(); } });
}
}
Timer event is executed by the EDT, so first event will be executed when current task (your action listener code with while loop) will complete. That is why you observe such behaviour.

Count Down Timer?

I make a simple application which contain a quiz questions and the user select an answer but i need your help in adding a count down timer in my app for 20 sec when this time is up it will transfer directly to the next question and when the user answer in time it will transfer to next question
Thank you
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.WindowConstants;
public class Countdown extends JFrame {
// Countdown 42 seconds
public static int counterValue = 42;
public static Timer timer;
public static JLabel label;
public Countdown() {
initGUI();
}
private void initGUI(){
BorderLayout thisLayout = new BorderLayout();
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
this.getContentPane().setLayout(thisLayout);
label = new JLabel();
label.setText(String.valueOf(counterValue));
this.getContentPane().add(label, BorderLayout.CENTER);
this.setTitle("Countdown Example");
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
Countdown countdown = new Countdown();
Countdown.timer = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
// =1 sec
Countdown.counterValue--;
Countdown.label.setText(String.valueOf(counterValue));
if(Countdown.counterValue == 0){
System.out.println("Counterdown ausgelaufen!");
// Timer stop
Countdown.timer.stop();
}
}
});
// Timer start
timer.start();
}
}
Taken from http://blog.mynotiz.de/programmieren/java-countdown-und-timer-am-beispiel-von-swing-1707/ ( German )
The "Android way" to do timed things is by posting Runnable tasks to a Handler.

Why does viewport changelistener get called multiple times

I've got a viewport, and I've attached a change listener to it. Whenever I scroll through my viewport, my change listener gets called about four times. I'm not sure how to avoid this; I only want the call to happen once?
There's no way around this, JViewport will fire several stateChanged events because it's providing notification about changes to a number of properties...
From the JavaDocs...
Adds a ChangeListener to the list that is notified each
time the view's size, position, or the viewport's extent size has
changed.
At this point, it's kind of hard to know what to suggest as we don't know what it is you are trying to achieve, however, if you have to use a ChangeListener, you could set up a coalescing mechanism. That is, rather then responding to each event, you basically wait until a long enough delay has occurred between events before responding to it...
For example...
public class DelayedChangeHandler implements ChangeListener {
private Timer timer;
private ChangeEvent last;
public DelayedChangeHandler() {
timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
stableStateChanged();
}
});
timer.setRepeats(false);
}
#Override
public void stateChanged(ChangeEvent e) {
last = e;
timer.restart();
}
protected void stableStateChanged() {
System.out.println("Finally...");
}
}
Basically, this is a ChangeListener implementation that uses a non-repeating javax.swing.Timer with a short delay. Each time stateChanged is called, the timer is restart. Finally, when the timer is allowed to "tick", it calls stableStateChanged indicating that enough time has passed since the last event was raised.
This assumes that you don't so much care about what caused the event, only that the event occured...
A runnable example...
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestViewport {
public static void main(String[] args) {
new TestViewport();
}
public TestViewport() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JPanel pane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(1000, 1000);
}
};
JScrollPane sp = new JScrollPane(pane);
sp.getViewport().addChangeListener(new DelayedChangeHandler());
sp.getViewport().addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(evt.getPropertyName());
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(sp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class DelayedChangeHandler implements ChangeListener {
private Timer timer;
private ChangeEvent last;
public DelayedChangeHandler() {
timer = new Timer(250, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
stableStateChanged();
}
});
timer.setRepeats(false);
}
#Override
public void stateChanged(ChangeEvent e) {
last = e;
timer.restart();
}
protected void stableStateChanged() {
System.out.println("Finally...");
}
}
}
You can try to use AdjustmentListener for gettign scroll event once, try next:
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.UnsupportedEncodingException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Example {
public static void main(String[] args) throws UnsupportedEncodingException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane pane = new JScrollPane(new JTextArea());
pane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(AdjustmentEvent e) {
if(e.getValueIsAdjusting()){
return;
}
System.out.println("vertical scrolled");
System.out.println("bar value = " + e.getValue());
}
});
frame.setContentPane(pane);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Here is another example.

Calling one JFrame from another using Timer without any buttons

Calling one JFrame from another using Timer without any buttons: time is decreased then open another JFrame without any buttons. Please help. Used in netbeans
Your question is very unclear, but the use of multiple frames is not recommended. As an alternative, consider a modeless dialog, shown below. The dialog's enclosed JOptionPane listens for a PropertyChangeEvent, while counting down from TIME_OUT using javax.swing.Timer. The JOptionPane button is convenient but not required.
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/**
* #see https://stackoverflow.com/a/12451673/230513
*/
public class JOptionTimeTest implements ActionListener, PropertyChangeListener {
private static final int TIME_OUT = 10;
private int count = TIME_OUT;
private final Timer timer = new Timer(1000, this);
private JDialog dialog = new JDialog();
private final JOptionPane optPane = new JOptionPane();
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new JOptionTimeTest().createGUI();
}
});
}
private void createGUI() {
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
timer.setCoalesce(false);
optPane.setMessage(message());
optPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
optPane.setOptionType(JOptionPane.DEFAULT_OPTION);
optPane.addPropertyChangeListener(this);
dialog.add(optPane);
dialog.pack();
frame.add(new JLabel(frame.getTitle(), JLabel.CENTER));
frame.pack();
frame.setVisible(true);
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
timer.start();
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (JOptionPane.VALUE_PROPERTY.equals(prop)) {
thatsAllFolks();
}
}
public void actionPerformed(ActionEvent e) {
count--;
optPane.setMessage(message());
if (count == 0) {
thatsAllFolks();
}
timer.restart();
}
private String message() {
return "Closing in " + count + " seconds.";
}
private void thatsAllFolks() {
dialog.setVisible(false);
dialog.dispatchEvent(new WindowEvent(
dialog, WindowEvent.WINDOW_CLOSING));
}
}

how to update a jLabel every time with a while loop with a delay

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int count = jSlider1.getValue();
int delay = jSlider2.getValue();
int valueOfSlider = jSlider2.getValue();
int valueOfSlider2 = jSlider1.getValue();
while (count > 0)
{
count--;
String count2 = ""+count;
jLabel3.setText(count2);
try {Thread.sleep(delay); }
catch (InterruptedException ie) { }
}
It will eventually show the final number on the jLabel but it does not incrementally update the number. any help
Swing is single-threaded. Therefore, long-running tasks should never take place in the EDT. This includes sleeping. Instead, use a javax.swing.Timer. This will delay in a background thread, and then post an action to be executed in the EDT.
See also:
How to Use Swing Timers
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public final class JLabelUpdateDemo {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame("Update JLabel Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(JTimerLabel.getInstance());
frame.setSize(new Dimension(275, 75)); // used for demonstration purposes
//frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Timer t = new Timer(1000, new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
int val = Integer.valueOf(JTimerLabel.getInstance().getText());
JTimerLabel.getInstance().setText(String.valueOf(++val));
}
});
t.start();
}
private static final class JTimerLabel extends JLabel{
private static JTimerLabel INSTANCE;
private JTimerLabel(){
super(String.valueOf(0));
setFont(new Font("Courier New", Font.BOLD, 18));
}
public static final JTimerLabel getInstance(){
if(INSTANCE == null){
INSTANCE = new JTimerLabel();
}
return INSTANCE;
}
}
}
This SSCCE imitates a counter that will count up from 0 every second (i.e. update the JLabel instance) until the application is terminated.
Your problem is that your doing something time consuming in an ActionPerformed callback, which executes in the event thread. In callbacks, you should do something quickly and return, even if that "something" is spawning a thread. The GUI can't update while you're occupying the event thread, it will only update after your callback returns.

Categories

Resources