Results 1 to 5 of 5
  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default instruction apparently not executed

    On clicking my test button, I should see "first message" displayed, then, after a 3 second delay, "second message" displayed. Here's my code:

    Java Code:
        private class TestButtonWatcher implements ActionListener
        {
            public void actionPerformed(ActionEvent a)
            {
              messagesBox.setText("first message");
              delay(3000);
              messagesBox.setText("second message");
            }
        }
    
        private void delay(int interval)
        {
           try {Thread.sleep(interval);}
           catch(InterruptedException e) {System.exit(0);}
        }
    BUT: on clicking, "first message" is NOT displayed; there is a 3 second delay and then "second message" IS displayed.

    Any help appreciated; thanks.

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

    Default

    First message actually is displayed, but only after 3 sec have elapsed and only for a fraction of a second only.

    Never call Thread.sleep on the EDT, Swing's main thread -- it simply puts the whole GUI to sleep. If you need a delay, use a Swing Timer.

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Very grateful; thanks.
    What does "EDT" mean?

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

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

    Default

    A short example with a Swing Timer:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class ShowDelay {
    
       public static void main(String[] args) {
          final JLabel messageLabel = new JLabel(" ");
          JButton testBtn = new JButton("Test");
          testBtn.addActionListener(new ActionListener() {
             
             // button's action listener
             public void actionPerformed(ActionEvent e) {
                messageLabel.setText("first message");
                
                // timer, which has it's own ActionListener
                new Timer(3000, new ActionListener() {
                   public void actionPerformed(ActionEvent e) {
                      messageLabel.setText("second message");
                      ((Timer)e.getSource()).stop(); // turn timer off
                   }
                }).start(); // start Timer
             }
          });
          
          JPanel panel = new JPanel(new BorderLayout());
          panel.add(messageLabel, BorderLayout.NORTH);
          panel.add(testBtn, BorderLayout.CENTER);
    
          JFrame frame = new JFrame("ShowDelay");
          frame.getContentPane().add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }

Similar Threads

  1. Finally does not get executed
    By rahulkumar in forum New To Java
    Replies: 8
    Last Post: 04-06-2010, 05:30 PM
  2. Help with instruction
    By diegoyj in forum New To Java
    Replies: 1
    Last Post: 01-27-2010, 10:09 PM
  3. Will finally get executed...
    By AlmostAGuru in forum New To Java
    Replies: 1
    Last Post: 08-10-2009, 07:12 PM
  4. Replies: 0
    Last Post: 01-19-2008, 09:04 AM
  5. First Typed via video instruction
    By ZAXTHEGREAT in forum New To Java
    Replies: 15
    Last Post: 07-23-2007, 04:27 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
  •