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

  • Home
  • SEARCH
  • 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 8070423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:23:49+00:00 2026-06-05T13:23:49+00:00

EDIT : Ok, I found out why. My getX and getY were overriding the

  • 0

EDIT :
Ok, I found out why.

My getX and getY were overriding the Component ones. So my output was like 1 pixel wise… Should have thought of that earlier.

Thanks to those who tried to help me !

Original Question :

I have this Board class which inherits from JPanel which is supposed to display a board (seriously) composed of Squares which are black or white labels. I’m using a GridLayout but when I launch the application (after putting my JPanel in some dummy JFrame) the JLabels appear to be stacked on top of each other in the left hand corner which is not what I want. However the mouse listener seems to behave correctly since I get the good coordinates and the labels’ color toggle between black and white.

Here is the code :

public class Board extends JPanel {
private int dimx,dimy;
private Square[][] squares;

public Board(int x, int y){
    super();
    this.setLayout(new GridLayout(x,y));

    dimx = x;
    dimy = y;
    squares = new Square[dimx][dimy];
    for(int i=0; i<dimx; i++){
        for(int j=0; j<dimy; j++){
            Square sq = new Square(i,j);
            squares[i][j] = sq;
            this.add(sq);
            sq.addMouseListener(new SquareListener(i,j,this));
        }
    }
}

public Square[][] getSquares() {
    return squares;
}
}

public class Square extends JLabel {

private boolean black;
private int x,y;
private char c;

public Square(int x, int y){
    super();
    setBackground(Color.WHITE);
    Border blackline = BorderFactory.createLineBorder(Color.black);
    setBorder(blackline);
    setOpaque(true);
    setHorizontalAlignment(JLabel.CENTER);

    this.x = x;
    this.y =y;

    c = ' ';
}

public void toggle(){
    black = !black;
}

public boolean isBlack(){
    return black;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public char getC() {
    return c;
}

public void setC(char c) {
    this.c = c;
}

protected void paintComponent(Graphics g){
    super.paintComponent(g);
    if(isBlack()){
        setBackground(Color.BLACK);
    }
    else{
        setBackground(Color.WHITE);
    }
}
}


public class SquareListener implements MouseListener{

private int x,y;
private Board board;

public SquareListener(int x, int y, Board b){
    this.x = x;
    this.y = y;
    this.board = b;
}

@Override
public void mouseClicked(MouseEvent arg0) {
    board.getSquares()[x][y].toggle();
    board.repaint();
    System.out.println("Clicked on "+x+","+y);
}
  • 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-06-05T13:23:50+00:00Added an answer on June 5, 2026 at 1:23 pm

    You shouldn’t change the background color inside the paintComponent() method. You shouldn’t even have a paintComponent() method. And you also shouldn’t have to call repaint(). Your toggle() method should be the one that changes the background:

    public void toggle(){
        black = !black;
        setBackGround(black ? Color.BLACK : Color.WHITE);
    }
    

    Finally, the getX() and getY() methods override methods from JComponent. Choose another name for these methods.

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

Sidebar

Related Questions

I'm going to edit an ImageField using jquery ajax,after searching I found out I
FINAL EDIT: After following the answer from Darin Dimitrov, I have found that the
I have been told to edit this file in Sharepoint Designer: /_layouts/KWizCom_WikiPlus/CreateNew.aspx I found
Edit: Just found out this is a chrome problem, the code works fine in
EDIT: I found out that I can get it to compile if I cast
@EDIT: I found out that it seems to be an issue of the Windows
[edit] Found the solution. Reinstall EVERYTHING - xcode, mono, monodevelop and monotouch. Now it
EDIT : I found classloader leak in my webapplication. It boils down to 3rd
EDIT: I've found what's causing the issue, but I don't know why and I
EDIT 9-3-10: I found this blog entry recently that was very enlightening. http://optimizermagic.blogspot.com/2007/12/outerjoins-in-oracle.html There

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.