Results 1 to 5 of 5
  1. #1
    eyal_____ is offline Member
    Join Date
    Jan 2012
    Rep Power

    Default Simple GUI problem

    Hello all.
    Im tring to create a button on panel, and by pressing it, A loop with a 1 sec delay starts. I want each time to change the label in the panel with the index of it.
    The problem is that is show only the last changed and not every change in the loop.
    attached 2 classes:
    run- main
    Test- the button
    Thank you...

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class run extends JFrame

    public static void main (String[] args) {
    JFrame frame=new JFrame ("test");
    frame.setLocation(100,500); // enter loc
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JPanel panel=new JPanel();

    JButton test=new JButton ("Test");
    panel.add (test);

    test.addActionListener(new Test(panel));


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Test extends JFrame implements ActionListener
    private JPanel _panel;
    public Test(JPanel panel){
    _panel= panel;
    public void actionPerformed(ActionEvent e) {
    JLabel _text1 =new JLabel();

    for (int i=1;i<=3;i++){
    _text1.setText("current # "+i);

    catch(Exception s){

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Simple GUI problem

    There exists a separate Thread, the EDT thread (Event Dispatch Thread); it does all the painting of the visible components as well the dispatching (read: running) of events. If it is running the code in your ActionListener (an event), it can't draw anything and the drawing commands are collected for later (when the EDT has time to draw again).

    kind regards,

    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Simple GUI problem

    You need to take turns with the JVM using the EDT thread. When it's your turn you change the label. When it's the JVMs turn it displays the changes you have made.
    You can do this by using a Timer. The Timer will give your code its "turn" so it can change the label. When your code in the timer is done and exits, then the JVM can show what you have changed.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Rep Power

    Default Re: Simple GUI problem

    See: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    You can use the Swing worker to create a Thread that loops and then sleeps for a second. Within the loop you can "publish" the counter so that the label can be updated on the GUI.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Simple GUI problem

    Moved from 'New to Java'

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Simple problem
    By beijct in forum New To Java
    Replies: 6
    Last Post: 12-03-2011, 06:26 AM
  2. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  3. Help with Simple If-Else problem!
    By M1NO in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 02:37 AM
  4. Simple problem
    By melovehockey in forum New To Java
    Replies: 8
    Last Post: 12-29-2010, 02:43 AM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 07:12 AM

Posting Permissions

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