问题
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?
回答1:
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);
来源:https://stackoverflow.com/questions/16826009/changing-the-icon-in-joptionpane