Making an image show up on a JButton Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Making an image show up on a JButton without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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);
We are here to answer your question about Making an image show up on a JButton - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji