Example usage for java.awt Component setBackground

List of usage examples for java.awt Component setBackground

Introduction

In this page you can find the example usage for java.awt Component setBackground.

Prototype

public void setBackground(Color c) 

Source Link

Document

Sets the background color of this component.

Usage

From source file:Main.java

public static void main(String[] argv) {
    JTable table = new JTable() {
        public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
                c.setBackground(Color.yellow);
            } else {
                c.setBackground(getBackground());
            }/*  w ww  .  j a v  a2s.c  om*/
            return c;
        }
    };
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTable table = new JTable() {
        public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) {
            Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
            if (vColIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
                c.setBackground(Color.yellow);
            } else {
                c.setBackground(getBackground());
            }//from ww w  .ja  v a 2s  . com
            return c;
        }
    };
}

From source file:SimpleMenu.java

/** A simple test program for the above code */
public static void main(String[] args) {
    // Get the locale: default, or specified on command-line
    Locale locale;//from w ww. jav  a  2 s. com
    if (args.length == 2)
        locale = new Locale(args[0], args[1]);
    else
        locale = Locale.getDefault();

    // Get the resource bundle for that Locale. This will throw an
    // (unchecked) MissingResourceException if no bundle is found.
    ResourceBundle bundle = ResourceBundle.getBundle("com.davidflanagan.examples.i18n.Menus", locale);

    // Create a simple GUI window to display the menu with
    final JFrame f = new JFrame("SimpleMenu: " + // Window title
            locale.getDisplayName(Locale.getDefault()));
    JMenuBar menubar = new JMenuBar(); // Create a menubar.
    f.setJMenuBar(menubar); // Add menubar to window

    // Define an action listener for that our menu will use.
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String s = e.getActionCommand();
            Component c = f.getContentPane();
            if (s.equals("red"))
                c.setBackground(Color.red);
            else if (s.equals("green"))
                c.setBackground(Color.green);
            else if (s.equals("blue"))
                c.setBackground(Color.blue);
        }
    };

    // Now create a menu using our convenience routine with the resource
    // bundle and action listener we've created
    JMenu menu = SimpleMenu.create(bundle, "colors", new String[] { "red", "green", "blue" }, listener);

    // Finally add the menu to the GUI, and pop it up
    menubar.add(menu); // Add the menu to the menubar
    f.setSize(300, 150); // Set the window size.
    f.setVisible(true); // Pop the window up.
}

From source file:Main.java

/** Set the background color property of the given components.  Intended to reduce clutter in GUI code. */
public static void setBackground(Color c, Component... components) {
    for (Component cm : components) {
        cm.setBackground(c);
    }/*from  w  w w. j  a va 2s .c  o  m*/
}

From source file:Main.java

public static void setColor_r(Component c, Color bg, Color fg) {
    if (bg != null) {
        c.setBackground(bg);
    }/*  www . jav  a  2 s .c  o  m*/
    if (fg != null) {
        c.setForeground(fg);
    }
    if (c instanceof Container) {
        Component[] comps = ((Container) c).getComponents();
        if (comps.length > 0) {
            for (Component comp : comps) {
                setColor_r(comp, bg, fg);
            }
        }
    }
}

From source file:Main.java

/**
 * Sets the background for an entire component hierarchy to the specified
 * color./*from ww  w . j  a  v  a2 s  .  c  o m*/
 * 
 * @param c
 *                the starting component
 * @param color
 *                the color to set
 */
public static void setComponentTreeBackground(Component c, Color color) {
    c.setBackground(color);

    Component[] children = getChildren(c);

    if (children != null) {
        for (int i = 0; i < children.length; i++) {
            setComponentTreeBackground(children[i], color);
        }
    }
}

From source file:Main.java

public static void setEnabledWithBackground(Component comp, boolean isEnabled) {
    comp.setEnabled(isEnabled);//from  w w  w  .  ja va  2s.  c  om
    if (isEnabled)
        comp.setBackground(SystemColor.text);
    else
        comp.setBackground(SystemColor.control);
}

From source file:Main.java

/**
 * Sets background color of component and all of its children.
 *
 * @param component//  w w  w  .  ja v  a  2 s.  co  m
 *            component to modify
 * @param bg
 *            new background color
 * @param childsOnly
 *            whether exclude component from changes or not
 */
public static void setBackgroundRecursively(final Component component, final Color bg,
        final boolean childsOnly) {
    if (!childsOnly) {
        component.setBackground(bg);
    }
    if (component instanceof Container) {
        for (final Component child : ((Container) component).getComponents()) {
            setBackgroundRecursively(child, bg, false);
        }
    }
}

From source file:Main.java

public static void makeStandOut(java.awt.Component component) {
    assert component != null;
    if (component instanceof javax.swing.JComponent) {
        javax.swing.JComponent jComponent = (javax.swing.JComponent) component;
        jComponent.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.RED, 4));
        jComponent.setOpaque(true);// ww  w.  j  av  a  2 s .c o m
    }
    component.setBackground(java.awt.Color.GREEN);
}

From source file:Main.java

/**
 * Sets the background color for the specified <code>ButtonGroup</code> and
 * all the JCheckBox, JComboBox, JButton, and JRadioButton components that 
 * it contains to the same color.//from  www  .j a  va  2 s .c  o m
 * 
 * @param buttons the button group to set the background for.
 * @param bg the background color.
 */
public static void setBackground(ButtonGroup buttons, Color bg) {
    Enumeration<?> children = buttons.getElements();
    if (children == null) {
        return;
    }
    Component child;

    if (bg != null) {
        while (children.hasMoreElements()) {
            child = (Component) children.nextElement();
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JCheckBox) || (child instanceof JComboBox)
                            || (child instanceof JButton) || (child instanceof JRadioButton))) {
                child.setBackground(bg);
            }
        }
    }
}