Black White
Black White

Reputation: 710

UIManager in Button Action

I have one LoginScreen and one MainWindow. When connection attempt succeed to DB, LoginScreen disposed and MainWindow appear.

I have one button has ActionListener for create a JOptionPane. Code succesfully working actually. I have three problems in Painting i think. Let me explain problems one by one;

Problem 1;

UIManager UI=new UIManager();
Object paneBG = UI.get("OptionPane.background");
Object panelBG = UI.get("Panel.background");
UI.put("OptionPane.background", Color.red);
UI.put("Panel.background", Color.red);

String[] buttons2 = { "EXIT", "OK" };
int rc2 = JOptionPane.showOptionDialog(MainWindow.this, 
                                        panel,
                                        "User Input", 
                                        JOptionPane.INFORMATION_MESSAGE,
                                        JOptionPane.PLAIN_MESSAGE, 
                                        icon, 
                                        buttons2, 
                                        buttons2[1]
                                      );

UI.put("OptionPane.background", paneBG);
UI.put("Panel.background", panelBG);

I use above code for change background color of OptionPane, show to user and rollback the UI colors to original.

If i directly run the MainWindow and click the button, color changed (sure panel and buttons area keep original colors in OptionPane, but other OptionPane areas turn to red. This is another issue) like this;

enter image description here

But when i come from LoginScreen, login attempt succeed, LoginScreen disposed and MainWindow appear. I click to same button but OptionPane not painted now, like this;

enter image description here

Problem 2;

I have another button in MainWindow and it create another OptionPane. I directly run the MainWindow and click first button (which has change the UI color and rollback action), close it than click second button (which has another OptinPane) OptionPane still painted so UI colors not rollback to default values.

Problem 3;

If we solve the first and second problem, How can i make transparent these inner panels (which has label and textfield panel one and buttons one)

Upvotes: 3

Views: 413

Answers (1)

VGR
VGR

Reputation: 44385

First of all, the public methods of UIManager are static. It is incorrect, misleading, and pointless to create an instance of UIManager. The correct way to invoke those methods is:

UIManager.put("OptionPane.background", Color.red);

Second, you probably should not use a global setting to change colors, especially dynamically. Instead, set the color of the object in question, by creating an actual instance of JOptionPane instead of using the static convenience method:

static void setBackground(Component c,
                          Color color) {
    if (c instanceof JTextField || c instanceof AbstractButton) {
        return;
    }

    c.setBackground(color);
    if (c instanceof Container) {
        Component[] children = ((Container) c).getComponents();
        for (Component child : children) {
            setBackground(child, color);
        }
    }
}

int show(Icon icon,
         JComponent panel) {

    String[] buttons2 = { "EXIT", "OK" };
    JOptionPane optionPane = new JOptionPane(panel,
                                             JOptionPane.PLAIN_MESSAGE, 
                                             JOptionPane.DEFAULT_OPTION,
                                             icon, 
                                             buttons2, 
                                             buttons2[1]);
    setBackground(optionPane, Color.RED);

    optionPane.createDialog(MainWindow.this, "User Input").setVisible(true);

    Object value = optionPane.getValue();
    int rc2 = JOptionPane.CLOSED_OPTION;
    if (value instanceof String) {
        rc2 = Arrays.asList(buttons2).indexOf(value);
    }

    return rc2;
}

You want to be careful to distinguish message type arguments from option type arguments. Passing both INFORMATION_MESSAGE and PLAIN_MESSAGE as arguments is never correct. The documentation states that the fourth and fifth arguments are an option type and a message type, respectively; an option type argument should be DEFAULT_OPTION, OK_CANCEL_OPTION, YES_NO_OPTION, etc.

Upvotes: 1

Related Questions