I have a simple GUI that can save and get some data out of an .doc file.
When i press a save button, i have a label that says "Succes" or "Error" via label.setText();
Update: The code is meant to be ran in a FXMLDocumentController (built på SceneBuilder)
I want the label to go back to being empty ("") after 3 seconds..
I have tried:
try {
Thread.sleep(1000);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
but is it like, that the sleep-function freezes the whole GUI so i can't interact with it while it's sleeping. How do i set up an timer that does not affect usability? :)
Create a TimerTask which is started after 3 seconds. This TimerTask has to execute the code which uses gui components via Platform.runLater(new Runnable())
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
label.setText("");
}
});
}
}, 3000);
class runnable implements Runnable {
private Object obj;
public runnable(Object obj)
{
this.obj = obj;
}
public void run() {
try {
Thread.sleep(3000);
this.obj.setText("");//right here just execute your method
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello from a thread!"); //your code here
}
}
public class Test {
public static void main(String[] args){
Object myObject;
(new Thread(new runnable(myObject))).start();
}
}
You could launch another thread to handle this. Just do your processing in the other thread. Sleep for 3 seconds then clear the lbl.
Here is an example showing how it works:
class runnable implements Runnable {
Test test;
public runnable(Test test)
{
this.test = test;
}
public void run() {
try {
Thread.sleep(3000);
this.test.test = "test2";
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello from a thread!");
}
}
public class Test {
public String test = "test";
public static void main(String[] args) throws InterruptedException{
Test test = new Test();
System.out.println(test.test);
(new Thread(new runnable(test))).start();
Thread.sleep(4000);
System.out.println(test.test);
}
}
************************************************************UPDATE*************************************************************
class runnable implements Runnable {
Test test;
public runnable(Test test)
{
this.test = test;
}
public void run() {
try {
Thread.sleep(3000);
this.test.label.setText("This is a test.");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Hello from a thread!");
}
}
public class Test {
public String test = "test";
JLabel label = new JLabel("Test");
JFrame frame = new JFrame();
JButton button = new JButton();
public static void main(String[] args) throws InterruptedException{
Test test = new Test();
test.label.setText("Test");
test.button.setText("Test Button");
test.button.setSize(50, 50);
test.frame.setSize(500, 500);
test.frame.add(test.button);
test.frame.add(test.label);
test.frame.setVisible(true);
test.button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}
});
(new Thread(new runnable(test))).start();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With