Example usage for java.awt Container setBackground

List of usage examples for java.awt Container setBackground

Introduction

In this page you can find the example usage for java.awt Container 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[] args) {
    JFrame f = new JFrame("Fixed size content");
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Container c = f.getContentPane();
    c.setBackground(Color.YELLOW);

    Dimension d = new Dimension(400, 40);
    c.setPreferredSize(d);/*from w w  w .j  ava  2  s  . c o  m*/
    f.pack();
    f.setResizable(false);
    f.setVisible(true);
}

From source file:TextAcceleratorExample.java

public static void main(String[] args) {
    try {//from   ww  w.j  a va  2  s.  c  o  m
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JLabel l;
    JTextField t;
    JButton b;
    JFrame f = new JFrame("Text Accelerator Example");
    Container cp = f.getContentPane();
    cp.setLayout(new GridBagLayout());
    cp.setBackground(UIManager.getColor("control"));
    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = GridBagConstraints.RELATIVE;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.insets = new Insets(2, 2, 2, 2);
    c.anchor = GridBagConstraints.EAST;

    cp.add(l = new JLabel("Name:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('n');
    cp.add(l = new JLabel("House/Street:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('h');
    cp.add(l = new JLabel("City:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('c');
    cp.add(l = new JLabel("State/County:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('s');
    cp.add(l = new JLabel("Zip/Post code:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('z');
    cp.add(l = new JLabel("Telephone:", SwingConstants.RIGHT), c);
    l.setDisplayedMnemonic('t');
    cp.add(b = new JButton("Clear"), c);
    b.setMnemonic('l');

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.anchor = GridBagConstraints.CENTER;

    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('n');
    c.gridx = 1;
    c.gridy = GridBagConstraints.RELATIVE;
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('h');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('c');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('s');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('z');
    cp.add(t = new JTextField(35), c);
    t.setFocusAccelerator('t');
    c.weightx = 0.0;
    c.fill = GridBagConstraints.NONE;
    cp.add(b = new JButton("OK"), c);
    b.setMnemonic('o');

    f.pack();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent evt) {
            System.exit(0);
        }
    });
    f.setVisible(true);
}

From source file:Main.java

/**
 * Sets the background color for the specified container and all the
 * JPanel, JLabel, JCheckBox, JComboBox, JTextField, JRadioButton, and
 * JScrollPane components that it contains to the same color.
 * /*w  ww . ja  v  a 2  s . com*/
 * @param c the container to set the background color of.
 * @param bg the background color.
 */
public static void setBackground(Container c, Color bg) {
    c.setBackground(bg);
    Component[] children = c.getComponents();
    if (children == null) {
        return;
    }
    Component child;
    int len = children.length;
    if (bg != null) {
        for (int i = 0; i < len; i++) {
            child = children[i];
            if (!bg.equals(child.getBackground())
                    && ((child instanceof JPanel) || (child instanceof JLabel) || (child instanceof JCheckBox)
                            || (child instanceof JComboBox) || (child instanceof JTextField)
                            || (child instanceof JRadioButton) || (child instanceof JScrollPane))) {
                child.setBackground(bg);
            }
        }
    } else {
        // Null background color case
        for (int i = 0; i < len; i++) {
            child = children[i];
            if ((child.getBackground() != null) && ((child instanceof JPanel) || (child instanceof JLabel)
                    || (child instanceof JCheckBox))) {
                child.setBackground(null);
            } //end if
        } // end for
    } //end if else
}

From source file:components.MenuLayoutDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from www  . j  a  v a 2 s .  co m*/
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MenuLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    MenuLayoutDemo demo = new MenuLayoutDemo();
    Container contentPane = frame.getContentPane();
    contentPane.setBackground(Color.WHITE); //contrasting bg
    contentPane.add(demo.createMenuBar(), BorderLayout.LINE_START);

    //Display the window.
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MenuLayoutDemo.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *///from w w w .  ja  v a2 s .  c  o  m
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("MenuLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    MenuLayoutDemo demo = new MenuLayoutDemo();
    Container contentPane = frame.getContentPane();
    contentPane.setBackground(Color.WHITE); // contrasting bg
    contentPane.add(demo.createMenuBar(), BorderLayout.LINE_START);

    // Display the window.
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Create the GUI and show it. For thread safety, this method should be
 * invoked from the event-dispatching thread.
 *///from  w ww  .  j av  a  2 s. c o  m
private static void createAndShowGUI() {
    // Create and set up the window.
    JFrame frame = new JFrame("MenuLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Create and set up the content pane.
    Main demo = new Main();
    Container contentPane = frame.getContentPane();
    contentPane.setBackground(Color.WHITE); // contrasting bg
    contentPane.add(demo.createMenuBar(), BorderLayout.LINE_START);

    // Display the window.
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MenuLayoutDemo.java

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event-dispatching thread./*from  ww w.ja  v a2 s  . c  om*/
 */
private static void createAndShowGUI() {
    //Make sure we have nice window decorations.
    JFrame.setDefaultLookAndFeelDecorated(true);

    //Create and set up the window.
    JFrame frame = new JFrame("MenuLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    MenuLayoutDemo demo = new MenuLayoutDemo();
    Container contentPane = frame.getContentPane();
    contentPane.setBackground(Color.WHITE); //contrasting bg
    contentPane.add(demo.createMenuBar(), BorderLayout.LINE_START);

    //Display the window.
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:JListBackground.java

public static void addComponentsToPane(Container pane) {
    String[] bruteForceCode = { "int count = 0", "int m = mPattern.length();", "int n = mSource .length();",
            "outer:", " ++count;", " }", " return count;", "}" };
    JList list = new JList(bruteForceCode);
    Border etch = BorderFactory.createEtchedBorder();
    list.setBorder(BorderFactory.createTitledBorder(etch, "Brute Force Code"));
    JPanel listPanel = new JPanel();
    listPanel.add(list);/*from  w w w.j  a v a  2s.  co  m*/
    listPanel.setBackground(lightBlue);
    list.setBackground(lightBlue);

    pane.add(listPanel, BorderLayout.CENTER);
    pane.setBackground(lightBlue);
}

From source file:Main.java

public Main() {
    Container content = getContentPane();
    content.setBackground(Color.white);

    JSlider slider1 = new JSlider();
    slider1.setBorder(BorderFactory.createTitledBorder("JSlider without Tick Marks"));

    content.add(slider1, BorderLayout.NORTH);

    JSlider slider2 = new JSlider();
    slider2.setBorder(BorderFactory.createTitledBorder("JSlider with Tick Marks"));

    slider2.setMajorTickSpacing(20);//from w  ww .  ja va  2s  . c  o m
    slider2.setMinorTickSpacing(5);
    slider2.setPaintTicks(true);
    content.add(slider2, BorderLayout.CENTER);

    JSlider slider3 = new JSlider();
    slider3.setBorder(BorderFactory.createTitledBorder("JSlider with Tick Marks & Labels"));

    slider3.setMajorTickSpacing(20);
    slider3.setMinorTickSpacing(5);
    slider3.setPaintTicks(true);
    slider3.setPaintLabels(true);
    content.add(slider3, BorderLayout.SOUTH);

    setSize(300, 300);
    setVisible(true);
}

From source file:room.utilization.PieChart.java

private final void createPieChart() {
    Container contentPane = getContentPane();
    contentPane.setBackground(Color.WHITE);
    setSize(700, 500);/* w w w.  j  a  va2s.  c  o  m*/
    setLocation(500, 250);
    setIconImage(new ImageIcon(
            "C:\\Users\\Olasubomi\\Documents\\NetBeansProjects\\Room Utilization\\src\\room\\utilization\\icon.png")
                    .getImage());
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    contentPane.add(createPanel());
    setVisible(true);
}