Reputation: 131
I am new to Swing. I need to give a Jbutton an icon. I found example code so many places and tried like this
ImageIcon cup = new ImageIcon("/home/Whether/Picture/start.png");
JButton startBut=new JButton(cup);
panel.add(startBut);
But button is showing without Icon.Image path is correct.
Pls coorect me if Im wrong?
Thnx in advance
Upvotes: 1
Views: 277
Reputation: 3704
The best way of adding images is to link image to an "anchor" java class
Create "Anchor" class as a source bound and place it into your project package which contains an "image.png" etc... So you can read image right from the Jar file as
Image image=ImageIO.read(Anchor.class.getClassLoader().getResourseAsStream("image.png"));
ImageIcon icon=new ImageIcon(image);
Rest things with JButton I guess you know :)
Good luck
Upvotes: 1