Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How can I place a pause between repaints when handling a button event?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How can I place a pause between repaints when handling a button event?

    I am new to this forum and Java, so this is my first post.

    I have some gray JButtons and, when clicked, I want them to turn red for 500 milliseconds and then blue. I put an action listener on them and handled the event with Thread.sleep(500) between filling red and filling blue, and even a repaint() between those (to the panel where the buttons are displayed), but apparently the thread that does the repaints is postponed until after the thread that sleeps. It seems like there should be a simple solution, but I cannot think of one. What I have tried is this (and it just draws the buttons pressed as blue):

    [code = Java]
    private boolean handleControl(JButton buttonPressed)
    {
    buttonPressed.setBackground(Color.RED);
    gui.repaint();
    try
    {
    Thread.sleep(500);
    }
    catch(InterruptedException e)
    {
    }
    buttonPressed.setBackground(Color.BLUE);

    return true;
    }
    [/code]

    My question is, can this be done in a simple way, or do I have to create my own thread and put everything on it?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How can I place a pause between repaints when handling a button event?

    No, don't sleep the thread in a Swing application. Use a javax.swing.Timer to do most kinds of animation, including changing component colors.

    The 'Java' in your code tags should not be capitalized. Make sure I'm right, and edit that.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can I place a pause between repaints when handling a button event?

    Quote Originally Posted by GregBrannon View Post
    No, don't sleep the thread in a Swing application. Use a javax.swing.Timer to do most kinds of animation, including changing component colors.

    The 'Java' in your code tags should not be capitalized. Make sure I'm right, and edit that.
    Thanks, Greg! Here's a sample I was able to code that solves my problem using a Swing Timer as you suggested:

    [code = java]
    // *************** my EventHanlder *******************
    // * *
    // * Sets button color to red for specified time *
    // * then changes color to blue *
    // * *
    // * Morris Night *
    // ************************************************** *

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

    public class EventHandler implements ActionListener
    {
    private JButton buttonPressed;
    private Timer t;

    public EventHandler(Timer tt)
    {
    t = tt;
    }

    public void actionPerformed(ActionEvent e)
    {
    Object whatHappened = e.getSource();

    if ( whatHappened instanceof javax.swing.Timer)
    {
    buttonPressed.setBackground(Color.BLUE);
    return;
    }

    if ( whatHappened instanceof javax.swing.JButton)
    {
    buttonPressed = (JButton)whatHappened;
    buttonPressed.setBackground(Color.RED);
    t.start();
    return;
    }

    }
    }

    // ********* Application using EventHandler *****************
    // *** This version sets the pause to 1 second *************

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

    public class ColoredButtons extends JFrame
    {
    JButton button1;
    JButton button2;
    EventHandler eventHandler;
    Timer t;

    public ColoredButtons()
    {
    setSize(100,100);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    button1 = new JButton("one");
    button2 = new JButton("two");
    button1.setBackground(Color.GRAY);
    button2.setBackground(Color.GRAY);

    t = new Timer(1000, null); // ** pause set to 1 second **
    t.setRepeats(false);
    eventHandler = new EventHandler(t);

    t.addActionListener(eventHandler);
    button1.addActionListener(eventHandler);
    button2.addActionListener(eventHandler);

    getContentPane().add(button1,BorderLayout.WEST);
    getContentPane().add(button2,BorderLayout.EAST);
    }

    public static void main(String args[])
    {
    ColoredButtons coloredButtons = new ColoredButtons();
    coloredButtons.setVisible(true);
    }
    }
    [/code]

    --- Update ---

    By the way, the reason for this detail can be seen in an Applet I am polishing up.
    It can be viewed via this link: Navajo Lesson
    -- Morris --

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How can I place a pause between repaints when handling a button event?

    And maybe no spaces (in code tags). I only know how to do them right, so I'm always surprised at the number of ways they can be done wrong.

    Glad that you got it working and are learning something in the process.

Similar Threads

  1. event handling doubts
    By Rakshith in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:52 AM
  2. Problem netbeans gui button event handling
    By hiepa in forum AWT / Java Swing
    Replies: 7
    Last Post: December 3rd, 2012, 05:24 PM
  3. Event Handling - Best Practice?
    By ishtar in forum Java Theory & Questions
    Replies: 6
    Last Post: October 2nd, 2011, 08:52 AM
  4. Event Handling
    By maress in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 24th, 2011, 03:29 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM

Tags for this Thread