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

/**
 * 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.
 * //www .  jav  a 2 s  .c  o m
 * @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:Main.java

private static void setForegroundOrBackgroundDeep(final boolean doForeground, Component component, Color color,
        Class[] excludedClasses, boolean processContentsOfExcludedContainers) {

    // If this component one that should be excluded?
    boolean excluded = false;
    if (excludedClasses != null) {
        for (int i = 0; i < excludedClasses.length && !excluded; i++) {
            if (excludedClasses[i].isInstance(component)) {
                excluded = true;/*from ww w.j  a  va 2s.  c  om*/
            }
        }
    }

    // If not exluded, set the component's foreground or background.
    if (!excluded) {
        if (doForeground) {
            component.setForeground(color);
        } else {
            component.setBackground(color);
        }
    }

    // Recursively process the contents of containers.
    if ((!excluded || processContentsOfExcludedContainers) && (component instanceof Container)) {
        Container container = (Container) component;
        Component[] children = container.getComponents();
        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                setForegroundOrBackgroundDeep(doForeground, children[i], color, excludedClasses,
                        processContentsOfExcludedContainers);
            }
        }
    }
}

From source file:org.revager.tools.GUITools.java

/**
 * Creates a new standard table./*from  ww  w.j a v a  2 s.c  o  m*/
 * 
 * @param model
 *            the table model
 * @param showHeader
 *            true if the header of the table should be visible
 * 
 * @return the newly created table
 */
@SuppressWarnings("serial")
public static JTable newStandardTable(TableModel model, boolean showHeader) {
    /*
     * Prep. for rollover
     */
    if (lastRolloverKey == Integer.MAX_VALUE) {
        lastRolloverKey = 0;
    } else {
        lastRolloverKey++;
    }

    final int keyIdx = lastRolloverKey;

    rollOverRowIndex.put(keyIdx, -1);

    final JTable table = new JTable(model) {

        @Override
        public boolean editCellAt(int row, int column, java.util.EventObject e) {
            boolean result = super.editCellAt(row, column, e);
            final Component editor = getEditorComponent();

            TableCellRenderer renderer = this.getColumnModel().getColumn(column).getCellRenderer();
            Font cellFont = null;
            if (renderer instanceof DefaultTableCellRenderer) {
                cellFont = ((DefaultTableCellRenderer) renderer).getFont();
            }
            if (editor != null && editor instanceof JTextComponent) {
                JTextComponent jTextComponent = (JTextComponent) editor;
                if (e == null) {
                    jTextComponent.selectAll();
                } else {
                    SwingUtilities.invokeLater(jTextComponent::selectAll);
                }
                jTextComponent.setBorder(UI.MARKED_BORDER_INLINE);
                if (cellFont != null) {
                    jTextComponent.setFont(cellFont);
                }
                editor.requestFocusInWindow();
            }
            return result;
        }

        @Override
        public TableCellRenderer getCellRenderer(int row, int column) {
            TableCellRenderer renderer = super.getCellRenderer(row, column);
            if (renderer instanceof DefaultTableCellRenderer) {
                ((DefaultTableCellRenderer) renderer).setBorder(new EmptyBorder(3, 3, 3, 3));
            }
            return renderer;
        }

        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
            Component comp = super.prepareRenderer(renderer, row, col);
            // Rollover
            comp.setBackground(getBackground());
            comp = super.prepareRenderer(renderer, row, col);
            if (!isRowSelected(row) && row == rollOverRowIndex.get(keyIdx)) {
                comp.setForeground(getForeground());
                comp.setBackground(UI.BLUE_BACKGROUND_COLOR);
            }

            // Tooltips
            JComponent jcomp = (JComponent) comp;
            if (renderer instanceof DefaultTableCellRenderer) {
                String toolTip = ((DefaultTableCellRenderer) renderer).getToolTipText();
                if (!StringUtils.isEmpty(toolTip)) {
                    jcomp.setToolTipText(toolTip);
                }
            }
            return comp;
        }
    };

    // Table properties
    table.setRowHeight(UI.TABLE_ROW_HEIGHT);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setShowGrid(false);
    table.setShowHorizontalLines(true);
    table.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

    // Rollover
    MouseInputAdapter rolloverListener = new MouseInputAdapter() {
        @Override
        public void mouseExited(MouseEvent e) {
            rollOverRowIndex.put(keyIdx, -1);
            table.repaint();
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int row = table.rowAtPoint(e.getPoint());
            if (row != rollOverRowIndex.get(keyIdx)) {
                rollOverRowIndex.put(keyIdx, row);
                table.repaint();
            }
        }
    };
    table.addMouseMotionListener(rolloverListener);
    table.addMouseListener(rolloverListener);

    // Header
    if (!showHeader) {
        table.setTableHeader(null);
    }
    return table;
}

