I have a class called A and a class called T. When A clicks a button I want it to update something T. I am having issues doing this… any advice thanks.
Class A:
FileWriter fstream = new FileWriter("open.txt", true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("\n" +number1);
jLabel1.setText(number1);
out.close();
T t = new T();
t.refresh();
refresh is the function I want to be called to update class T
T:
public void refresh() {
File file = new File("open.txt");
try {
Scanner sc = new Scanner(file);
if (sc.hasNext()) {
jTextField2.setText(sc.next());
} else {
jTextField2.setText("Nothing to see here");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
That’s the function I want it to call. It just doesn’t update the text. I figure it’s something related to how I use the text file. Any ideas thanks.
You’re appending to your file:
but reading the first value:
which won’t ever change. I suspect that’s why you’re not seeing changes.
Do you need to write to that file ? Can’t you just call your
refresh()method (or an overloaded variant) with the data you’ve just written into the file ?