aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JToggleButton and ImageIcon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JToggleButton and ImageIcon" Watch "JToggleButton and ImageIcon" New topic
Author

JToggleButton and ImageIcon

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

im not sure what im doing wrong. i tried setting icon of JToggleButton using constructor. no image. i then tried to setIcon() and setSelectedIcon() and i get null pointer exceptions. the image files are in the same folder as the class file. here is my code:

i guess monday i'll try changing path to:
ImageIcon water = new ImageIcon("./water.jpg");

[ April 24, 2004: Message edited by: Randall Twede ]

SCJP
Visit my download page
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

ok, i found out what caused the NullPointerExceptions. it was a typo
maze[1][j].setIcon(water);
instead of
maze[i][j].setIcon(water);
however, the JToggleButtons still display no images. i tried some new images, but that made no difference. i did
System.out.println(water.toString());
System.out.println(wood.toString());
and the filenames get printed.
i dont get it. an ImageIcon IS an Icon so what is wrong?
[ April 26, 2004: Message edited by: Randall Twede ]
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
I almost replied to you over the weekend, but was getting burned out. Anyhow, my first feeling is that images, even if they are in the same directory as your class file, may not be in the search path for the application.
Try specifying the full path of the image, or at least running a test to see if it is an I/O problem, or try to list off the contents of the subdirectory.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

maggie,
you are correct. it is a "bug" in netbeans. netbeans requires hardcoding full path (so much for portability). i opened it in a different IDE (bluejay) and it is fine.
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
*shrug*
i use netbeans and i don't hardcode my paths. it could be the way you've got it set up. it does have this weirdness about the way you mount file systems for your packages to compile and run correctly.
but that's neither here nor there. i'm not qualified to advise you on setting up netbeans to work for you.
cheers!
maggie
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

shrug*
well, it's not just me. a classmate is the one that told me she fixed it by hardcoding absolute path. im using it at the computer lab at school. im clueless. i go file/mount file system, then choose local directory, then select the folder. we have a program called deep freeze that resets all configurations and deletes user files from local hard drive and stuff everytime the computers are restarted (we have a network drive though and that is where the folder in question is at), so maybe it is because every day it thinks it was just installed, or maybe problem is that it is on the network drive. im not sure how teacher has it set up either. i guess i tell him to run it from bluejay if it doesnt work from netbeans.
[ April 27, 2004: Message edited by: Randall Twede ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JToggleButton and ImageIcon