Hi,
I'm super new to Java, and am desperately in need of some help.
I'm making a little java desktop application where I basically have a grid of 4 JButtons ( 2 x 2 grid), and I need the background colour of individual JButtons to change, and after one second, change back to the original colour (the game I'm trying to make is Simon, where you have to follow a pattern of buttons that light up).
I have a vector that contains randomly generated numbers in the range of 1 to 4, and I want to be able to get each element from the vector and get the corresponding button to change to a different colour for one second (for example, if the vector contains 2 4 1, I would want button 2 to change, then button 4 to change, then button 1 to change).

Is this possible or is there a better way to go about doing this with something other than JButtons? How do I implement this?

Also, I'm running Mac OS X, which apparently (based on some things I've read on forums) doesn't like JButtons background changing (I think it's because of system look and feel), how can I change this so it works on mac?

Thank you in advance for any help :)

Yeah, JButton doesn't show a background change too well on several Look And Feels, you could use a JLabel just fine though. Make sure to setOpaque(true) so the background color is shown.

For the timing, you might get by with a simple Swing Timer that sets each color after a tiny delay. Here's a small example of that with a single label. See comments in code

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class TimerBlink extends javax.swing.JFrame {

    public TimerBlink() {
        initComponents();
        setBounds(100,100,200,150);

        // create a new timer with a delay of 500ms that uses Blinker
        Timer blinkTimer = new Timer(500, new Blinker());
        blinkTimer.start();
    }

    /** this class' actionPerformed() will get called each time the Timer fires */
    class Blinker implements ActionListener{
        boolean on=false;

        public void actionPerformed(ActionEvent e) {
            // blink the label background on and off
            label1.setBackground( on ? Color.BLUE : null);
            on = !on;
        }
    }

    /** everything below here is just inconsequential UI setup */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        label1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        label1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        label1.setText("1");
        label1.setOpaque(true);
        getContentPane().add(label1);
        label1.setBounds(20, 20, 90, 40);

        pack();
    }// </editor-fold>

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TimerBlink().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel label1;
    // End of variables declaration

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.