Skip to main content

Extending JButtons not appearing

2 replies [Last post]

I am currently working on a game board. I want to use JButtons as the pieces and make and Extended JButton that can store the buttons x and y positions on a grid. When ever I try to extend JButton only the first button will show up on the board and you have to click all the others for them to appear. So my question is: What am I forgetting?

Here is the extended JButton code I wrote:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GamePiece extends JButton{

private int x;
private int y;

public GamePiece(String a, int bx, int by){


public GamePiece(String a, Icon i, int bx, int by){

public int getX(){
return x;

public int getY(){
return y;

public void setX(int bx){
x = bx;

public void setY(int by){
y = by;


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2012-01-16

I know this is from a long time ago, but I just recently had this problem.

The issues are the names of the X and Y getter and setter methods. They are already defined by JButton, and this messes things up when you call them.

Simply rename the getter and setter functions and the problem should be solved.

Joined: 2003-06-13

Don't repaint the piece; repaint the board.

You are probably adding each piece to a panel (something like JPanel). After adding or changing anything in a piece make sure you invalidate the panel, and then you can repaint the panel.