Here you can find the source of applyDefaultProperties(final JTextField comp)
Parameter | Description |
---|---|
comp | Component to set default properties for. Can be <code>null</code>. |
public static void applyDefaultProperties(final JTextField comp)
//package com.java2s; import java.awt.Button; import java.awt.Checkbox; import java.awt.Choice; import java.awt.Color; import java.awt.Component; import java.awt.Dialog; import java.awt.Font; import java.awt.Label; import java.awt.List; import java.awt.Panel; import java.awt.ScrollPane; import java.awt.Scrollbar; import java.awt.TextArea; import java.awt.TextComponent; import java.awt.TextField; import java.awt.Window; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JPopupMenu; import javax.swing.JRadioButton; import javax.swing.JScrollBar; import javax.swing.JScrollPane; import javax.swing.JSeparator; import javax.swing.JSplitPane; import javax.swing.JTabbedPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.UIManager; import javax.swing.text.JTextComponent; public class Main { /**/* www .j a v a 2s . co m*/ * Defines the default fall back color for backgrounds. The default is * white. */ private static final Color DEFAULT_BACKGROUND = Color.white; /** * Defines the default fall back color for foregrounds. The default is * black. */ private static final Color DEFAULT_FOREGROUND = Color.black; /** * Defines the default fall back font to be used when no other font is * available. The default is Font("Helvetica", Font.PLAIN, 12). */ private static Font g_defaultFont = null; /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Button comp) { if (comp == null) { return; } applyProperties(comp, "Button.background", // $NON-NLS-1$ "Button.foreground", // $NON-NLS-1$ "Button.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JButton comp) { if (comp == null) { return; } applyProperties(comp, "Button.background", // $NON-NLS-1$ "Button.foreground", // $NON-NLS-1$ "Button.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Choice comp) { if (comp == null) { return; } applyProperties(comp, "ComboBox.background", // $NON-NLS-1$ "ComboBox.foreground", // $NON-NLS-1$ "ComboBox.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JComboBox comp) { if (comp == null) { return; } applyProperties(comp, "ComboBox.background", // $NON-NLS-1$ "ComboBox.foreground", // $NON-NLS-1$ "ComboBox.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Label comp) { if (comp == null) { return; } applyProperties(comp, "Label.background", // $NON-NLS-1$ "Label.foreground", // $NON-NLS-1$ "Label.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JLabel comp) { if (comp == null) { return; } applyProperties(comp, "Label.background", // $NON-NLS-1$ "Label.foreground", // $NON-NLS-1$ "Label.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final List comp) { if (comp == null) { return; } applyProperties(comp, "List.background", // $NON-NLS-1$ "List.foreground", // $NON-NLS-1$ "List.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JList comp) { if (comp == null) { return; } applyProperties(comp, "List.background", // $NON-NLS-1$ "List.foreground", // $NON-NLS-1$ "List.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JMenuItem comp) { if (comp == null) { return; } applyProperties(comp, "MenuItem.background", // $NON-NLS-1$ "MenuItem.foreground", // $NON-NLS-1$ "MenuItem.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JMenu comp) { if (comp == null) { return; } applyProperties(comp, "Menu.background", // $NON-NLS-1$ "Menu.foreground", // $NON-NLS-1$ "Menu.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JPopupMenu comp) { if (comp == null) { return; } applyProperties(comp, "PopupMenu.background", // $NON-NLS-1$ "PopupMenu.foreground", // $NON-NLS-1$ "PopupMenu.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JMenuBar comp) { if (comp == null) { return; } applyProperties(comp, "MenuBar.background", // $NON-NLS-1$ "MenuBar.foreground", // $NON-NLS-1$ "MenuBar.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JOptionPane comp) { if (comp == null) { return; } applyProperties(comp, "OptionPane.background", // $NON-NLS-1$ "OptionPane.foreground", // $NON-NLS-1$ "OptionPane.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Checkbox comp) { if (comp == null) { return; } if (comp.getCheckboxGroup() == null) { applyProperties(comp, "CheckBox.background", // $NON-NLS-1$ "CheckBox.foreground", // $NON-NLS-1$ "CheckBox.font"); // $NON-NLS-1$ } else { // Checkbox is in group applyProperties(comp, "RadioButton.background", // $NON-NLS-1$ "RadioButton.foreground", // $NON-NLS-1$ "RadioButton.font"); // $NON-NLS-1$ } } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JCheckBox comp) { if (comp == null) { return; } applyProperties(comp, "CheckBox.background", // $NON-NLS-1$ "CheckBox.foreground", // $NON-NLS-1$ "CheckBox.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JRadioButton comp) { if (comp == null) { return; } applyProperties(comp, "RadioButton.background", // $NON-NLS-1$ "RadioButton.foreground", // $NON-NLS-1$ "RadioButton.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Scrollbar comp) { if (comp == null) { return; } applyProperties(comp, "ScrollBar.background", // $NON-NLS-1$ "ScrollBar.foreground", // $NON-NLS-1$ null); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JScrollBar comp) { if (comp == null) { return; } applyProperties(comp, "ScrollBar.background", // $NON-NLS-1$ "ScrollBar.foreground", // $NON-NLS-1$ null); } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final ScrollPane comp) { if (comp == null) { return; } applyProperties(comp, "ScrollPane.background", // $NON-NLS-1$ "ScrollPane.foreground", // $NON-NLS-1$ "ScrollPane.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JScrollPane comp) { if (comp == null) { return; } applyProperties(comp, "ScrollPane.background", // $NON-NLS-1$ "ScrollPane.foreground", // $NON-NLS-1$ "ScrollPane.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JSeparator comp) { if (comp == null) { return; } applyProperties(comp, "Separator.background", // $NON-NLS-1$ "Separator.foreground", // $NON-NLS-1$ "Separator.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JSplitPane comp) { if (comp == null) { return; } applyProperties(comp, "SplitPane.background", // $NON-NLS-1$ null, null); } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JTabbedPane comp) { if (comp == null) { return; } applyProperties(comp, "TabbedPane.background", // $NON-NLS-1$ "TabbedPane.foreground", // $NON-NLS-1$ "TabbedPane.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JTable comp) { if (comp == null) { return; } applyProperties(comp, "Table.background", // $NON-NLS-1$ "Table.foreground", // $NON-NLS-1$ "Table.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final TextArea comp) { if (comp == null) { return; } applyProperties(comp, "TextArea.background", // $NON-NLS-1$ "TextArea.foreground", // $NON-NLS-1$ "TextArea.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JTextArea comp) { if (comp == null) { return; } applyProperties(comp, "TextArea.background", // $NON-NLS-1$ "TextArea.foreground", // $NON-NLS-1$ "TextArea.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final TextField comp) { if (comp == null) { return; } if (comp.getEchoChar() != (char) 0) { applyProperties(comp, "PasswordField.background", // $NON-NLS-1$ "PasswordField.foreground", // $NON-NLS-1$ "PasswordField.font"); // $NON-NLS-1$ } else { // Normal text field applyProperties(comp, "TextField.background", // $NON-NLS-1$ "TextField.foreground", // $NON-NLS-1$ "TextField.font"); // $NON-NLS-1$ } } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JTextField comp) { if (comp == null) { return; } applyProperties(comp, "TextField.background", // $NON-NLS-1$ "TextField.foreground", // $NON-NLS-1$ "TextField.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JPasswordField comp) { if (comp == null) { return; } applyProperties(comp, "PasswordField.background", // $NON-NLS-1$ "PasswordField.foreground", // $NON-NLS-1$ "PasswordField.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JTree comp) { if (comp == null) { return; } applyProperties(comp, "Tree.background", // $NON-NLS-1$ "Tree.foreground", // $NON-NLS-1$ "Tree.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Panel comp) { if (comp == null) { return; } applyProperties(comp, "Panel.background", // $NON-NLS-1$ "Panel.foreground", // $NON-NLS-1$ "Panel.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JPanel comp) { if (comp == null) { return; } applyProperties(comp, "Panel.background", // $NON-NLS-1$ "Panel.foreground", // $NON-NLS-1$ "Panel.font"); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Window comp) { if (comp == null) { return; } applyProperties(comp, "window", // $NON-NLS-1$ "windowText", // $NON-NLS-1$ null); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Dialog comp) { if (comp == null) { return; } applyProperties(comp, "control", // $NON-NLS-1$ "controlText", // $NON-NLS-1$ null); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JDialog comp) { if (comp == null) { return; } applyProperties(comp, "control", // $NON-NLS-1$ "controlText", // $NON-NLS-1$ null); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final TextComponent comp) { if (comp == null) { return; } applyProperties(comp, "text", // $NON-NLS-1$ "textText", // $NON-NLS-1$ null); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. It uses values defined as UIManager * properties. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final JTextComponent comp) { if (comp == null) { return; } applyProperties(comp, "text", // $NON-NLS-1$ "textText", // $NON-NLS-1$ null); // $NON-NLS-1$ } /** * Sets default background and foreground color as well as a default font * for the specified component. * * @param comp * Component to set default properties for. Can be * <code>null</code>. */ public static void applyDefaultProperties(final Component comp) { if (comp == null) { return; } applyProperties(comp, "control", // $NON-NLS-1$ "controlText", // $NON-NLS-1$ null); } /** * Sets background and foreground color and a font for the specified * component. * * @param comp * Component to set properties for. * @param colBack * Background color. Can be <code>null</code> to use parent color * (not suggested due to differences between Java VMs). * @param colFore * Foreground color. Can be <code>null</code> to use parent color * (not suggested due to differences between Java VMs). * @param font * Font. Can be <code>null</code> to use parent font (not * suggested due to differences between Java VMs). */ public static void applyProperties(final Component comp, final Color colBack, final Color colFore, final Font font) { if (comp != null) { comp.setBackground(colBack); comp.setForeground(colFore); comp.setFont(font); } } /** * Sets background and foreground color and a font for the specified * component. * * @param comp * Component to set properties for. If <code> * null</code> nothing happens. * @param strKeyBackground * Key for background color defined in * <code>javax.swing.UIManager</code>. If it is <code>null</code> * or not found first the key "control" will be tried, and if * still <code>null</code>, a "fallback" color will be taken. * @param strKeyForeground * Key for background color defined in * <code>javax.swing.UIManager</code>. If it is <code>null</code> * or not found first the key "textText" will be tried, and if * still <code>null</code>, a "fallback" color will be taken. * @param strKeyFont * Key for font defined in <code> * javax.swing.UIManager</code>. If it is <code>null</code> or not found * first the key "Label.font" will be tried, and if still * <code>null</code>, a "fallback" font will be taken. */ public static void applyProperties(final Component comp, final String strKeyBackground, final String strKeyForeground, final String strKeyFont) { if (comp != null) { Color colBack = null; Color colFore = null; Font font = null; if (strKeyBackground != null) { colBack = UIManager.getColor(strKeyBackground); } if (strKeyForeground != null) { colFore = UIManager.getColor(strKeyForeground); } if (strKeyFont != null) { font = UIManager.getFont(strKeyFont); } if (colBack == null) { colBack = UIManager.getColor("control"); // $NON-NLS-1$ if (colBack == null) { colBack = DEFAULT_BACKGROUND; // Fall back default } } if (colFore == null) { colFore = UIManager.getColor("controlText"); // $NON-NLS-1$ if (colFore == null) { colFore = DEFAULT_FOREGROUND; // Fall back default } } if (font == null) { font = UIManager.getFont("Label.font"); // $NON-NLS-1$ if (font == null) { font = g_defaultFont; // Fall back default } } applyProperties(comp, colBack, colFore, font); } // end if } }