Java Code Examples for java.awt.Component#setBackground()

The following examples show how to use java.awt.Component#setBackground() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tree.java    From TrakEM2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
	final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	// Colorize visited review cells
	if (8 == column && visited_reviews.contains(((NodeTableModel)table.getModel()).nodes.get(row))) {
		c.setForeground(Color.white);
		c.setBackground(Color.green);
	} else {
		if (isSelected) {
			setForeground(table.getSelectionForeground());
            setBackground(table.getSelectionBackground());
		} else {
			c.setForeground(Color.black);
			c.setBackground(Color.white);
		}
	}
	return c;
}
 
Example 2
Source File: JPACompletionItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public Component getPaintComponent(javax.swing.JList list, boolean isSelected, boolean cellHasFocus) {
    Component ret = getPaintComponent(isSelected);
    if (ret == null) {
        return null;
    }
    if (isSelected) {
        ret.setBackground(list.getSelectionBackground());
        ret.setForeground(list.getSelectionForeground());
    } else {
        ret.setBackground(list.getBackground());
        ret.setForeground(list.getForeground());
    }
    ret.getAccessibleContext().setAccessibleName(getItemText());
    ret.getAccessibleContext().setAccessibleDescription(getItemText());
    return ret;
}
 
Example 3
Source File: LogRecordsListRenderer.java    From Hotel-Properties-Management-System with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Component getListCellRendererComponent(@SuppressWarnings("rawtypes") JList list, Object value,
		int index, boolean isSelected, boolean cellHasFocus) {
	Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
	
	if(isSelected || cellHasFocus) {
		
		c.setBackground(Color.decode("#10d6d1"));
	}
	
	else {
		
		c.setBackground(index % 2 == 0 ? list.getSelectionBackground() : list.getBackground());
	}
	return c;
}
 
Example 4
Source File: AlertedCardsRenderer.java    From MtgDesktopCompanion with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,int row, int column) {
	Component comp = new DefaultTableCellRenderer().getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	comp.setForeground(Color.BLACK);
	
	
	
	if ((Integer) value > 0)
		comp.setBackground(Color.GREEN);
	else if(isSelected)
		comp.setBackground(table.getSelectionBackground());
	else
		comp.setBackground(table.getBackground());
	
	
	return comp;
}
 
Example 5
Source File: BaseTable.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Overridden to set the colors apropriately - we always want the editor
 * to appear selected */
public Component prepareEditor(TableCellEditor editor, int row, int col) {
    Component result = editor.getTableCellEditorComponent(this, getValueAt(row, col), false, row, col);

    if (result != null) {
        result.setBackground(getSelectionBackground());
        result.setForeground(getSelectionForeground());
        result.setFont(getFont());
    }

    return result;
}
 
Example 6
Source File: TagMapMarkerRenderer.java    From dsworkbench with Apache License 2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    TagMapMarker tagMarker = (TagMapMarker) value;
    Color selectColor = tagMarker.getTagColor();
    if (selectColor == null) {
        if (!isSelected) {
            if (row % 2 == 0) {
                c.setBackground(Constants.DS_ROW_A);
            } else {
                c.setBackground(Constants.DS_ROW_B);
            }
        }
    } else {
        if (isSelected) {
            selectColor = selectColor.darker();
        }
        c.setBackground(selectColor);
    }

    ((JLabel) c).setText("");
    ((JLabel) c).setHorizontalAlignment(SwingConstants.CENTER);
    if (tagMarker.getTagIcon() >= 0) {
        ((JLabel) c).setIcon(new ImageIcon(ImageManager.getNoteSymbol(tagMarker.getTagIcon())));
    } else {
        ((JLabel) c).setIcon(no_tag);
    }
    return c;
}
 
Example 7
Source File: CardsJTable.java    From magarena with GNU General Public License v3.0 5 votes vote down vote up
private Component prepareDefaultRenderer(TableCellRenderer renderer, int row, int column) {

        final Component cell = super.prepareRenderer(renderer, row, column);
        final MagicCardDefinition card = tableModel.getCardDef(row);
        final boolean isRowSelected = isRowSelected(row);
        final boolean isAlternate = row % 2 == 0;

        ((JComponent) cell).setOpaque(true);
        ((JComponent) cell).setBorder(NO_FOCUS_BORDER);

        /*
        Set text color.
        */
        cell.setBackground(isRowSelected
                ? SELECT_BCOLOR
                : isAlternate
                        ? ALTERNATE_COLOR
                        // have to create a new color object because Nimbus returns
                        // a color of type DerivedColor, which behaves strange, not sure why.
                        : new Color(getBackground().getRed(), getBackground().getGreen(), getBackground().getBlue())
        );
        cell.setForeground(isRowSelected
                ? card.isInvalid()
                        ? Color.LIGHT_GRAY
                        : getSelectionForeground()
                : card.isInvalid()
                        ? Color.GRAY
                        : defaultForeColor
        );

        return cell;
    }
 
