Results 1 to 12 of 12
  1. #1
    carnotan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default 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. :(

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  3. #3
    carnotan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Last edited by DarrylBurke; 03-22-2011 at 04:36 PM.

  5. #5
    carnotan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    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 :(

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    carnotan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Netbeans isn't helping at all. It's doing it even more difficult.
    Only if you persist in using the visual designer, which is an advanced tool not targeted at beginners.

    And add components to a GUI before, rather than after, you setVisible(true)

    db

  9. #9
    carnotan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    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

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    What I'm trying to do (unsuccessfully) right now is to create a swing.Timer,
    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.

    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();
    	}
    }

  12. #12
    carnotan is offline Member
    Join Date
    Mar 2011
    Posts
    6
    Rep Power
    0

    Default

    Thanks a lot to all of you. If i can't do it with all that help then I really can't do it and I better become a hermit and start growing potatoes.
    By the way, I don't know what the expresion "Master Matisse" means, and I've just googled it. Perhaps my english is not as good as I thought.

Similar Threads

  1. Cant refresh JFrame, jTextArea1
    By yahyaaa in forum New To Java
    Replies: 9
    Last Post: 01-27-2011, 02:05 PM
  2. Replies: 13
    Last Post: 09-29-2010, 10:55 PM
  3. JFrame refresh problem
    By elliot in forum AWT / Swing
    Replies: 5
    Last Post: 02-03-2010, 11:18 PM
  4. [SOLVED] Adding JLabel to JFrame
    By mlfatty in forum AWT / Swing
    Replies: 3
    Last Post: 03-04-2009, 11:33 PM
  5. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 07:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •