eclipse example:
After reading the above, it still took me more research to understand, where and how to place image resources, using eclipse.
The outcome: in eclipse you need to store images underneath any "Source Folder" (such as "src") or below in a "Folder".
You create both by right-clicking on your project, "New"->"Source Folder" or "New"->"Folder". Any folder name is fine. Examples for "<Source Folder>/<image Folder>" are "src/images" or "resource/img".
Here's some fully functioning sample code, which expects two button images, "Button-Image-Up.png" and "Button-Image-Down.png" in folder "images":
import javax.swing.JDialog;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class ImageButtonDlg extends JDialog {
// define button images and fall-back text
private static gstrTxt="<Button Fall-back Text>"; // in case image does not load
private static String gstrImgUp="Button-Image-Up.png"; // regular image
private static String gstrImgDown="Button-Image-Down.png"; // button pressed image
private static String gstrImgFolder="images"; // image folder, "/images" is also fine
public static void main(String[] args) {
ImageButtonDlg dialog = new ImageButtonDlg();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
/**
* Create the dialog.
*/
public ImageButtonDlg() {
initializeDialog();
}
/**
* Create a new ImageIcon from an image resource.
* The input can be:
* - an image file name, e.g. <image.png>
* - a relative path, e.g. <image/image.png>
* - an absolute path, e.g. </home/user/dev/image.png>
* In eclipse, images can be stored below any project "Source folder",
* such as "src".
* ├── src
* │ ├── img1.png
* │ └── images
* │ └── img2.png
* ├── resources
* │ ├── img3.png
* │ └── images
* │ └── img4.png
* └── img5.png
* In above example img1.png to img4.png are found by
* image file name or relative path
* However, img5 is stored in the project root folder and
* needs an absolute path.
*
* @param strImagePath - image filename, absolute path or relative path
* @return new ImageIcon or 'null' if load fails
*/
public static ImageIcon getResourceImageIcon(String strFilepath) {
ClassLoader loader = null;
URL url = null;
Image img=null;
ImageIcon imgIcon=null;
loader = ImageButtonDlg.class.getClassLoader();
System.out.println(loader.toString());
try { // file location: <a relative path>
url = loader.getResource("images/"+strFilepath);
if(url==null) {
System.out.printf("ImageButtonDlg.class.getResource(%s)=>null\n", "images/"+strFilepath);
}
} catch (Exception e0) {
e0.printStackTrace();
}
if(url==null) {
try { // file location: <image filename>
url = loader.getResource(strFilepath);
if(url==null) {
System.out.printf("Util.class.getResource(%s)=>null\n", strFilepath);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
try {
img = ImageIO.read(url);
} catch (Exception e2) {
e2.printStackTrace();
System.exit(0);
}
if (img!=null){
imgIcon = new ImageIcon(img);
}
return imgIcon;
}
/**
* Create JButton with Image
* In case Image load fails, we create a JButton with text
* @param strImgPath - path to Image
* @param strAltText - fall-back text
* @return
*/
public static JButton getNewJButtonWithImageIcon(String strImgPath, String strAltText) {
JButton btnReturn = null;
ImageIcon imgIcon = getResourceImageIcon(strImgPath);
if (imgIcon!=null){
btnReturn = new JButton(imgIcon);
}
else {
btnReturn = new JButton(strAltText);
}
return btnReturn;
}
/**
* Image button was pressed, display a message box
*/
private void actionImageButtonPressed() {
try {
JOptionPane.showMessageDialog(null, "Image Button was pressed", "Info", JOptionPane.INFORMATION_MESSAGE);
}
catch (Exception e) {
; // ignore
}
}
/**
* Initialize the dialog
* add a button panel and the image button to the window/content pane
*/
private void initializeDialog()
{
this.setTitle("Image Button Example");
this.setResizable(false);
this.setBounds(200, 200, 699, 601);
JPanel panelButton = new JPanel();
panelButton.setLayout(new FlowLayout(FlowLayout.RIGHT)); // all buttons in a row
getContentPane().add(panelButton, BorderLayout.SOUTH); // button pane at the bottom of the window
// create the Image Button
JButton btnImageButton = getNewJButtonWithImageIcon(gstrImgUp, gstrTxt);
btnImageButton.setToolTipText("<Explain what pressing the Button does>");
btnImageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
actionImageButtonPressed();// The Action
}
});
// load button image when button clicked/released
btnImageButton.addMouseListener((MouseListener) new MouseAdapter() {
public void mousePressed(MouseEvent e) {
btnImageButton.setIcon(getResourceImageIcon(gstrImgDown));
}
public void mouseReleased(MouseEvent e) {
btnImageButton.setIcon(getResourceImageIcon(gstrImgUp));
}
});
btnImageButton.setActionCommand("ImageButtonAction");
// add button to button panel
panelButton.add(btnImageButton);
}
}
Have Fun!
Volker Fröhlich
P.S.:
Perhaps someone can share additional practical experience.
- In eclipse "WindowBuilder Editor" such image buttons are shown, but they cannot be accessed - is there some workaround?
- What is the NetBeans approach, if any different?