Example 8
Source File: java_awt_Component.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Component getObject() {
    Component component = new MyComponent();
    component.setBackground(Color.WHITE);
    component.setFont(new Font(null, Font.BOLD, 5));
    return component;
}
 
Example 9
Source File: Test6978482.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) {
    Component c = new Component() {};
    c.setBackground(Color.WHITE);
    c.setForeground(Color.BLACK);
    Graphics g = new BufferedImage(1024, 768, BufferedImage.TYPE_INT_RGB).getGraphics();
    g.setClip(0, 0, 1024, 768);
    for (Border border : BORDERS) {
        System.out.println(border.getClass());
        border.getBorderInsets(c);
        border.paintBorder(c, g, 0, 0, 1024, 768);
    }
}
 
Example 10
Source File: JTagsPanel.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
public void setColors(Color background, Color foreground) {
	this.fontBackground = background;
	this.fontForeground = foreground;
	for (Component c : getComponents()) {
		if (c instanceof TagLabel) {
			c.setBackground(background);
			c.setForeground(foreground);
		}

	}
}
 
Example 11
Source File: BorderLayoutNotification.java    From JCommunique with MIT License 5 votes vote down vote up
/**
 * Adds a Component to the Notification.
 *
 * @param comp
 *            the Component to add
 * @param borderLayout
 *            the BorderLayout String, e.g. BorderLayout.NORTH
 */
public void addComponent(Component comp, String borderLayout) {
	m_panel.add(comp, borderLayout);

	WindowTheme theme = this.getWindowTheme();
	if (theme != null) {
		comp.setBackground(theme.background);
		comp.setForeground(theme.foreground);
	}
	getWindow().revalidate();
	getWindow().repaint();
}
 
Example 12
Source File: TypeRenderer.java    From OpenID-Attacker with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (value instanceof RequestType) {
        cell.setBackground(getColor((RequestType) value));
    }
    return cell;
}
 
Example 13
Source File: TabPanelLog.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
		boolean cellHasFocus) {
	Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
	
	if (value == null) {
		setText(prompt);
		return this;
	}

	setText(SOL + value);// + WHITESPACES);

	// 184,134,11 mud yellow
	// 255,229,204 white-ish (super pale) yellow
	// (37, 85, 118) navy blue
	// 131,172,234 pale sky blue

	if (isSelected) {
		if (theme == 7) {
			c.setBackground(new Color(184, 134, 11, 255)); // 184,134,11 mud yellow
			c.setForeground(Color.white);// new Color(255,229,204)); // 255,229,204 white-ish (super pale)
												// yellow
		} else {// if (theme == 0 || theme == 6) {
			c.setBackground(new Color(37, 85, 118, 255)); // (37, 85, 118) navy blue
			c.setForeground(Color.white);// new Color(131,172,234)); // 131,172,234 pale sky blue
		}

	} else {
		// unselected, and not the DnD drop location
		if (theme == 7) {
			c.setForeground(new Color(184, 134, 11)); // 184,134,11 mud yellow
			c.setBackground(new Color(255, 229, 204, 40)); // 255,229,204 white-ish (super pale) yellow
		} else {// if (theme == 0 || theme == 6) {
			c.setForeground(new Color(37, 85, 118));// (37, 85, 118) navy blue
			c.setBackground(new Color(131, 172, 234, 40)); // 131,172,234 pale sky blue
		}
	}
	// result.setOpaque(false);
	return c;
}
 
Example 14
Source File: FindTopComponent.java    From constellation with Apache License 2.0 5 votes vote down vote up
/**
 * Shades the FindCriteriaPanels blue and white consecutively.
 */
private void shader() {
    int panelFindCount = 0;

    for (int i = 0; i < panelFind.getComponentCount(); i++) {
        final Component item = panelFind.getComponent(i);
        if (item instanceof FindCriteriaPanel) {
            final Color color;

            // Shade every second panel:
            if ((panelFindCount % 2) == 0) {
                color = PALE_BLUE;
            } else {
                color = Color.WHITE;
            }

            item.setBackground(color);

            item.validate();
            item.repaint();

            panelFindCount++;
        }
    }

    panelFind.validate();
    panelFind.repaint();
}
 
Example 15
Source File: IKTasksTableModel.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
   IKTasksCell obj = (IKTasksCell)value;
   // Reset bg/fg colors before calling super.getTableCellRendererComponent()
   setBackground(null);
   setForeground(null);
   Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
   comp.setEnabled(obj.tasks.getEnabled(obj.index));
   // Override bg/fg colors for invalid entry
   if(obj.renderValidity() && !obj.tasks.isValid(obj.index)) {
      if(isSelected) comp.setForeground(invalidColor);
      else comp.setBackground(invalidColor);
   }
   return comp;
}
 
Example 16
Source File: MetadataOutlineCellRenderer.java    From snap-desktop with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Highlight the non editable cells making the foreground lighter.
 */

