0

I have create an image button but it didn’t show the image. The file is on src\MyPackage folder. How can I map it?

There is my code:

jpAnnotation=new JPanel();     

jpAnnotation.setLayout(new FlowLayout(FlowLayout.LEADING));
JButton btnUnderline =new JButton(new ImageIcon ("UnderlineIcon.gif"));
btnUnderline.setSize(50, 260);
btnUnderline.setAlignmentX(JButton.LEFT_ALIGNMENT);
btnUnderline.setHorizontalAlignment(JButton.LEFT);
btnUnderline.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0){                      
         ActionEvent ae = new ActionEvent(bean, 0, "Underline");
         bean.actionPerformed(ae);
    }
});
jpAnnotation.add(btnUnderline); 
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user819774
  • 1,456
  • 1
  • 19
  • 48
  • 2
    I would suggest trying to use [`ImageIO.read`](http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html) over `ImageIcon` as it will throw an `IOException` when something goes wrong, rather the silently failing like `ImageIcon` does – MadProgrammer Mar 27 '14 at 22:54
  • `btnUnderline.setSize(50, 260);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement ***or sizing*** of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 28 '14 at 09:58

2 Answers2

3

Just a little code snippet:

btnUnderline.setIcon(
  new ImageIcon(getClass().getResource("/path/to/UnderlineIcon.gif")));

Brief explanation

Using this statement for loading your image, you don't have to care about the right URL to your file, because you automatically get the correct URL.

This is based on loading the resource from the class path and not from the filesystem path!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
PrR3
  • 1,250
  • 1
  • 18
  • 26
  • I would guess that this is what the OP is looking for, but perhaps you'd like to explain why so they understand there mistake ;) – MadProgrammer Mar 27 '14 at 22:43
  • @PrR3, I used your code but it has error 'The method setIcon(Icon) in the type AbstractButton is not applicable for the arguments (Image)' – user819774 Mar 27 '14 at 22:50
  • 2
    `ImageIcon(String)` looks for a file on the file system, from what the OP has posted, it looks they might be using embedded resources (files stored within the Jar/Classpath), which is generally a preferred approach. Just so we can distinguish between the two mechanisms ;) – MadProgrammer Mar 27 '14 at 22:53
  • [`AbstractButton.setIcon(Icon)`](http://docs.oracle.com/javase/8/docs/api/javax/swing/AbstractButton.html#setIcon-javax.swing.Icon-) specifies an `Icon` not an `Image`, so the last part of that, `.getImage()` seems wrong to me.. – Andrew Thompson Mar 28 '14 at 09:34
  • @AndrewThompson yeah man thx for your advice. i used it in an extended context and forgot to remove the transformation step completely ([look here](http://pastebin.com/HxFKBh78))! Sry for that! – PrR3 Mar 28 '14 at 09:47
1

Try this:

btnUnderline.setIcon( new ImageIcon( "C:\\YourFolder\src\MyPackage\UnderlineIcon.gif" ) );

If of course you're using Windows. Alternatively you can move the gif to the same directory as where you're executing your code from.

Zyn
  • 614
  • 3
  • 10
  • 2
    Using an absolute reference to any external file is a bad idea, this will break the programs ability to execute on other computers... – MadProgrammer Mar 27 '14 at 22:42