swing - Java: Can't repaint ImageIcon -
i have jframe, , inside jframe jpanel, , inside jpanel 12 jbuttons. have set imageicon each button.
then, need give jbutton new imageicon. tried this:
buttons[0].seticon(new imageicon("path/to/new/icon")); but didn't work. tried:
buttons[0].revalidate(); my_jpanel.revalidate(); so forgetting about? why doesn't repaint?
you should make sure you're loading file think you're loading. try this:
imageicon icon = new imageicon("path/to/new/icon"); system.out.println(icon.getdescription()); this null if file not loaded properly. if problem, try using absolute path (e.g. /home/watexmelon/program/blah.jpg on linux or c:\\users\\watexmelon\\program\\blah.jpg) on windows.
if expecting icon in classpath, instead use:
new imageicon(getclass().getresource("/class/path/to/icon")); you should consider checking out imageio prevent problem in future, it's more robust basic constructors.
if above not problem, consider swing not thread safe , may modifying icon on thread other event dispatch thread. try doing this:
eventqueue.invokelater(new runnable() { @override public void run() { buttons[0].seticon(new imageicon("path/to/new/icon")); } } if still doesn't fix problem, problem may @camrickr suggested, e.g. jbutton you're adding icon not 1 think you're adding to. test this, both when create button, , when update icon, a:
system.out.println(system.identityhashcode(button[0])); if doesn't match, buttons not same button.
Comments
Post a Comment