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);
Just a little code snippet:
btnUnderline.setIcon(
new ImageIcon(getClass().getResource("/path/to/UnderlineIcon.gif")));
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!