Khosraw Azizi
Khosraw Azizi

Reputation: 81

How to change button properties using UIManager

Currently working on a project for an email client programmed using Java's Mail API. I'm having a problem trying to change some properties in Swing using the UIManager.

It currently looks like this for the specific section of the JOptionPane: enter image description here

You can see that the button color has not changed with the currently changes in code:

protected static Credentials credentialHandler() {
    String EMAIL = null;
    String PASSWORD = null;

    UIManager.put("OptionPane.background",new ColorUIResource(32, 38, 44));
    UIManager.put("OptionPane.foreground",Color.WHITE);
    UIManager.put("Panel.background",new ColorUIResource(32, 38, 44));
    UIManager.put("TextField.background",new ColorUIResource(62, 68, 74));
    UIManager.put("TextField.foreground",Color.WHITE);
    UIManager.put("Label.foreground",Color.WHITE);
    UIManager.put("PasswordField.foreground",Color.WHITE);
    UIManager.put("Button.background",new ColorUIResource(62, 68, 74));
    JPanel panel = new JPanel();

    JLabel labelEmail = new JLabel("Enter an email:");
    labelEmail.setForeground(Color.WHITE);
    JTextField email = new JTextField(32);
    email.setBackground(new Color(32, 38, 44));
    JLabel labelPass = new JLabel("Enter a password:");
    JPasswordField pass = new JPasswordField(16);
    pass.setBackground(new Color(32, 38, 44));
    panel.add(labelEmail);
    panel.add(email);
    panel.add(labelPass);
    panel.add(pass);
    String[] options1 = new String[]{"Login", "Cancel"};
    int option1 = JOptionPane.showOptionDialog(null, panel, "Credentials",
            JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
            null, options1, options1[1]);
    if (option1 == 0) {
        EMAIL = email.getText();
        char[] passwordChar = pass.getPassword();
        PASSWORD = new String(passwordChar);
    } else {
        JOptionPane.showMessageDialog(null, "Looks like you exited the program. If you think this is a mistake, please report it to the developer!");
        System.exit(2);
    }
    return new Credentials(EMAIL, PASSWORD);
}

I want to change the button's background color and as a plus, the font color (gonna assume it's something to do with the foreground).

Upvotes: 1

Views: 682

Answers (1)

Abra
Abra

Reputation: 20914

Replace new ColorUIResource(62, 68, 74) with new java.awt.Color(62, 68, 74).

/* Add following "import":
 * import java.awt.Color;
 */
UIManager.put("Button.background", new Color(62, 68, 74));

For setting the font color, use the following (which sets the JButton text color to white):

UIManager.put("Button.foreground", Color.white);

Upvotes: 2

Related Questions