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 6644663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:11:36+00:00 2026-05-26T00:11:36+00:00

I am struggling with a problem on coloring an array of JButtons. I made

  • 0

I am struggling with a problem on coloring an array of JButtons.

I made two arrays of JButtons:

public JButton Speler1[] = new JButton[140]; //Player1
public JButton Speler2[] = new JButton[140]; //Player2

These two arrays of buttons make lane 1 and lane 2 of a racing game. I want the position of player 1 and 2 to be colored on both screens. So player 1 can see where player 2 is and vice versa.

I already made a method which sends the position of both players to eachother.

if (message.contains("Positie")) {
   String posit = message.replaceFirst("Positie", "");
   int positi = Integer.valueOf(posit);
   positie2 = positi;
   kleurHokje kleur = new kleurHokje();
   kleur.hokVerkleur(positi); // positi is the position of each player
}

So when I call the method hokVerkleur(positi), I want to change a button on lane 2.

class kleurHokje{
    public void hokVerkleur(int loc){
        Speler2[loc].setBackground(Color.yellow);
        Speler2[positie2].setBackground(Color.gray);                
    }
}

It just wont work. While i’m doing almost the same for Speler1[positie] except Speler1 doesn’t use the network, which works as I want.

Any help is appreciated,

Thanks Jef

Edit:
If I place my code in one of the MouseListeners it works fine, but rather have it to be colored automatically instead of having to click each time.

 class Klaar extends MouseAdapter {

    public void mouseClicked(MouseEvent e) {            
        Speler2[positie2].setBackground(Color.gray);
    }
}

Ps. my first language isn’t english, I hope you understand my problem.

  • 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-26T00:11:36+00:00Added an answer on May 26, 2026 at 12:11 am
    If I place my code in one of the MouseListeners it works fine:
    

    agreed, if you change Color for JButton from BackGroung Task, then there any changes, you have some issues with Concurency in Swing, your updated to the GUI is out of EDT,

    1) then you have to wrap coloring JButtons into invokeLater();

        java.awt.EventQueue.invokeLater(new Runnable() {
    
            @Override
            public void run() {
                Speler2[loc].setBackground(Color.yellow);
                Speler2[positie2].setBackground(Color.gray);
            }
        });
    

    2) but you have to solve by using regular Swing methods

    2a) wrap your GUI rellated code to the javax.swing.Action

    2b) initialize your BackGroung Tasks from

    • SwingWorker
    • Runnble#Tread
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Hibernate and I'm struggling with problem: I have 3 tables and
I am struggling with a problem regarding to visiblox . I am quite new
Hello I am struggling with simple problem. General idea: class Foo(){ public boolean method1();
I am new to database and am struggling with a problem. I use SQL
Hi i am actually struggling with a problem for the past two days. I
I am new to using JAXB and I'm struggling with a problem right now.
I am hardly struggling against problem I experience for last two days. In fact
I am struggling with a problem in assembly, where I have to take the
I'm struggling with the problem to cut the very first sentence from the string.
I've been struggling with this problem.. There is a wrapper div and it contains

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.