I found an example of how to do a repeating task in java. Now I want that the label's text changes every second. How do I do that?
I get the error: non-static method repeatingTask() cannot be referenced from a static context
Somehow the JLabel is not static but public static void main is of course static...
public class whathappens {
StartGUI startGUI = new StartGUI();
public void repeatingTask(){
getJLabel1().setText("Running: "+ new java.util.Date());
}
public static void main(String[] args) {
StartGUI.main(args);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
repeatingTask();
System.out.println("Running: " + new java.util.Date());
}
}, 0, 1000);
}
}
My idea was to call the method getJLabel1() from startGUI class to change the label within "whathappens" class
Your not clear about how you get your JLabel.
One way or another, your timer task must hold a reference to the JLabel. You can try something like that :
JLabel label = new JLabel();
Timer timer = new Timer(1000, e -> label.setText("Running: " + new Date());
timer.start();
This code uses javax.swing.Timer which is better than java.util.Timer in your context as if fires event within the https://en.wikipedia.org/wiki/Event_dispatching_thread. And it is in this thread that GUI code must be updated.
Related
i am usin the timer class and in the docs it is written that i should import javax.swing.Timer to use it. does it mean that i can not use it in my normal java file? because i tried the below code, and it displays nothing:
static ActionListener timeStampListener = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.out.println("action listener");
for (int i = 1; i <= logfile.getTotalLines(); i++) {
System.out.println("Engine Time(ms): " +
logfile.getFileHash().get(i).getTimeStampInSec());
}
}
};
Timer t = new Timer(2, timeStampListener);
t.setRepeats(true);
t.start();
the problem is your main thread exist before starting timer thread .since your application is non-gui use util.Timer instead Swing.Timer ..if you want to work this code using swing timer then add a swing component .add new jframe() and see it's working ..you don't need swing.timer use util timer .
static ActionListener timeStampListener1 = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("hi");
}
};
public static void main(String[] args) {
new JFrame(); //add this line
Timer t = new Timer(2, timeStampListener1);
t.setRepeats(true);
t.start();
}
or give some times by adding thread.sleep to timer to on and see it's working
Timer t = new Timer(2, timeStampListener1);
t.setRepeats(true);
t.start();
Thread.sleep(1000);
this is how can u use util timer for this
imports
import java.util.Timer;
import java.util.TimerTask;
code
public static void main(String[] args) {
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("action listener");
for (int i = 1; i <= logfile.getTotalLines(); i++) {
System.out.println("Engine Time(ms): "
+ logfile.getFileHash().get(i).getTimeStampInSec());
}
}
}, 500, 2);
}
No, it means that you should import which Timer class you will use. When you import javax.swing.Timer you specifies Timer class in javax.swing package. You can use it in your java file.
Anyway, have you tried not using static keyword with your timeStampListener?
I am trying to build a simple GUI clock with multithreading.My purpose is making two identical exampl clock window.
public class JavaApplication9 {
public static void main(String[] args) {
JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}//end main}
class TextClockWindow extends JFrame {
private JTextField timeField; // set by timer listener
private JButton listener;
public TextClockWindow() {
// GUI
timeField = new JTextField(6);
timeField.setFont(new Font("sansserif", Font.PLAIN, 48));
JButton button1 = new JButton("Action");
add(button1);
button1.addActionListener((ActionListener) listener);
ActionListener listener=new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){setBackground(Color.red );
}
};
Container content = this.getContentPane();
content.setLayout(new FlowLayout());
content.add(timeField);
this.setTitle("My_simple_clock"); this.pack();
// Create a 1-second timer and action listener for it.
// Specify package because there are two Timer classes
javax.swing.Timer t = new javax.swing.Timer(1000,
new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
timeField.setText("" + h + ":" + m + ":" + s);
}
});
t.start();
}
This is code without multithreading.But wheni trying to use Runnable some error occured.
In method main Non Static variable cannot be referenced in a static context.
My code with multithreading:
public class MyClock{
public static void main(String[] args) {
Runnable r=new Clocks();
Thread n=new Thread(r);
n.start();
}
public class Clocks implements Runnable {
public Clocks() {}
public void run() {JFrame clock = new TextClockWindow();
clock.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clock.setVisible(true);
}
please help find the reason why it is not work.runnable is writing correctly....
Is there a reason why you declared Clocks class as inner class ?
Move your Clocks class outside MyClock class and remove the public qualifier if you're declaring the class in the same file. It will start working.
I'm trying to implement a timer using one thread and print it on a JButton using another thread.
my class for time is like this:
public class Time extends Thread
{
int counter = 0;
public String currentTime = new String();
public String printFormat(int second)
{
return String.format("%d:%d", second/60, second%60);
}
synchronized public void count(int minute) throws InterruptedException
{
minute *= 60;
while(minute >= 0)
{
wait(1000);
minute--;
currentTime = printFormat(minute);
System.out.println(currentTime);
}
}
and my main thread is like this:
button.setText(time.currentTime);
what is wrong with this piece of code?
"if you can explain it using java swing timer , I would appreciate that"
If you want to use a javax.swing.Timer do the following, it really simple.
The same way you set a ActionListener to a button, you do the same for the timer. Except instead of the button firing the event, it's fired by the timer, every duration period you set for it.
In the case of a clock like timer, you would set it to 1000,
indication do something every 1000 milliseconds.
In this particular
example, I just set the text of the button with a count value that I
increment by one every time the timer event is fired. Heres the Timer code
Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
button.setText(String.valueOf(count));
count++;
}
});
timer.start();
As you can see it' pretty simple
You can run this example
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ButtonTimer {
private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {
Timer timer = new Timer(1000, new ActionListener(){
public void actionPerformed(ActionEvent e) {
button.setText(String.valueOf(count));
count++;
}
});
timer.start();
JFrame frame = new JFrame();
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new ButtonTimer();
}
});
}
}
If you want help trying to figure out your current code, consider posting a runnable program we can test out. So we can see where you're going wrong.
Here's a tutorial on Concurrency With Swing
I have a Swing JFrame. If I create a new JFrame in a new thread during the program execution where will be the EDT ? In the current thread of the last JFrame window or in the first window.
EDIT:
Thanks for your answers.
I understand them and i'm ok with them. I know that we don't must create swing object elsewhere that the EDT but I meet problem.
I explain; I developed a JAVA application for create and extract archive like winrar. You can create several arhive in same time with multi-thread. And recently, I wanted add an information status during the archive creation in the form of JprogressBar in a new JFrame at every creation. But my problem is for generate a communication in the new status frame and the thread who create archive. That's why, I create the JFrame in the archive thread for update the progress bar currently.
But like i could read it in divers information source and on your answers/comments, it's against java swing and performance; I can't create swing object elsewhere that the EDT.
But then, how should I solve my problem ?
The EDT - the event dispatch thread - is separate from any concrete GUI component, such as a JFrame.
Generally you should create all GUI components on the EDT, but that does not mean they own the EDT, nor does the EDT own the components.
To create two JFrames, both on the EDT, you can do the following:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame1 = new JFrame("Frame 1");
frame1.getContentPane().add(new JLabel("Hello in frame 1"));
frame1.pack();
frame1.setLocation(100, 100);
frame1.setVisible(true);
}
});
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame2 = new JFrame("Frame 2");
frame2.getContentPane().add(new JLabel("Hello in frame 2"));
frame2.pack();
frame2.setLocation(200, 200);
frame2.setVisible(true);
}
});
}
The Event Dispatch Thread is fixed. It doesn't get reassigned just because you created a Swing object on another thread (which you should never do anyway).
that should be very simple, if all events all done in current EDT, then EDT doesn't exists, another Queue is possible to stating,
from Swing's Listeners,
by caling code wrapped into invokeLater / invokeAndWait,
but safiest (and best for me) would be javax.swing.Action
output
run:
Time at : 19:35:21
There isn't Live EventQueue.isDispatchThread, why any reason for that
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that
Time at : 19:35:21
Calling from EventQueue.isDispatchThread
Calling from SwingUtilities.isEventDispatchThread
Time at : 19:35:21
Calling from EventQueue.isDispatchThread
Calling from SwingUtilities.isEventDispatchThread
Time at : 19:35:51
There isn't Live EventQueue.isDispatchThread, why any reason for that
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that
Time at : 19:36:21
There isn't Live EventQueue.isDispatchThread, why any reason for that
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that
Time at : 19:36:51
There isn't Live EventQueue.isDispatchThread, why any reason for that
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that
Time at : 19:37:21
There isn't Live EventQueue.isDispatchThread, why any reason for that
There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that
BUILD SUCCESSFUL (total time: 2 minutes 17 seconds)
from code:
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.*;
import javax.swing.*;
public class IsThereEDT {
private ScheduledExecutorService scheduler;
private AccurateScheduledRunnable periodic;
private ScheduledFuture<?> periodicMonitor;
private int taskPeriod = 30;
private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
private Date dateRun;
public IsThereEDT() {
scheduler = Executors.newSingleThreadScheduledExecutor();
periodic = new AccurateScheduledRunnable() {
private final int ALLOWED_TARDINESS = 200;
private int countRun = 0;
private int countCalled = 0;
#Override
public void run() {
countCalled++;
if (this.getExecutionTime() < ALLOWED_TARDINESS) {
countRun++;
isThereReallyEDT(); // non on EDT
}
}
};
periodicMonitor = scheduler.scheduleAtFixedRate(periodic, 0, taskPeriod, TimeUnit.SECONDS);
periodic.setThreadMonitor(periodicMonitor);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
isThereReallyEDT();
JFrame frame1 = new JFrame("Frame 1");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.getContentPane().add(new JLabel("Hello in frame 1"));
frame1.pack();
frame1.setLocation(100, 100);
frame1.setVisible(true);
}
});
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame2 = new JFrame("Frame 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.getContentPane().add(new JLabel("Hello in frame 2"));
frame2.pack();
frame2.setLocation(200, 200);
frame2.setVisible(true);
isThereReallyEDT();
}
});
}
private void isThereReallyEDT() {
dateRun = new java.util.Date();
System.out.println(" Time at : " + sdf.format(dateRun));
if (EventQueue.isDispatchThread()) {
System.out.println("Calling from EventQueue.isDispatchThread");
} else {
System.out.println("There isn't Live EventQueue.isDispatchThread, why any reason for that ");
}
if (SwingUtilities.isEventDispatchThread()) {
System.out.println("Calling from SwingUtilities.isEventDispatchThread");
} else {
System.out.println("There isn't Live SwingUtilities.isEventDispatchThread, why any reason for that ");
}
System.out.println();
}
public static void main(String[] args) {
IsThereEDT isdt = new IsThereEDT();
}
}
abstract class AccurateScheduledRunnable implements Runnable {
private ScheduledFuture<?> thisThreadsMonitor;
public void setThreadMonitor(ScheduledFuture<?> monitor) {
this.thisThreadsMonitor = monitor;
}
protected long getExecutionTime() {
long delay = -1 * thisThreadsMonitor.getDelay(TimeUnit.MILLISECONDS);
return delay;
}
}
Bellow is the code for the simplest GUI countdown. Can the same be done in a shorter and more elegant way with the usage of the Swing timer?
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class CountdownNew {
static JLabel label;
// Method which defines the appearance of the window.
public static void showGUI() {
JFrame frame = new JFrame("Simple Countdown");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("Some Text");
frame.add(label);
frame.pack();
frame.setVisible(true);
}
// Define a new thread in which the countdown is counting down.
public static Thread counter = new Thread() {
public void run() {
for (int i=10; i>0; i=i-1) {
updateGUI(i,label);
try {Thread.sleep(1000);} catch(InterruptedException e) {};
}
}
};
// A method which updates GUI (sets a new value of JLabel).
private static void updateGUI(final int i, final JLabel label) {
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
label.setText("You have " + i + " seconds.");
}
}
);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showGUI();
counter.start();
}
});
}
}
Yes you SHOULD use a Swing Timer. You SHOULD NOT, use a util Timer and TimerTask.
When a Swing Timer fires the code is executed on the EDT which means you just need to invoke the label.setText() method.
When using the uitl Timer and TimerTask, the code DOES NOT execute on the EDT, which means you need to wrap your code in a SwingUtilities.invokeLater to make sure the code executes on the EDT.
And that is way using a Swing Timer is shorter and more elegant than your current approach, it simplifies the coding because to code is executed on the EDT.
You could make it a little more elegant by using Timer with an appropriate TimerTask.
Yes, use a timer. updateGUI would be the code for the timer task, but it will need some changes as you won't be able to pass in i for each call since you just get a run() method.