Results 1 to 2 of 2
  1. #1
    cmplin is offline Member
    Join Date
    Apr 2016
    Rep Power

    Default continue changing color of Jbutton

    I am designing a game which will show different color every 2 second. I am using setBackground color on JButton, and try to use a delay loop to accomplish it, but it seems to always display the last color. Can anyone help? thanks

    class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    int rint;
    int k = 0;
    if (e.getSource() == jb1) {
    long start = System.currentTimeMillis();
    int delay = 1000;
    s1 = jcbb.getSelectedIndex(); // based on selected value to define how many different random color to display
    for(int i=0; i < s1+1; i++){
    rint = (int)(Math.random()*6);
    jl12.setBackground(clist[rint]); // clist is a list of pre-defined Colors
    created[i] = rint;
    while (System.currentTimeMillis()- start < delay){
    // empty loop


  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: continue changing color of Jbutton

    First, use [code][/code] to show us code. It will give you indenting and formatting.

    Second, use the Random class instead of Math.random(). It's a lot simpler to use.

    Third, use a Timer to do the changing. A never ending while loop will eat CPU cycles and destroy your application:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    public class ColorButton extends JButton {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> {
                JFrame frame = new JFrame();
                frame.getContentPane().setLayout(new FlowLayout());
                frame.getContentPane().add(new ColorButton("This is my button"));
                frame.setSize(400, 400);
        private int previous = -1;
        public ColorButton(String text) {
            Color[] colors = {, Color.white,, Color.yellow};
            Timer timer = new Timer(2000, e -> {
                int i = -1;
                while (i == previous) {
                    i = new Random().nextInt(4);
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Changing the color of windows
    By Zarah in forum New To Java
    Replies: 2
    Last Post: 05-02-2014, 09:33 AM
  2. changing int with jbutton
    By Frank_21 in forum New To Java
    Replies: 19
    Last Post: 05-23-2013, 06:13 AM
  3. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 08:35 AM
  4. Changing background color
    By nikkka in forum New To Java
    Replies: 4
    Last Post: 03-12-2011, 06:54 AM
  5. Color-changing model
    By higuchi in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 08:29 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