Results 1 to 7 of 7
  1. #1
    behrk2 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default JButton onClick change color background

    Hey guys,

    I have an array of six colors. I also have a button. What I want to do is, everytime the button is clicked, have its background color change to the next index of the array. Once the button has changed to all six colors, I want it to repeat.

    Here is the code I have:

    Java Code:
    private void gboard_row1_btn1ActionPerformed(java.awt.event.ActionEvent evt) {
            Color colors[] = new Color[6];
    
            int i=0;
            
            colors[0] = Color.red;
            colors[1] = Color.orange;
            colors[2] = Color.yellow;
            colors[3] = Color.green;
            colors[4] = Color.blue;
            colors[5] = new Color(138, 43, 226);
    
        while( i<6 ) {
                gboard_row1_btn1.setBackground(colors[i]);
                i++;
        }
        }
    My loop is screwed up and I don't know how I would go about implementing a solution for this. Does anyone have any ideas?

    Thanks!

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

    Default

    Do you know how to handle getSource() in ActionEvent.

    Java Code:
    evt.getSource()
    The above line is quite similar to your code.

  3. #3
    behrk2 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks for your help, with that information, I came up with this...

    Java Code:
    private void gboard_row1_btn1ActionPerformed(java.awt.event.ActionEvent evt) {
            
        Object source = evt.getSource();
        Color color = getBackground();
        if (source == new Color(255, 255, 255))
          color = Color.red;
        else if (source == Color.red)
          color = Color.orange;
        else if (source == Color.orange)
          color = Color.yellow;
        else if (source == Color.yellow)
          color = Color.green;
        else if (source == Color.green)
          color = Color.blue;
        else if (source == Color.blue)
          color = new Color(138, 43, 226);
        else if (source == new Color(138, 43, 226))
          color = Color.red;
        setBackground(color);
        repaint();
    
        }
    But that still does not work. Any suggestions? I will keep trying to work with it and post if I come up with a solution

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

  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
    20

    Default

    Here is a suggestion. randomly find a color and repaint the container with that selected color. Just think what happened in your loop. After action event is fired your loop executed. So...

    And also you can do it in a thread. Set a color, and repaint. After certain time interval repeat the process.

  6. #6
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    why would you need a loop?

    you (seemingly) have an actionListener tied to a button, so all you
    need to do is have class fields for the color array and the counter,
    then in gboard_row1_btn1ActionPerformed you have

    gboard_row1_btn1.setBackground(colors[i % colors.length]);
    i++;

  7. #7
    behrk2 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks Michael,

    That looks like a much better way. I got the same answer in another forum I had posted to as well...

    Java Code:
    A far better way to use the index is with the % operator.
    
    . . . colors[index++ % colors.length] . . .
    
    You can reduce the entire method to
    code:
    --------------------------------------------------------------------------------
    
    public void actionPerformed(ActionEvent e){	
    m_btn1.setBackground(colors[index++ % colors.length]);
    }
    
    --------------------------------------------------------------------------------
    
    You might have problems when you have clicked 2147483647 times with arithmetical overflow, however. Try index++ and ++index, see which works better. You can divide it into 3 statements if you prefer:
    
    index++;
    index %= colors.length;
    myButton.setBackground(colors[index]);
    
    And colors is identical for all instances, so it could beneficially be a static field.
    I will try it out!

Similar Threads

  1. when muse pressed the background change
    By pcman in forum Java Applets
    Replies: 1
    Last Post: 03-17-2008, 11:51 PM
  2. window background color?
    By javan00b in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 10:43 AM
  3. JButton onClick?
    By Joe2003 in forum AWT / Swing
    Replies: 2
    Last Post: 01-06-2008, 03:04 PM
  4. How to change TXT color Onclick
    By dave700800 in forum New To Java
    Replies: 1
    Last Post: 12-08-2007, 01:39 AM
  5. How to change shape of JButton
    By FaRuK in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2007, 12:56 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
  •