From source file:com.aw.swing.mvp.grid.RowColorBkgChanger.java

public void process(Object row, Component cellCmp) {
    Color color = getColor(row);
    cellCmp.setBackground(color);
}

From source file:at.tuwien.ifs.commons.gui.controls.MultiOptionToggleButton.java

public MultiOptionToggleButton(final ImageIcon[] icons, final String[] buttonTexts, final String tooltip,
        final MultiOptionToggleListener listener) {
    super(icons[0]);
    this.setToolTipText(tooltip);
    this.addActionListener(new ActionListener() {
        @Override//from   ww  w  .  j av  a 2s .  c om
        public void actionPerformed(ActionEvent e) {
            menu.show(MultiOptionToggleButton.this, 0, MultiOptionToggleButton.this.getHeight());

            // highlight current selection
            final Component[] components = menu.getComponents();
            for (Component c : components) {
                c.setBackground(null);
            }
            menu.getComponent(selectedIndex).setBackground(Color.GRAY);
        }
    });

    for (int i = 0; i < buttonTexts.length; i++) {
        JMenuItem jMenuItem = new JMenuItem(buttonTexts[i], icons[i]);
        jMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listener.performAction(e.getActionCommand());
                selectedIndex = ArrayUtils.indexOf(buttonTexts, e.getActionCommand());
                setIcon(icons[selectedIndex]);
            }
        });
        menu.add(jMenuItem);
    }

    JMenuBar menuBar = new JMenuBar();
    menuBar.setBorderPainted(false);
    menuBar.add(menu);
}

From source file:org.jtheque.films.stats.view.impl.panels.PanelStats.java

/**
 * Create a panel containing a pie chart.
 *
 * @param key     The i18 key of the title of the pie chart.
 * @param dataset The dataset used to populate the pie chart.
 *
 * @return The component containing the pie chart.
 *//*w  w  w.  ja va 2 s  . c om*/
final Component createPieChartPanel(String key, PieDataset dataset) {
    JFreeChart pie = ChartFactory.createPieChart3D(resources.getMessage(key), dataset, true, true, true);

    pie.getLegend().setLegendItemGraphicLocation(RectangleAnchor.RIGHT);

    Managers.getManager(ILanguageManager.class).addInternationalizable(new InternationalizableChart(pie, key));

    Component chartPanel = new ChartPanel(pie);

    chartPanel.setBackground(Color.white);

    return chartPanel;
}

From source file:Main.java

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
        boolean cellHasFocus) {
    Component c = defaultRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    if (c instanceof JLabel) {
        if (isSelected) {
            c.setBackground(Color.blue);
        } else {//www .  j a v  a 2  s  . c  o m
            c.setBackground(Color.red);
        }
    } else {
        c.setBackground(Color.red);
        c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    }
    return c;
}

From source file:Cal.java

/** Set just the day, on the current month */
public void setDayActive(int newDay) {

    clearDayActive();/*from   w  ww  .j ava 2s  .co m*/

    // Set the new one
    if (newDay <= 0)
        dd = new GregorianCalendar().get(Calendar.DAY_OF_MONTH);
    else
        dd = newDay;
    // Now shade the correct square
    Component square = labs[(leadGap + newDay - 1) / 7][(leadGap + newDay - 1) % 7];
    square.setBackground(Color.red);
    square.repaint();
    activeDay = newDay;
}

From source file:net.sf.jabref.gui.search.SearchBar.java

private void paintBackgroundWhite(Container container) {
    container.setBackground(Color.WHITE);
    for (Component component : container.getComponents()) {
        component.setBackground(Color.WHITE);

        if (component instanceof Container) {
            paintBackgroundWhite((Container) component);
        }// w  w  w  .  ja  v  a  2 s . c o  m
    }
}

From source file:lu.lippmann.cdb.graph.GenericGraphViewImpl.java

/**
 * {@inheritDoc}// w w  w  . jav  a 2  s.  c o m
 */
@Override
public void addMetaInfoComponent(final Component c) {
    c.setBackground(this.getBackground()); //same bg
    validate();
    repaint();
}