Making an image show up on a JButton

I’m trying to make a chessboard (8×8 grid) and fill it with background squares, but it always appears as a grid of empty squares. The image is called emptysquare.jpg but it does have a background color.

How to I correctly add an image to a JButton on the grid I have?

Answer

The reason that your chessboard is empty is due to the fact that you call JFrame#setVisible before adding your ChessSquare buttons to your panel. Ensure all component have been added before making this call.

Also set the Icon like this in ChessSquare

ImageIcon empty = ...
setIcon(empty);

Leave a Reply

Your email address will not be published. Required fields are marked *