Reputation: 771
I'm having a small issue which I seem to be unable to solve for some weird reason.
I'm struggling to understand why my attempt to have a custom taskbar icon as opposed to the default java logo as the icon for my program isn't working.
Essentially my program starts off with a JDesktopPane which contains a JFrame and at a click of a button on the JFrame the JFrame calls a JInternalFrame.
From my code below you will notice this is how I attempt to set the taskbar icon:
java.net.URL resource = getClass().getClassLoader().getResource("systrayicon.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(resource);
TestJDesktopPaneFrame.setIconImage(image);
You will notice that by using getClass().getClassLoader().getResource("systrayicon.jpg") I'm accessing the imahge I intend on using as my taskbar icon from the following path file:/C:/Users/WorkPC/Documents/TEST/bin/systrayicon.jpg
I have used getClass().getClassLoader().getResource("") countless number of times and it has worked well for me e.g. in the button I use to call the JInternalFrame I use getClass().getClassLoader().getResource("") to access the custom image for the button.
Below is my entire code:
package bge.applcs.dsa;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import javax.swing.*;
public class TestJDesktopPane extends JDesktopPane {
public static TestJDesktopPane TestJDesktopPane;
public static JFrame TestJDesktopPaneFrame = new JFrame("");
public JButton btnJIFrame;
public TestJDesktopPane() throws IOException {
createPanel();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
showFrame();
}
});
}
public static void showFrame() {
try {
TestJDesktopPane = new TestJDesktopPane();
} catch (IOException e1) {
e1.printStackTrace();
}
TestJDesktopPaneFrame.setContentPane(TestJDesktopPane);
TestJDesktopPaneFrame.setUndecorated(true);
MoveMouseListener mml = new MoveMouseListener(TestJDesktopPane);
TestJDesktopPane.addMouseListener(mml);
TestJDesktopPane.addMouseMotionListener(mml);
TestJDesktopPaneFrame.pack();
TestJDesktopPaneFrame.setVisible(true);
TestJDesktopPaneFrame.setResizable(false);
TestJDesktopPaneFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TestJDesktopPaneFrame.setLocationRelativeTo(null);
TestJDesktopPaneFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
"Cancel");
TestJDesktopPaneFrame.getRootPane().getActionMap().put("Cancel", new AbstractAction(){
public void actionPerformed(ActionEvent x) {
System.exit(0);
}
});
}
public void createPanel() {
setLayout(null);
setPreferredSize(new Dimension(1000, 300));
java.net.URL resource = getClass().getClassLoader().getResource("systrayicon.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(resource);
TestJDesktopPaneFrame.setIconImage(image);
// ****************************** Minimize button ******************************
// Prepare images
ImageIcon btnJIFrameNonRollover = new ImageIcon((getClass().getClassLoader().getResource("btnjiframe.jpg")));
ImageIcon btnJIFrameRollover = new ImageIcon((getClass().getClassLoader().getResource("btnjiframerollover.jpg")));
// Create custom button
btnJIFrame = new JButton(btnJIFrameNonRollover);
btnJIFrame.setBorder(null);
btnJIFrame.setContentAreaFilled(false);
btnJIFrame.setBorderPainted(false);
btnJIFrame.setFocusPainted(false);
btnJIFrame.setBounds(100,100,100,100);
btnJIFrame.setRolloverIcon(btnJIFrameRollover);
btnJIFrame.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnJIFrame.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JInternalFrame logInJIFrame = new JInternalFrame();
add(logInJIFrame);
}
});
//
add(btnJIFrame);
}
}
EDIT:
I attempted the following but got an error:
//
java.net.URL resource = getClass().getClassLoader().getResource("/images/systrayicon.jpg");
Image image = Toolkit.getDefaultToolkit().getImage(resource);
signInDesktopPaneFrame.setIconImage(image);
Here's the error:
Uncaught error fetching image:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.getConnection(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)
Thanks for any suggestions.
Upvotes: 2
Views: 930
Reputation: 4340
getResource method causes the class loader to look through the directories and JAR files in the program's class path for your image so it should be either in your jar or one of other jar inside your classpath
http://docs.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource
I advice you to make folder inside your src folder for images and then move your images there
then you can specify the foldername/imagename to access it:
ImageIcon icon = ImageIcon(this.getClass().getResource("/images/filename.png"));
or you can use:
ImageIcon icon = ImageIcon(this.getClass().getClassLoader().getResource("images/filename.png"));
both of them will start from the root of classpath
Upvotes: 2