• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble Changing the Icon of JLabel  RSS feed

 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a chess program in java swing using netbeans GUI creator. So far I put 64 JPanels back to back, changed the background colors to black and white, respectively, and put a JLabel with no text in each one (all visually, with generated code). I am now trying to initialize the icons of the appropriate JLabels with JPEG images of chess pieces that I have placed in the resources folder of my netbeans project (with manual code). Unfortunately, somehow when I try to initialize the icons, something is going wrong. No errors, but the labels simply remain blank when I run the following code (or they become blank if I visually set the icons before compiling):

ImageIcon blackRook = new ImageIcon("/resources/Black Rook.JPEG");
jLabel1.setIcon(blackRook);

As mentioned earlier, I can change the icon of the JLabels visually (with generated code). Here is the code I find in the generated code section when I change the top left JPanel's icon to an image of a Black Rook:

jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N

I tried copying this into the program where I want the board to be initialized on a mouse click, but there was some problem with the resourceMap that I don't know how to solve, so I went to trying to set the icons in the way that has worked for me before (the first code snippet shown). Anyone know what I'm doing wrong?

Other notes:

Changing the image type to png or bmp also doesn't work (I just tried that).

Neither did changing the path to any permutation of the full path (removed each slash and word sequentially from the left and compiled it each time, but still nothing):

/Chess/src/chess/resources/Black Rook.JPEG
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume your directories are like this:

Chess // project directory
== src // source directory
==== chess // containing all your java files and the resources directory
====== <java source files>
====== resources // directory containing the images
========= whitebishop.jpg
========= blackking.jpg

and that you have the line "package chess;"

Then this works for me:


Greetings,
Piet
 
Louis Lewis
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! That worked. All the assumptions were true.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!