0

What I'm trying to achieve here is put an image inside of any JButton (which is stored inside of an array) whenever you click on one of them. All the images are inside of the eclipse-workspace. What am I doing wrong here?

@Override
public void actionPerformed(ActionEvent e) {
  JButton source = (JButton) e.getSource();
  source.setIcon(new ImageIcon("csharp.png"));
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • What could be the issue here, that the image you are referencing is not actually in the same directory as your source file here. This snippet would imply, that this is the case. If the image is actually in another directory, you might need to use the relative path to the image. – maloomeister Jun 23 '20 at 07:38
  • For more information on this issue, refer to this: https://stackoverflow.com/questions/4801386/how-do-i-add-an-image-to-a-jbutton – maloomeister Jun 23 '20 at 07:47
  • Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Sep 13 '20 at 15:53

0 Answers0