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

    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.setResizable(false);
    frame.setVisible(true);
    frame.setAlwaysOnTop(true);
    frame.setLocation(100,500); // enter loc
    frame.setSize(600,80);
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JPanel panel=new JPanel();
    frame.add(panel);

    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();
    _panel.add(_text1);

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

    try{
    Thread.sleep(1000);
    }
    catch(Exception s){
    System.exit(0);
    }
    }
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,409
    Rep Power
    25

    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
    Posts
    1,236
    Rep Power
    7

    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 Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default Re: Simple GUI problem

    Moved from 'New to Java'

    db
    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, 05: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, 01:43 AM
  5. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06: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
  •