I can't tell without seeing it.
Post the code and the full text of the error message.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I can't tell without seeing it.
Post the code and the full text of the error message.
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
Line 36 - teste cannot be resolved to a variable.package cupTimer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.GregorianCalendar; import javax.swing.* ; public class myTimer { public static void main(String[] args) throws InterruptedException { int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { long tempoEmMs = System.currentTimeMillis() ; GregorianCalendar today = new GregorianCalendar(); GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0); long thisTimeInMs = cup.getTimeInMillis(); long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000); int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY)); int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE)); int secondsLeft = (60 - today.get(GregorianCalendar.SECOND)); String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds"); System.out.println(teste); } }; new Timer(delay, taskPerformer).start(); JLabel label = new JLabel("Image and Text", JLabel.CENTER); JFrame frame = new JFrame("Teste"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); label.setText(teste); frame.pack(); frame.setVisible(true); } }
The variable: teste is out of scope where the error happens. It's defined inside a method and is not defined where the code tries to use it. Move the setText statement into the method where teste is defined.
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
I can't, in that way label won't be resolved to a variable. (it's defined in other method...)
@@Edit
I tried putting all the JPane inside the main method, but that won't work either (no error code, just not working.)
Last edited by Kerooker; April 27th, 2014 at 05:38 PM.
Post the code and the full text of the error messages.
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
package cupTimer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.GregorianCalendar; import javax.swing.* ; public class myTimer { public static void main(String[] args) throws InterruptedException { int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { long tempoEmMs = System.currentTimeMillis() ; GregorianCalendar today = new GregorianCalendar(); GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0); long thisTimeInMs = cup.getTimeInMillis(); long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000); int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY)); int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE)); int secondsLeft = (60 - today.get(GregorianCalendar.SECOND)); String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds"); System.out.println(teste); } }; new Timer(delay, taskPerformer).start(); JLabel label = new JLabel("Text", JLabel.CENTER); label.setText(teste); JFrame frame = new JFrame("Teste"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } }
label.setText(teste); - Teste cannot be resolved to a variable
I didn't find a way to call the text in another method...
The spelling is wrong. The variable is named: teste, not Testelabel.setText(teste); - Teste cannot be resolved to a variable
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
No, the two calls are with the same name. The variable is named 'teste', and I tried to call 'teste', just copied the wrong name from the error, sorry.
The setText() call must be in the same scope with where teste is defined.
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
I tried doing it:
But then, "label cannot be resolved".package cupTimer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.GregorianCalendar; import javax.swing.* ; public class myTimer { public static void main(String[] args) throws InterruptedException { int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { long tempoEmMs = System.currentTimeMillis() ; GregorianCalendar today = new GregorianCalendar(); GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0); long thisTimeInMs = cup.getTimeInMillis(); long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000); int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY)); int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE)); int secondsLeft = (60 - today.get(GregorianCalendar.SECOND)); String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds"); System.out.println(teste); label.setText(teste); } }; new Timer(delay, taskPerformer).start(); JLabel label = new JLabel("Text", JLabel.CENTER); JFrame frame = new JFrame("Teste"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } }
Post the full text showing the statement where the error happened and line number."label cannot be resolved".
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
label cannot be resolved - line 24
What statement is on line 24?
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
label.setText(teste);
label needs to be defined BEFORE it is used. Move its definition ahead of where it is used.
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
It's not possible, or else I would need to cal the label after the main method ended, and it's not possible to declare the Jframe before the timer...
I need a way to use a variable from the main mathod in the JFrame method.
Please explain and post the error messagesit's not possible to declare the Jframe before the timer.
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
I tried this
But it didn't work, without any errors, it just terminate the program without anything in the console.package cupTimer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.GregorianCalendar; import javax.swing.* ; public class myTimer { public static void main(String[] args) throws InterruptedException { int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { long tempoEmMs = System.currentTimeMillis() ; GregorianCalendar today = new GregorianCalendar(); GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0); long thisTimeInMs = cup.getTimeInMillis(); long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000); int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY)); int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE)); int secondsLeft = (60 - today.get(GregorianCalendar.SECOND)); String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds"); System.out.println(teste); JLabel label = new JLabel (teste, JLabel.CENTER); JFrame frame = new JFrame("Teste"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } }; new Timer(delay, taskPerformer).start(); } }
I tried putting the JFrame inside the main method.
Post the full text of the error messages if you need help with them.it didn't work, without any errors,
The actionPerformed() method is called once a second. Do you want a new frame created once a second?
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
No error message, just not working.
I want the frame to be updated every second with the new information, so it can be counted down second to second.
Updating an existing frame is different from creating a new frame every second.I want the frame to be updated every second
You are now back to the problem discussed in post#24 - Thread sleep
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
But if I leave the frame out of the timer method, I cannot call the values in the main method, as they're in different methods
Should I put the frame before the rest?
yes
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)
package cupTimer; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.GregorianCalendar; import javax.swing.* ; public class myTimer { public static void main(String[] args) throws InterruptedException { JLabel label = new JLabel("Text", JLabel.CENTER); JFrame frame = new JFrame("Teste"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); int delay = 1000; //milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { long tempoEmMs = System.currentTimeMillis() ; GregorianCalendar today = new GregorianCalendar(); GregorianCalendar cup = new GregorianCalendar(2014,5,12,0,0); long thisTimeInMs = cup.getTimeInMillis(); long daysLeft = ((thisTimeInMs - tempoEmMs) / 86400000); int hoursLeft = (24 - today.get(GregorianCalendar.HOUR_OF_DAY)); int minutesLeft = (60 - today.get(GregorianCalendar.MINUTE)); int secondsLeft = (60 - today.get(GregorianCalendar.SECOND)); String teste = ("Missing:" + daysLeft + " Days " + hoursLeft + " Hours " + minutesLeft + " Minutes " + secondsLeft + " Seconds"); System.out.println(teste); label.setText(teste); } }; new Timer(delay, taskPerformer).start(); } }
Error at label.setText(teste);
Cannot refer to a non-final variable label inside an inner class defined in a different method
Make it finalCannot refer to a non-final variable
If you don't understand my answer, don't ignore it, ask a question.
Kerooker (April 27th, 2014)