Results 1 to 2 of 2
  1. #1
    xytho33 is offline Member
    Join Date
    Sep 2010
    Rep Power

    Default Swing: Change the icon on a JComponent


    I am trying to change the icon in a Swing JComponent using the UIManager. I am executing some example code that I found on the web. Similar code appears in John Zukowski's book, The Definitive Guide to Java Swing, so it appears that it should work. However, when I execute this code, it does not change the icons. Can anyone determine why this code does not work? Thanks!

    Example here:

    package stuffPackage;

    import java.awt.BorderLayout;
    import java.awt.Color;

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.LookAndFeel;
    import javax.swing.UIDefaults;
    import javax.swing.UIManager;

    public class LazyLoaderTest {
    public static void main(String args[]) {
    JFrame frame = new JFrame("Lazy Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    String path = "/home/icons/FloppyDrive.gif";
    File iconFile = new File(path);

    if(iconFile.exists()) {
    System.out.println(iconFile.getAbsolutePath() + " was found!");
    else {
    System.out.println("iconFile NOT found");

    Object iconObject = LookAndFeel.makeIcon(LazyLoaderTest.class, path);
    UIManager.put("Tree.leafIcon", iconObject);

    Integer fifteen = new Integer(15);
    Object lazyArgs[] = new Object[] { Color.GREEN, Boolean.TRUE, fifteen, fifteen };
    Object lazyDiamond = new UIDefaults.ProxyLazyValue("DiamondIcon", lazyArgs);
    UIManager.put("Tree.openIcon", lazyDiamond);

    JTree tree = new JTree();
    JScrollPane scrollPane = new JScrollPane(tree);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(200, 200);

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power


    1. Testing the existence of the file doesn't help, because makeIcon uses getResourceAsStream to load the image. Instead test
    Java Code:
    System.out.println("Path is ok ? " + (LazyLoaderTest.class.getResourceAsStream(path) != null));
    2. Do you have a DiamondIcon class that implements Icon and has a constructor that takes 4 parameters Color, Boolean, int[], int[] ? Even if you do, I'm guessing is won't provide any visible icon with two int arrays in which all the members are zero.


Similar Threads

  1. Can´t change selected/pressed Icon of a JMenu
    By Valhallon in forum AWT / Swing
    Replies: 3
    Last Post: 07-08-2011, 10:16 PM
  2. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Swing
    Replies: 3
    Last Post: 09-29-2010, 03:14 PM
  3. Replies: 1
    Last Post: 07-10-2010, 04:21 AM
  4. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 01:37 AM
  5. Icon change and lib folder problem
    By LeonLanford in forum New To Java
    Replies: 10
    Last Post: 10-21-2009, 04:13 PM

Tags for this Thread

Posting Permissions

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