Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7620093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:51:16+00:00 2026-05-31T03:51:16+00:00

I have this 2D JButton array (color white), and what I want to do

  • 0

I have this 2D JButton array (color white), and what I want to do is when I click on a square, it’ll turn into orange. When I click another square, that square becomes orange as well but the first square I clicked will return to its normal white color. How do I do it without having all the squares turn into orange?

Here’s a visualization:
enter image description here

Btw, when I click the squares with the numbers, their colors do not change, think of them as like setEnabled(false) buttons. I already know how to change the colors of the buttons, I just want to know how the color gets passed on square after square.

Thanks to anyone who can help!

Edit: My “attempt” but it’s making all of my null valued buttons orange.

button[i][j].addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e){
       for(int i = 0; i < button.length; i++)
           for(int j = 0; j < button.length; j++){
               if(g.gameBoard[i][j] == null){
                   //must find the position where the user clicks and make it orange
                    button[i][j].setBackground(Color.ORANGE);
               }
           }   
       }
}); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:51:17+00:00Added an answer on May 31, 2026 at 3:51 am

    you can use JToggleButton and put them in a ButtonGroup so that you can treat them as a group and set the color to the activated one.

    P.S. i will keep in contact with this thread for the next 24 hours if there is any question i will try to explain more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JFrame. This JFrame contains a JButton. I click the JButton and
I have this code in jQuery, that I want to reimplement with the prototype
I have a JButton that I would like to change the background color of
I have a JButton that is constructed using an Action and this action has
How can I set the color of a JButton ? I have tried this:
I have a JFrame and this JFrame has a JButton . I want in
I have this code: public class Window extends JFrame { public Window(){ ... JButton
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this tiny flag that I am using for an icon on the
I know that I am missing something simple here. I have got this homework

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.