I currently have a program that is reading in live information from a text file which is constantly being updated, which is in its own separate class "Reciever". For this, I have just made an int to increase over time for simplicity. All the relevant numbers are added to the int x. The value for x is updated by a TimerTask every second in the example I provided for simplicity. Now I have a GUI class that when I click a button in the GUI, it runs the Reciever class. I want the JLabel in my GUI class to constant print the updated value x from the Reciever class which I do not know how to do so.
Here is my reciever class which when run, updates a value every second.
package DataParsing;
import java.util.Timer;
import java.util.TimerTask;
public class Reciever {
private static int x;
private static Timer timer = new Timer();
public static void main(String[] args) {
x = 1;
timer.schedule(new RecallLoop22(), 1, 1000);
}
public int getX() {
return this.x;
}
public void setX(int b) {
this.x = b;
}
}
class RecallLoop22 extends TimerTask {
private Reciever recieve = new Reciever();
private static int i = 0;
public void run() {
recieve.setX(i);
i++;
System.out.println(recieve.getX() + " Recieve X");
}
}
Here is my GUI class which when I press the button, I want it to print the constantly updating value into the JLabel.
package DataParsing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class NGUI implements ActionListener {
private Reciever recieve = new Reciever();
private JLabel label = new JLabel("");
private DatabaseSorter sort = new DatabaseSorter();
private JButton button1 = new JButton("Foo");
public NGUI() {
label.setText("B: " + recieve.getX());
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.add(label);
panel.add(button1);
button1.addActionListener(this);
panel.setBorder(BorderFactory.createEmptyBorder(50, 50, 50, 50));
panel.setLayout(new FlowLayout());
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
String[] arr = new String[0];
new NGUI();
}
public void actionPerformed(ActionEvent e) {
String[] arr = new String[0];
if (e.getActionCommand().equals("Foo")) {
recieve.main(arr);
System.out.println("Recieve X value " + recieve.getX());
label.setText("B: " + recieve.getX());
button1.setBackground(Color.GREEN);
}
}
}
I'm honestly not sure what to do from here. All of the posts I've found related to this just don't quite hit what I'm looking for nor are they close enough for me to derive from them.
Any help would be greatly appreciated!
This is the sort of thing (taking your code) Hovercraft Full Of Eels means:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Container;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.*;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.List;
import javax.swing.*;
public class F extends JFrame {
JLabel progress;
class Receiver extends SwingWorker<Void, String> {
private static Timer timer = new Timer();
private static DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
@Override
protected Void doInBackground() {
timer.schedule(new RecallLoop22(), 1, 1000);
return null;
}
protected void process(List<String> chunks) {
progress.setText(chunks.get(0));
}
private class RecallLoop22 extends TimerTask {
public void run() {
publish(dtf.format(LocalTime.now()));
}
}
}
private void setGui() {
try {
setLocation(0, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
progress = new JLabel("0");
cp.add(progress);
new Receiver().execute();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
EventQueue.invokeAndWait(() -> {
F f = new F();
f.setGui();
f.setSize(200, 200);
f.setVisible(true);
});
} catch (Exception e) {
e.printStackTrace();
}
}
}