Results 1 to 12 of 12
- 03-22-2011, 04:11 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Cannot refresh a JLabel on a JFrame
First of all thanks in advance for all the help given. I've never programmed a GUI so I'm really lost. And I'm about to kill myself.
Here's the thing:
I need to show a list of words, each of them for one second. To do that I create a new JFrame and add a JLabel to it. Then I take an arrayList of Strings and use the JLabel.setText method to change it. The problem that I can only see the last one of the words, and I've tried to repaint() the JFrame every step, but it does nothing. Here's the code:
Java Code:private void comezarActionPerformed(java.awt.event.ActionEvent evt) { JFrame ventana= new JFrame(); ventana.setSize(400,400); JLabel etiqueta= new JLabel(); ventana.add(etiqueta); ventana.setVisible(true); for (int i=0;i<=14; i++){ etiqueta.setText(escogidas.get(i)); ventana.repaint(); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(Experimento1.class.getName()).log(Level.SEVERE, null, ex); } } }
Any help would be really apprecieated. I 'm really desperate. :(
- 03-22-2011, 04:15 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Now is a good time to read
How to Use Swing Timers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- 03-22-2011, 04:28 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Either I'm not understanding how timers are used or I really don't need to use them at all. To wait the specified time I have :
Thread.sleep(1000);
The waiting is not the problem, the problem is the refreshing.
Thanks for the tip.
- 03-22-2011, 04:31 PM #4
Also Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Never call Thread.sleep(...) on the EDT.
dbLast edited by DarrylBurke; 03-22-2011 at 04:36 PM.
- 03-22-2011, 04:49 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Well, it was my Cognitive Science teacher who told me to use that method. So either he has no f***** idea (which is not only possible but likely ) or it could be done with the sleep method.
I'm totally aware that the program will "freeze" exactly 15 seconds, and that's exactly what it should do. It will be a program to study the human memory. Once the user starts it, it has to be completed.
I really appreciate the effort you are making to help, but I don't get to the point you are trying to make. I'm feeling a little stupid right now...
So, unless there is another reason for which I shouldn't use the sleep method I'm still stuck at the same point :(
-
If you call Thread.sleep(...) on the main Swing thread, you not only pause the program, you completely freeze the program -- it can't update graphics, it can't interact at all with the user even if they want to click the box to close it. For this reason and others, it's never a good idea to call Thread.sleep(...) on the Swing EDT. Please take the time and effort to read the link that Darryl has provided as it will tell you all this and more.
- 03-25-2011, 01:30 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
I'm really reading all the documentation that you told me, but I can't make any sense out of it. I've never programmed a GUI and even less try to go multithreading. Netbeans isn't helping at all. It's doing it even more difficult.
What I'm trying to do (unsuccessfully) right now is to create a swing.Timer, but I don't know how to create or launch an event whenever the text of the JLabel is changed.
Java Code:private void comezarActionPerformed(java.awt.event.ActionEvent evt) { final JFrame ventana =new JFrame(); Timer tiempo = new Timer (1000,new ActionListener() { public void actionPerformed(ActionEvent e) { ventana.repaint(0); } }); tiempo.start(); JLabel etiqueta = new JLabel(); ventana.setSize(400, 400); ventana.setVisible(true); ventana.getContentPane().add(etiqueta); for (int i=1;i<=14;i++){ etiqueta.setText(escogidas.get(i)); } }
Any help( or gun to kill myself) will be very much appreciated.
- 03-25-2011, 02:29 PM #8
Only if you persist in using the visual designer, which is an advanced tool not targeted at beginners.Netbeans isn't helping at all. It's doing it even more difficult.
And add components to a GUI before, rather than after, you setVisible(true)
db
- 03-25-2011, 02:36 PM #9
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Thanks for the tip. I still don't know how to manage the actionListeners.
I HAVE to do a GUI. If you really know any other way to design it easily I would fall in love with you :3
Thanks a lot again.
- 03-25-2011, 02:43 PM #10
You've already been given two links to sections of the Swing tutorial. There's plenty more you can learn from the other sections -- and it'll be easier than trying to master Matisse.
db
- 03-25-2011, 02:44 PM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Well, when learning a new concept create a simple test program to test the concept. Once you understand the concept you implement the knowledge into your real program. This simple program is called a SSCCE.What I'm trying to do (unsuccessfully) right now is to create a swing.Timer,
If you want to update the text of a label, then you need access to the label in the ActionListener. Here is an example of a SSCCE:
Java Code:import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class TimerTime extends JFrame implements ActionListener { JLabel timeLabel; public TimerTime() { timeLabel = new JLabel( new Date().toString() ); getContentPane().add(timeLabel, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e) { timeLabel.setText( new Date().toString() ); } public static void main(String[] args) { TimerTime frame = new TimerTime(); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setVisible(true); int time = 1000; javax.swing.Timer timer = new javax.swing.Timer(time, frame); timer.setInitialDelay(1); timer.start(); } }
- 03-25-2011, 03:02 PM #12
Member
- Join Date
- Mar 2011
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Cant refresh JFrame, jTextArea1
By yahyaaa in forum New To JavaReplies: 9Last Post: 01-27-2011, 02:05 PM -
Draw text over background image using JLabel and JFrame
By vytska in forum New To JavaReplies: 13Last Post: 09-29-2010, 10:55 PM -
JFrame refresh problem
By elliot in forum AWT / SwingReplies: 5Last Post: 02-03-2010, 11:18 PM -
[SOLVED] Adding JLabel to JFrame
By mlfatty in forum AWT / SwingReplies: 3Last Post: 03-04-2009, 11:33 PM -
help me with JFrame and JLabel
By michcio in forum New To JavaReplies: 5Last Post: 11-20-2007, 07:44 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks