Changing the icon in JOptionPane

杀马特。学长 韩版系。学妹 提交于 2019-12-19 11:20:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!