Results 1 to 16 of 16
Like Tree2Likes
  • 1 Post By gimbal2
  • 1 Post By nando88

Thread: Use JFrame as Taskbar Icon

  1. #1
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Use JFrame as Taskbar Icon

    I want to know if its possible to use a JFrame as a Taskbar icon?
    I currently have an image as the icon, but I want to use a jprogressbar as the icon instead.
    Is this possible, and if so, please tell me how.
    Currently, the only idea I had was to use the setVisible property of the JFrame in the trayIcon, but it only accepts images.
    Another idea I had was to take a snapshot every once in a while of the taskbar and place that snapshot as the trayIcon, because I am using a thread to control the jprogressbar, because the value I want to display is constantly changing.
    Can someone please help me out?
    Thanks in advance!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Use JFrame as Taskbar Icon

    You mentioned an approach in your description. What happened when you tried it?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    I tried using the getVIsible method of the JFrame, but the trayIcon only accepts images, so I didn't work.
    I tried using the createImage method of the JProgressBar, but it doesn'y display any value when I use it.
    Then I tried using a thread I have in a class, that changes the value of the progressbar, and it says that a non static method cannot be used in a static context.
    Thanks in advance!

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,260
    Rep Power
    6

    Default Re: Use JFrame as Taskbar Icon

    I don't think it is possible to embed a Java program into the task bar, no. The OS doesn't even understand what Java is.

    Another idea I had was to take a snapshot every once in a while of the taskbar and place that snapshot as the trayIcon, because I am using a thread to control the jprogressbar, because the value I want to display is constantly changing.
    That is an idea with a better chance of success. But only when you are somewhat experienced with Java, and if you don't know how to deal with the error "non static method cannot be used in a static context", you're biting off way more than you can chew right now. Try googling that error as "java non static method cannot be used in a static context".
    KevinWorkman likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Use JFrame as Taskbar Icon

    You can take a "screenshot" of a JComponent by invoking its paint method on a Graphics object obtained from a BufferedImage. You could then set that image as your icon.

    Java Code:
    Rectangle rec = component.getBounds();
    BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
    component.paint(bufferedImage.getGraphics());
    This should probably be done in the Swing thread.

    BTW, you may also want to put some kind of progress indicator in the system tray: How to Use the System Tray (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Get in the habit of using standard Java naming conventions!

  6. #6
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    I already used the code you provided, and it works.
    I just have another question.
    how do I make this icon refresh itself?
    I am asking this because I want the icon to change color depending on the value of the progress bar, So I need the icon to refresh itself.
    I am using a thread but the problem I am having is that when I use a:
    Java Code:
    while(true){
    //the code you provided above
    }
    I get a lot of icons creating themselves in the taskbar.
    Please tell me how to fix this.
    Thanks in advance!

  7. #7
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    This is the the thread I'm using.
    something inside this thread, that's also inside the while loop, is making the same icon create itself a lot of times.
    Can someone please tell me how to fix this problem?
    Java Code:
    public void run() {
                    try {
                        while(true){
                        
                        /*if(batteryStatus.getInt()<=30){
                            input = Principal.class.getResourceAsStream("/images/BatteryIcon00001");   
                            
                        }
                        else if(batteryStatus.getInt()>30 && batteryStatus.getInt()<=50){
                            input = Principal.class.getResourceAsStream("/images/BatteryIcon00002");                        
                        }
                        else if(batteryStatus.getInt()>50 && batteryStatus.getInt()<=80){
                            input = Principal.class.getResourceAsStream("/images/BatteryIcon00003");
                        }
                        else if(batteryStatus.getInt()>80){
                            input = Principal.class.getResourceAsStream("/images/BatteryIcon00004");
                        }*/
                        //input = Principal.class.getResourceAsStream("/images/BatteryIcon00001");   
                        //BufferedImage bi = ImageIO.read(input);
                        Rectangle rec = jb.getBounds();
                        BufferedImage bufferedImage = new BufferedImage(20, 16, BufferedImage.TYPE_INT_ARGB);
    jb.paint(bufferedImage.getGraphics()); 
                        
                        final TrayIcon trayIcon = new TrayIcon(bufferedImage,"tray icon");
            final SystemTray tray = SystemTray.getSystemTray();
                        
            // Create a popup menu components
            MenuItem open = new MenuItem("Open");
            MenuItem aboutItem = new MenuItem("About");
    //        CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
    //        CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
    //        Menu displayMenu = new Menu("Display");
    //        MenuItem errorItem = new MenuItem("Error");
    //        MenuItem warningItem = new MenuItem("Warning");
    //        MenuItem infoItem = new MenuItem("Info");
    //        MenuItem noneItem = new MenuItem("None");
            MenuItem exitItem = new MenuItem("Exit");
             
            //Add components to popup menu
            popup.add(open);
            popup.add(aboutItem);
            popup.addSeparator();
    //        popup.add(cb1);
    //        popup.add(cb2);
            popup.addSeparator();
    //        popup.add(displayMenu);
    //      displayMenu.add(errorItem);
    //        displayMenu.add(warningItem);
    //        displayMenu.add(infoItem);
    //        displayMenu.add(noneItem);
            popup.add(exitItem);
             
            trayIcon.setPopupMenu(popup);
             
            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.out.println("TrayIcon could not be added.");
                return;
            }
            
            trayIcon.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        Principal p = new Principal();
                        p.setVisible(true);
                    } catch (Exception ex) {
                        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            });
             
            aboutItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    ImageIcon icon = new ImageIcon(Principal.class.getResource("images/Battery.gif"));
                    JOptionPane.showMessageDialog(null,"This software was created by ProTechHN","About",JOptionPane.INFORMATION_MESSAGE,icon);                
                }
            });
            open.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        Principal p = new Principal();
                        p.setVisible(true);
                    } catch (Exception ex) {
                        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    
                }
            });
             
    //        cb1.addItemListener(new ItemListener() {
    //            public void itemStateChanged(ItemEvent e) {
    //                int cb1Id = e.getStateChange();
    //                if (cb1Id == ItemEvent.SELECTED){
    //                    trayIcon.setImageAutoSize(true);
    //                } else {
    //                    trayIcon.setImageAutoSize(false);
    //                }
    //            }
    //        });
    //         
    //        cb2.addItemListener(new ItemListener() {
    //            public void itemStateChanged(ItemEvent e) {
    //                int cb2Id = e.getStateChange();
    //                if (cb2Id == ItemEvent.SELECTED){
    //                    trayIcon.setToolTip("Sun TrayIcon");
    //                } else {
    //                    trayIcon.setToolTip(null);
    //                }
    //            }
    //        });
             
            ActionListener listener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    MenuItem item = (MenuItem)e.getSource();
                    //TrayIcon.MessageType type = null;
                    System.out.println(item.getLabel());
                    if ("Error".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.ERROR;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is an error message", TrayIcon.MessageType.ERROR);
                         
                    } else if ("Warning".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.WARNING;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is a warning message", TrayIcon.MessageType.WARNING);
                         
                    } else if ("Info".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.INFO;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is an info message", TrayIcon.MessageType.INFO);
                         
                    } else if ("None".equals(item.getLabel())) {
                        //type = TrayIcon.MessageType.NONE;
                        trayIcon.displayMessage("Sun TrayIcon Demo",
                                "This is an ordinary message", TrayIcon.MessageType.NONE);
                    }
                }
            };
             
    //        errorItem.addActionListener(listener);
    //        warningItem.addActionListener(listener);
    //        infoItem.addActionListener(listener);
    //        noneItem.addActionListener(listener);
             
            exitItem.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    tray.remove(trayIcon);
                    System.exit(0);
                }
            });
        
                        
                    } 
                    }catch (Exception ex) {
                        Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
    Thanks in advance!

  8. #8
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    Can someone please tell me how to remove an image from the trayIcon?
    Thanks in advance!

  9. #9
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Use JFrame as Taskbar Icon

    On line 56, you're adding a new icon every iteration of the loop. Try updating the image and using TrayIcon (Java Platform SE 7 ) instead. And when you're done, use SystemTray (Java Platform SE 7 ).
    Get in the habit of using standard Java naming conventions!

  10. #10
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    Can you please tell me how to refresh the icon image that's created with this code:
    Java Code:
    Rectangle rec = component.getBounds();
    BufferedImage bufferedImage = new BufferedImage(rec.width, rec.height, BufferedImage.TYPE_INT_ARGB);
    component.paint(bufferedImage.getGraphics());
    Thanks in advance!

  11. #11
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Use JFrame as Taskbar Icon

    I already did, but I'll explain a different way.

    You've got a TrayIcon, right? Instead of repeatedly adding it to the system tray, only add it once. Then in your loop, run the code that creates the image and use TrayIcon#setImage(...) to update your icon.
    Get in the habit of using standard Java naming conventions!

  12. #12
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    The thing is I want to do it with progress bar state Changed.
    I tried to do it, but had no success.
    I tried using bufferedImage as a global variable and the tray also, but now the progress bar will not show, and it gives me no error on the compiler (Netbeans).
    Please help me out!
    I really need to do this with the state changed event of the progress bar.

  13. #13
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    I just fixed the problem I was having.
    Thank you all for helping!
    This is what I did:
    Java Code:
    public static BufferedImage bufferedImage = new BufferedImage(20, 18, BufferedImage.TYPE_INT_ARGB);
     public static TrayIcon trayIcon = new TrayIcon(bufferedImage,"progress bar");
                                                                                                                  
     private void jbStateChanged(javax.swing.event.ChangeEvent evt) {                  //Color now changes when progress bar percent changes              
            Rectangle rec = jb.getBounds();        
            jb.paint(bufferedImage.getGraphics());
            trayIcon.setImage(bufferedImage);                
        }                               
    //The last thing I did was place these 3 lines in the main
            Rectangle rec = jb.getBounds();        
            jb.paint(bufferedImage.getGraphics());
            trayIcon.setImage(bufferedImage);
    kjkrum likes this.

  14. #14
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Use JFrame as Taskbar Icon

    Glad you figured it out.

    You aren't doing anything with the Rectangle you create on line 5 of your last snippet. If you use an IDE like Eclipse, it will warn you about things like that.
    Get in the habit of using standard Java naming conventions!

  15. #15
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    I just have 1 more problem.
    My problem is that if I already have the jframe opened and I accidentally open it again, I lose the icon image.
    Can someone please tell me how to fix this?
    Thanks in advance!

  16. #16
    nando88 is offline Member
    Join Date
    Aug 2013
    Posts
    28
    Rep Power
    0

    Default Re: Use JFrame as Taskbar Icon

    Fortunately I had a backup!
    Now everything is working fine.
    Thanks in advance!

Similar Threads

  1. Make a jframe "shine" on the taskbar
    By danoc93 in forum AWT / Swing
    Replies: 9
    Last Post: 06-26-2012, 06:31 AM
  2. JFrame Set Icon?
    By Alerhau in forum AWT / Swing
    Replies: 3
    Last Post: 08-14-2011, 12:30 AM
  3. Change JFrame taskbar color?
    By dunafrothint in forum AWT / Swing
    Replies: 3
    Last Post: 03-05-2010, 06:14 AM
  4. [SOLVED] JFrame Image Icon
    By Ch4os in forum NetBeans
    Replies: 1
    Last Post: 11-24-2008, 11:53 AM
  5. Problem in setting icon for JFrame!
    By pranav13 in forum AWT / Swing
    Replies: 8
    Last Post: 11-04-2008, 06:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •