Tuesday, November 21, 2017

Use UIManager to set fonts, foreground color, background color, select color, border for Swing components

Once you set the properties in the UIManager, all your Swing components will have the corresponding look and feel.

public void setLookAndFeel () {

        Font plainFont = new Font("Serif", Font.PLAIN, 12);
        Font boldFont = new Font("Serif", Font.BOLD, 12);

        Color focusColor = Color.BLUE;
        Color backgroundColor = Color.YELLOW;
        Color disabledForgroundColor = Color.GREY;
        Color selectColor = Color.LIGHT_GRAY;

        UIManager.put("Text.font", new FontUIResource(plainFont));
        UIManager.put("TextField.font", new FontUIResource(plainFont));
        UIManager.put("FormattedTextField.font", new FontUIResource(plainFont));
        UIManager.put("PasswordField.font", new FontUIResource(boldFont));
        UIManager.put("TextArea.font", new FontUIResource(plainFont));
        UIManager.put("TextPane.font", new FontUIResource(plainFont));
        UIManager.put("List.font", new FontUIResource(plainFont));
        UIManager.put("Table.font", new FontUIResource(plainFont));
        UIManager.put("TableHeader.font", new FontUIResource(plainFont));
                   
        UIManager.put("CheckBox.font", new FontUIResource(plainFont));
        UIManager.put("RadioButton.font", new FontUIResource(plainFont));
        UIManager.put("Button.font",new FontUIResource(boldFont));
        UIManager.put("Button.focus", new ColorUIResource(focusColor));
        UIManager.put("Button.border", new LineBorder(Color.ORANGE, 5, true));
        UIManager.put("ComboBox.font", new FontUIResource(plainFont));
        UIManager.put("ComboBox.TableHeader.font", new FontUIResource(plainFont)); 
        UIManager.put("Label.font", new FontUIResource(plainFont));
        UIManager.put("ToolTip.font", new FontUIResource(boldFont));
        UIManager.put("ToggleButton.font", new FontUIResource(plainFont));
        UIManager.put("TitledBorder.font", new FontUIResource(boldFont));
     
        UIManager.put("Menu.font", new FontUIResource(boldFont));
        UIManager.put("MenuItem.font", new FontUIResource(boldFont));
        UIManager.put("MenuBar.font", new FontUIResource(boldFont));
        UIManager.put("PopupMenu.font", new FontUIResource(boldFont));
        UIManager.put("RadioButtonMenuItem.font", new FontUIResource(boldFont));
        UIManager.put("CheckBoxMenuItem.font", new FontUIResource(boldFont));
        UIManager.put("MenuItem.acceleratorFont", new FontUIResource(boldFont));
        UIManager.put("CheckBoxMenuItem.acceleratorFont", new FontUIResource(boldFont));
        UIManager.put("Menu.acceleratorFont", new FontUIResource(boldFont));
        UIManager.put("RadioButtonMenuItem.acceleratorFont", new FontUIResource(boldFont));
     
        UIManager.put("ToolBar.font", new FontUIResource(boldFont));
        UIManager.put("MenuBar.font", new FontUIResource(boldFont));
        UIManager.put("Panel.font", new FontUIResource(boldFont));
        UIManager.put("ProgressBar.font", new FontUIResource(boldFont));
        UIManager.put("OptionPane.font", new FontUIResource(boldFont));
        UIManager.put("OptionPane.buttonFont", new FontUIResource(boldFont));
        UIManager.put("OptionPane.messageFont", new FontUIResource(plainFont));
        UIManager.put("ScrollPane.font", new FontUIResource(boldFont));
        UIManager.put("EditorPane.font", new FontUIResource(plainFont));
        UIManager.put("ColorChooser.font", new FontUIResource(boldFont));
        UIManager.put("InternalFrame.titleFont", new FontUIResource(boldFont));
        UIManager.put("TabbedPane.font", new FontUIResource(boldFont));

        UIManager.put("Panel.background", backgroundColor);
        UIManager.put("CheckBox.background", backgroundColor);
        UIManager.put("RadioButton.background", backgroundColor);
        UIManager.put ("Button.select", selectColor);
UIManager.put ("ToggleButton.select", selectColor);
        UIManager.put("ComboBox.disabledBackground", backgroundColor);
        UIManager.put("ComboBox.fieldBackground", backgroundColor);
        UIManager.put("ComboBox.disabledForeground", disabledForgroundColor);
        UIManager.put("OptionPane.background", backgroundColor);
        UIManager.put("OptionPane.warningDialog.border.background", backgroundColor);
        UIManager.put("Label.background", backgroundColor);
        UIManager.put("TextField.shadow", backgroundColor);
        UIManager.put("TextField.background", backgroundColor); 
    }

Reference:

1. Swing UIManager Keys
2. Nimbus Defaults

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment