Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default Changing Java Icon

    I want to change the coffee cup java icon from Frame....I want my own icon over there....
    How can I do that?

    Oooooooo after a lots of googling i got something...but having no help....It is showing error, throwing Exception...
    Can u anyone please make the following code correct ??


    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import java.awt.Image;
    public class Test
    {
    public static void main(String[] args) throws Exception
    {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setIconImage(java.awt.Toolkit.getDefaultTool kit().getImage(this.getClass().getResource("D:/icon")));
    frame.pack();
    frame.setVisible(true);
    }
    }
    Last edited by Adam Cruge; 04-11-2009 at 01:24 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
        frame.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(
                 this.getClass().getResource("D:/icon")));
    You may not use the this keyword in static context. In static context use
    "ClassName.class" in place of this.
    Try something like this (pseudo code):
    Java Code:
        // You'll likely need to try/catch one or more exceptions...
        java.net.URL url = Test.class.getResource("D:/icon");
        System.out.println("url = " + url);
        BufferedImage image = javax.imageio.ImageIO.read(url);
        frame.setIconImage(image);

  3. #3
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    what Classes I need to import ???

  4. #4
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    or else give total code with necessary correction......

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Besides the two that were given you would need
    Java Code:
    java.awt.image.BufferedImage;

  6. #6
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    ok....
    But still have some problem....
    Showing url = null and Exception in "main"
    I am using a JPEG image ....is it causing any problem ??

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Showing url = null and Exception in "main"
    When using the getResource method the image/resource must be in/on your class path; the class loader is looking for it - see Method Detail for the method in the Class api. The exception is probably that the image returned by ImageIO.read is null.
    I am using a JPEG image ....is it causing any problem ??
    Shouldn't be. ImageIO (version 1.6) will read bmp, gif, jpg and png formats okay.

  8. #8
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    So what be the possible solution ??

  9. #9
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    So I put my Test.java file in D:\ and in the same place I have icon.jpeg file. But getResource() method can not find it and returns "null"....
    What should I do ?
    Please help me suggesting a solution...
    Thank you in advance....

  10. #10
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    the code is
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import java.awt.Image;
    import java.net.URL;
    import java.awt.image.*;
    import java.net.*;
    public class Test
    {
    public static void main(String[] args) throws Exception
    {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    URL url = Test.class.getResource("D:/icon.gif");
    System.out.println("url = " + url);
    BufferedImage image = javax.imageio.ImageIO.read(url);
    frame.setIconImage(image);
    frame.pack();
    frame.setVisible(true);
    }
    }


    Please make correction......

  11. #11
    vijayabaskar is offline Member
    Join Date
    Apr 2009
    Posts
    37
    Rep Power
    0

    Default

    where did u sava ur java file..
    it will be in D drive..
    u give the path getResource("../icon.gif")

  12. #12
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    thank you...It works....

  13. #13
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    I can not understand this line in my code.Actually "hardwired" wrote this line....
    So would you please explane this line a little bit ???
    The line is --- "BufferedImage image=javax.imageio.ImageIO.read(url);"

  14. #14
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    I want to know why BufferedImage is used?
    Why not we used simple Image?
    setIconImage(Image img) this method need Image type object...but we supplied BufferedImage ...
    Why ???

  15. #15
    smitha is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Red face to develop a generic parser in java

    I need help to start up with a parser to convert a pojo into CLI command

  16. #16
    smitha is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    I am supposed to use java.util.regex package.
    Please can you people help me out

  17. #17
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    what u wanna do???
    Write in detail and if possible some code....

  18. #18
    smitha is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Smile Parser

    we have a class called interface which stores
    data like
    interface=ethernet
    port=1
    pass this object to a parser module so that it can pass interface as a token
    ethernet as a token value and form a command interface ethernet

  19. #19
    Adam Cruge is offline Member
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import java.awt.Image;
    import java.net.URL;
    import java.awt.image.*;
    import java.net.*;
    public class Test
    {
    public static void main(String[] args) throws Exception
    {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    URL url = Test.class.getResource("icon.gif");
    System.out.println("url = " + url);
    BufferedImage image = javax.imageio.ImageIO.read(url);
    frame.setIconImage(image);
    frame.pack();
    frame.setVisible(true);
    }
    }



    This is my code and I want this frame to become a system tray icon when i click on minimize button...
    how will i do it ???

    How to get access to the minimize button of titlebar ???
    Last edited by Adam Cruge; 04-11-2009 at 05:52 PM.

  20. #20
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    I want to know why BufferedImage is used?
    It is the accepted/newer/preferred way.
    Why not we used simple Image?
    Toolkit methods are older and do not warn you when image files cannot be found, cannot be read or have corrupted data. They also require a MediaTracker to load the image data (see MediaTracker api for example code).
    Lesson: Working with Images

    I want this frame to become a system tray icon when i click on minimize button...
    how will i do it ???

    It should work okay with the mouse as–is. For more on the system tray see
    How to Use the System Tray
    Java Code:
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import javax.imageio.ImageIO;
    
    public class TestRx
    {
        public static void main(String[] args) throws Exception
        {
            JFrame frame = new JFrame("Hello World");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            URL url = Test.class.getResource("icon.gif");
                                             //"images/middle.gif");
            System.out.println("url = " + url);
            BufferedImage image = ImageIO.read(url);
            frame.setIconImage(image);
            frame.setSize(200,100);
            frame.setLocation(200,200);
            frame.setVisible(true);
        }
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. can java program display icon on unix?
    By java2008 in forum Advanced Java
    Replies: 1
    Last Post: 10-06-2008, 08:33 AM
  2. How to implement the Icon interface in Java
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-24-2008, 12:17 AM
  3. java : how to hide application icon from a task bar
    By yogeshagashe in forum Advanced Java
    Replies: 0
    Last Post: 03-12-2008, 02:05 PM
  4. Changing icon of JOptionPane
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-21-2007, 08:01 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
  •