简体   繁体   中英

Changing the icon in JOptionPane

I have a class that extends JOptionPane . In it there's a method that calls showConfirmDialog (new JFrame(), (JScrollPane) jp, "Friends List", 2, 0, icon);

Is there a way to change the icon without having to call showConfirmDialog a second time? That is, based on my input in the JOptionPane , can I change the icon without making a new confirm dialog?

As shown here , you can add a JOptionPane to a Dialog and listen for the desired PropertyChangeEvent . The example below switches between two UIManager icons in response to clicking the buttons.

图片

JDialog d = new JDialog();
d.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final Icon PENDING = UIManager.getIcon("html.pendingImage");
final Icon MISSING = UIManager.getIcon("html.missingImage");
final JOptionPane optionPane = new JOptionPane("Click a Button",
    JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
optionPane.addPropertyChangeListener(new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        if (e.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
            Integer value = (Integer) e.getNewValue();
            if (value.intValue() == JOptionPane.YES_OPTION) {
                optionPane.setIcon(PENDING);
            } else {
                optionPane.setIcon(MISSING);
            }
        }
    }
});
d.setContentPane(optionPane);
d.pack();
d.setLocationRelativeTo(null);
d.setVisible(true);

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