Results 1 to 4 of 4

Thread: jLabel

  1. #1
    Matty is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default jLabel

    Hi guys, im new to the thread posting and am sort of a beginner in java. I programme on Netbeans IDE 6.1

    I mainly teach myself, starting from simple codes and then i grow them..

    Anyway, i can do many things with java but i somehow can't manage to do something so simple, a Clock.

    I have a panel with a label on it and a button.

    When the button is pressed the clock/timer should start counting..

    Now here comes the problem, my code does what it is supposed to do but doesnt show it to the screen, eg.

    for(int regen=0; regen<10; regen++)
    {
    try{Thread.sleep(1000);} catch(Exception e){}
    jLabel1.setText(""+regen);
    }

    when the button is clicked the above code is run and the GUI seems to freeze for 10 seconds then display jLabel1's text as "9". That is, it skips all the other numbers. i have tried using jLabel.repaint(); but that hasnt worked.

    I would really apprecaite it if someone could help me, show me something really simple.

    Thank you very much.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    If you are executing that code on the GUI's thread, the GUI can't do anything until you are finished using the thread. That would 10*1 second later. When the GUI gets the thread back, the text in the label is "9" and is shown.
    You need to start your own thread to do the updating of the label and the sleep. That would let the GUI have its thread back to be able to notice the changes to the label.

  3. #3
    Matty is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    okay i have researched a bit about threads so you i dont need the whole code, but please if you clould tell me how to go about letting the GUI have its own Thread? What you are saying makes sense, thank you very much Norm

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    how to go about letting the GUI have its own Thread
    returning from the listener code will release the GUI's thread.

Similar Threads

  1. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 07:03 AM
  2. JLabel .setActionCommand
    By stevemcc in forum AWT / Swing
    Replies: 1
    Last Post: 03-28-2008, 04:16 AM
  3. Problems with JLabel 2
    By geork in forum New To Java
    Replies: 2
    Last Post: 02-03-2008, 08:40 PM
  4. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 01:55 PM
  5. JLabel
    By Freddie in forum AWT / Swing
    Replies: 2
    Last Post: 05-29-2007, 02:19 PM

Posting Permissions

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