1 Reply Latest reply: Jun 25, 2014 7:58 AM by 946128 RSS

    Unable to set swing custom taskbar icon

    f36003b7-3e04-4b6e-bd85-ce17e97b58ae

      Hi all,

       

       

      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);    

       

              ImageIcon btnJIFrameNonRollover = new ImageIcon((getClass().getClassLoader().getResource("btnjiframe.jpg")));

              ImageIcon btnJIFrameRollover = new ImageIcon((getClass().getClassLoader().getResource("btnjiframerollover.jpg")));

       

              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);

          }

         

        }

       

       

      Thanks for any suggestions.