Results 1 to 5 of 5
  1. #1
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Question Help with JButton - how to make a button blink

    Hi!

    I am currently developing a game - poker dice - for a major project in matric. The project is coming along swimmingly :), but I still don't know how to make a JButton flash / blink. The intention is to attract the user's attention to the particular button.

    Please can someone help me with the code / method for this problem. I am coding in Java format.

    Thanks a million
    Last edited by geoffreybarwise; 05-21-2008 at 10:43 AM.

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    If you mean Jbutton blinks,

    do it in thread...
    freedom exists in the world of ideas

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    How about the use of a Timer. Define a array of colors, and with the timer tick change the background color of it.

  4. #4
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Thumbs up Array of colours

    Thanks for the help man, i'll definately try that

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes, why not. Try something like this. This is not what exactly you need, but the logic is there.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
    
    public class ButtonBlinker extends JFrame {
        
        private JButton btn;
        final Color[] colors = {Color.RED, Color.GREEN, Color.ORANGE};
        
        ButtonBlinker() {
            Container content = getContentPane();
            
            btn = new JButton("Blinker");
            
            content.add(btn);
            
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new ButtonBlinker().blinkTimer();
        }
    
        private void blinkTimer(){
            Timer tt = new Timer(2000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    btn.setBackground(colors[colorChoocer()]);
                }
            });
            tt.start();        
        }
        
        private int colorChoocer() {
            return new Random().nextInt(3);
        }
    }

Similar Threads

  1. Help with JButton and layout
    By adlb1300 in forum AWT / Swing
    Replies: 1
    Last Post: 12-25-2007, 08:33 AM
  2. Need help with JButton event
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:15 AM
  3. mouse over on JButton
    By gradon in forum Java Applets
    Replies: 1
    Last Post: 08-04-2007, 05:50 AM
  4. Few action in one Jbutton
    By kubiasty in forum New To Java
    Replies: 0
    Last Post: 07-25-2007, 10:19 AM
  5. Mouse over JButton
    By sandor in forum AWT / Swing
    Replies: 1
    Last Post: 05-17-2007, 09:15 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
  •