SS113
SS113

Reputation: 548

Changing JOptionPane background color based on If statement

I would like to change the background color of a JOptionPane message based on an If statement. If a user hits one button, a green message background would come up otherwise a red one will. With the following code, both messages come up one after the other. What am I doing wrong? Thank you

if (e.getSource() == cmdYes)
                 new UIManager();
            UIManager.put("OptionPane.background",new   ColorUIResource(0,255,0));
             UIManager.put("Panel.background",new ColorUIResource(0,255,0));
                JOptionPane.showMessageDialog(null, "Green Message", 
                        "Green", 
                        JOptionPane.INFORMATION_MESSAGE);

            if (e.getSource() == cmdNo)
                 new UIManager();
            UIManager.put("OptionPane.background",new ColorUIResource(255,0,0));
             UIManager.put("Panel.background",new ColorUIResource(255,0,0));
                JOptionPane.showMessageDialog(null, "Red Message", 
                        "Red", 
                        JOptionPane.INFORMATION_MESSAGE);

Upvotes: 0

Views: 3412

Answers (1)

subash
subash

Reputation: 3140

change like this. you miss this {} brackets..

if (e.getSource() == cmdYes) {
            new UIManager();
            UIManager.put("OptionPane.background", new ColorUIResource(0, 255, 0));
            UIManager.put("Panel.background", new ColorUIResource(0, 255, 0));
            JOptionPane.showMessageDialog(null, "Green Message", "Green", JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource() == cmdNo) {
            new UIManager();
            UIManager.put("OptionPane.background", new ColorUIResource(255, 0, 0));
            UIManager.put("Panel.background", new ColorUIResource(255, 0, 0));
            JOptionPane.showMessageDialog(null, "Red Message", "Red", JOptionPane.INFORMATION_MESSAGE);
}

Upvotes: 2

Related Questions