Results 1 to 8 of 8
  1. #1
    sharafat is offline Member
    Join Date
    Jan 2008
    Location
    Dhaka, Bangladesh
    Posts
    5
    Rep Power
    0

    Question How to change Window Icon

    Can anybody please give an example code on how to change the icon in the upper-left corner of a window?
    Using setImageIcon() doesn't seem to work.
    If it can be changed through a property in NetBeans IDE, then how's that supposed to do?

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    In the Netbeans properties window for your form, try iconImage. Otherwise, try:
    Java Code:
    Image icon = Toolkit.getDefaultToolkit().getImage([COLOR=#0066ff][I]"icon.gif"[/I][/COLOR]);
    frame.setIconImage(icon);
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    sharafat is offline Member
    Join Date
    Jan 2008
    Location
    Dhaka, Bangladesh
    Posts
    5
    Rep Power
    0

    Unhappy

    Umm... still not working. Probably the image path isn't correct. My project name is "Test", java file (class) name is frame.java . I placed an image file named abc.jpg . The project directory structure is as below:

    Test -> src -> frame.java, frame.form, abc.jpg
    -> build -> classes -> frame.class

    Is the following code correct:

    Java Code:
    public frame() {
            Image icon = Toolkit.getDefaultToolkit().[B]getImage("[COLOR="Red"]abc.jpg[/COLOR]")[/B];
            JFrame frame2 = new JFrame("Hi");
            frame2.setIconImage(icon);
            frame2.setVisible(true);
            initComponents();
        }

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Since it is isn't much, post you entire .java source file containing this code - what you have there doesn't look wrong, but I'm not sure why you made a frame constructor. Your path doesn't look correct, which is likely causing your problem - have you experimented putting the .jpg in other locations? How about within build/classes where your class is located? Isn't this where a java program is run, by using classes, right? So wouldn't it make sense to put the .jpg in the classes folder? I dunno, you tell me.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  5. #5
    sharafat is offline Member
    Join Date
    Jan 2008
    Location
    Dhaka, Bangladesh
    Posts
    5
    Rep Power
    0

    Smile

    Well, finally managed to display the icon image. The final piece of code needed was:

    Java Code:
    [I]FrameName[/I].setIconImage(Toolkit.getDefaultToolkit().getImage("abc.jpg"));
    And the image must be placed in the project root directory - in this case,
    Test -> abc.jpg

    But the problem is, the JAR file produced by NetBeans 6 doesn't include that image file and hence showing the default coffee-cup icon instead. I added the image file in the JAR file, but nothing changed. When running from the NetBeans IDE, it's showing just fine, but from the Binary file the IDE produces, it is not. Any idea?

  6. #6
    qeuin is offline Member
    Join Date
    Feb 2008
    Posts
    1
    Rep Power
    0

    Default

    Put the image in the folder where you have the form's sourcecode which is using this image or in a subfolder. (Then it should be included in JAR automatically)
    Then load the image using:
    Java Code:
    setIconImage(Toolkit.getDefaultToolkit().getImage(
           MainJFrame.class.getResource("imgs/video.jpg")));
    This is an example when I have file video.jpg in folder imgs and changing image in a window of class MainJFrame.

    You can set this directly in NetBeans properties. You have to open window of iconImage property and then choose to "set property using "custom code"".
    There's no difference, but it looks nicer, because then you have your code in the NetBeans generated code.

    Hope this would work

  7. #7
    sharafat is offline Member
    Join Date
    Jan 2008
    Location
    Dhaka, Bangladesh
    Posts
    5
    Rep Power
    0

    Thumbs up

    Many many thanks. :)

  8. #8
    paulcheck is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default What about web start?

    I am able to get the icon to change in a regular java application by following these instructions. I have a folder "Images" in the "src" directory and everything works for the java application as is.

    However...when I switch the application to enable java web start, I am able to build the application as usual, but when I go to run the application I get an error. I can toggle back and forth between "<default config"> and "Web Start" in the drop down menu on the main netbeans page (just to the left of the build hammer) and the app correspondingly works/doesn't work. In the "web start" mode, the error is clearly related to the image.

    So...do I have to copy that Image directory somwhere where the web start application can find/use it?

Similar Threads

  1. Icon initialization problem
    By saz25 in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 11:37 PM
  2. Changing icon of JOptionPane
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-21-2007, 08:01 AM
  3. How to set an Icon in a Label?
    By Soda in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 01:38 PM
  4. How can I add an ICON on a JDialog's Title-Bar?
    By iimasd in forum AWT / Swing
    Replies: 2
    Last Post: 11-06-2007, 01:54 PM
  5. To add an icon to my project
    By Albert in forum AWT / Swing
    Replies: 1
    Last Post: 07-13-2007, 04:14 PM

Posting Permissions

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