简体   繁体   中英

How to set the default icon for all Java Swing windows?

How to set the default icon for all Java Swing windows?

Otherwise i have to set icons for every frame i created.

What's your suggestions? Simple hackings are also accepted.

many thx

Update: Best if the method you suggested can leave existing frame-creation codes untouched. thx

Create an Abstact class that extends JFrame

In the constructor set your icon.

create child class that extends your new Abstract Class and call super in your constructor

public abstract class MainFrame extends JFrame {
    protected MainFrame() {
        this.setIconImage(null); // Put your own image instead of null
    }
}

public class ChildFrame extends MainFrame {
    public ChildFrame() {
        super();
    }
}

You can also just create object from your new class

public class MainFrame extends JFrame {
    public MainFrame() {
        this.setIconImage(null); // Put your own image instead of null
    }
}

public class Frame {

    private MainFrame mainframe = new MainFrame();

    public Frame() {
        super();
    }
}

To make windows icons changes globally without changing old code I am using this code snippet

public static void fixWindowsIcons(final List<Image> iconImages) {
    PropertyChangeListener l = new PropertyChangeListener() {

        private Window prevActiveWindow;

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            final Window o = KeyboardFocusManager.getCurrentKeyboardFocusManager()
                    .getActiveWindow();
            if (o != null && prevActiveWindow != o) {
                prevActiveWindow = o;
                List<Image> windowIcons = o.getIconImages();
                if (windowIcons == null || windowIcons.size() == 0) {
                    o.setIconImages(iconImages);
                }
            }
        }
    };
    KeyboardFocusManager.getCurrentKeyboardFocusManager()
            .addPropertyChangeListener("activeWindow", l); //$NON-NLS-1$
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM