I am very new to java programming and SwingGUI. I am currently trying to creatte a clicker game using swing. I need to update the text of a jlabel constantly to a pre-defined variable. I have looked around and cannot seem to find a clear answer on how to update the GUI or component constantly or after a set interval.
Asked
Active
Viewed 282 times
-2
-
Show some code, what have you tried? – phil652 Apr 19 '17 at 19:43
-
So what happened when you tried the suggestion? – camickr Apr 21 '17 at 02:27
-
i used a swing timer and it worked perfectly but because i am using swing gui and cannot alter the variables i returns the error " a non static variable cannot be referenced from a static form" – Lewis Solarin Apr 21 '17 at 18:46
1 Answers
1
Use a Swing Timer
.
Read the section from the Swing tutorial on How to Use Timers for more information and working examples.
Another simple example: Update a Label with a Swing Timer
-
Timer timer = new Timer(1000, this); timer.setInitialDelay(1); timer.start(); { //Code Here } Is This The Correct Format? – Lewis Solarin Apr 19 '17 at 19:50
-
@LewisSolarin, you tell me? Try it! We are not here to write the code for you, only point you in the right direction. You were given a link to a tutorial and another question that contains working example. Read the tutorial. Download and test the code from the linked example. You learn by trying. If you still have a problem, then post an [mcve] demonstrating the problem. And don't post the code in a comment since it is not readable. Post the ~MCVE` in the question. – camickr Apr 19 '17 at 19:55