@Override
@SuppressWarnings("unchecked")
public Component getTableCellRendererComponent(final JTable table,
                                               final Object value,
                                               final boolean isSelected,
                                               final boolean hasFocus,
                                               final int row,
                                               final int column) {
    Component cell = null;
    Object valueToDisplay = value;
    if (value instanceof Property) {
        try {
            valueToDisplay = ((Property) value).getValue();
        } catch (IllegalAccessException | InvocationTargetException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    if (valueToDisplay != null) {
        TableCellRenderer renderer = table.getDefaultRenderer(valueToDisplay.getClass());
        if (renderer != null) {
            cell = renderer.getTableCellRendererComponent(table, valueToDisplay, isSelected,
                                                          hasFocus, row, column);
        }
    } else {
        cell = super.getTableCellRendererComponent(table, valueToDisplay, isSelected, hasFocus, row, column);
    }
    if (cell != null) {
        if (cell instanceof HtmlRenderer.Renderer) {
            ((HtmlRenderer.Renderer) cell).setIndent(5);
        } else if (cell instanceof DefaultTableCellRenderer.UIResource) {
            ((UIResource) cell).setHorizontalAlignment(JLabel.LEFT);
        }
        Color foregroundColor = table.getForeground();
        cell.setForeground(foregroundColor);
        cell.setBackground(row % 2 == 0 ? Color.WHITE : VERY_LIGHT_GRAY);
        if (isSelected) {
            cell.setBackground(table.getSelectionBackground());
        }
    }
    return cell;
}
 
Example 17
Source File: TabPanelSchedule.java    From mars-sim with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
		boolean cellHasFocus) {
	Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
	if (value == null) {
		setText(prompt);
		// this.setForeground(Color.orange);
		// this.setBackground(new Color(184,134,11));
		return this;
	}

	setText(SOL + value);// + SPACES);

	// 184,134,11 mud yellow
	// 255,229,204 white-ish (super pale) yellow
	// (37, 85, 118) navy blue
	// 131,172,234 pale sky blue

	if (isSelected) {
		if (theme == 7) {
			c.setBackground(new Color(184, 134, 11, 255)); // 184,134,11 mud yellow
			c.setForeground(Color.white);// new Color(255,229,204)); // 255,229,204 white-ish (super pale)
												// yellow
		} else {// if (theme == 0 || theme == 6) {
			c.setBackground(new Color(37, 85, 118, 255)); // (37, 85, 118) navy blue
			c.setForeground(Color.white);// new Color(131,172,234)); // 131,172,234 pale sky blue
		}

	} else {
		// unselected, and not the DnD drop location
		if (theme == 7) {
			c.setForeground(new Color(184, 134, 11)); // 184,134,11 mud yellow
			c.setBackground(new Color(255, 229, 204, 40)); // 255,229,204 white-ish (super pale) yellow
		} else {// if (theme == 0 || theme == 6) {
			c.setForeground(new Color(37, 85, 118));// (37, 85, 118) navy blue
			c.setBackground(new Color(131, 172, 234, 40)); // 131,172,234 pale sky blue
		}
	}
	// result.setOpaque(false);
	return c;
}
 
Example 18
Source File: UITools.java    From MtgDesktopCompanion with GNU General Public License v3.0 4 votes vote down vote up
public static void applyDefaultSelection(Component pane) {
		pane.setForeground(SystemColor.textHighlightText);
		pane.setBackground(SystemColor.inactiveCaption);
}
 
Example 19
Source File: CustomCellRenderer.java    From Java-Simple-Hotel-Management with GNU General Public License v3.0 4 votes vote down vote up
@Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        //System.out.println("workings");
/*
        
        if (value.equals("<<"))
        {
            c.setBackground(new java.awt.Color(0, 0, 255));
        }
        else 
        {
            c.setBackground(table.getBackground());
        }
        
        // for creating row header illusion
  
        */
        if(column == 0)
        {
            
            setForeground(table.getTableHeader().getForeground());
            setBackground(Color.LIGHT_GRAY);
            setFont(table.getTableHeader().getFont());
        }
        else
        {
            setBackground(table.getBackground());
        }
        
        
        if (value.equals(">"))
        {
            c.setBackground(new java.awt.Color(0, 0, 255));
        }
        else 
        {
            c.setBackground(table.getBackground());
        }
        

        return c;
    }
 
Example 20
Source File: PayPostTableCellRenderer.java    From Hotel-Properties-Management-System with GNU General Public License v2.0 3 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
		int row, int column) {
	
	final Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
	
	String rowVal = (String) table.getValueAt(row, 1);
	
	if(isSelected || hasFocus) {
		
		cellComponent.setBackground(table.getSelectionBackground());
	}
	
	else {
	
		if(rowVal.equals("CASH PAYMENT")) {
				
			cellComponent.setBackground(Color.decode("#75dbcf"));
				
		}
		else if(rowVal.equals("SYSTEM")) {
			
			cellComponent.setBackground(Color.decode("#d66c7f"));
		}
		else {
			
			cellComponent.setBackground(table.getBackground());
		}
	}
	return cellComponent;
}