1

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
A Lee
  • 23
  • 1
  • 5
  • 1
    I don't quite understand your question? Do you want to change the icon while the dialog is on the screen? Also, you don't `new JFrame()` as the first parameter, you can pass `null`, but it's better practice to pass a component that this visible on the screen, if you have one – MadProgrammer May 30 '13 at 00:23
  • Yes, I want to change the icon while the dialog is on the screen. – A Lee May 30 '13 at 00:29
  • By it's nature, the `JOptionPane` dialogs are blocking, the only way to even attempt to change it would be to have background thread "might" be able to interact with the instance of the `JOptionPane`...You get the can of worms, I'll get the can opener – MadProgrammer May 30 '13 at 00:35
  • so look for another solution, got it thanks – A Lee May 30 '13 at 00:37
  • @MadProgrammer is right, but you may be able to leverage the approach shown below. – trashgod May 30 '13 at 04:10

1 Answers1

4

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.

image

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);
trashgod
  • 203,806
  • 29
  • 246
  • 1,045