Java Utililty Methods JComponent Color

List of utility methods to do JComponent Color

Description

The list of methods to do JComponent Color are organized into topic(s).

Method

voidapplyBackgroundToSubComponents(Container container, Color color)
apply Background To Sub Components
container.setBackground(color);
for (Component component : getComponents(container)) {
    if (component instanceof JPanel) {
        component.setBackground(color);
JTextPanebuildTextPane(String text, Color foreground)
Formats the text and displays it in a JTextPane .
if (text == null)
    text = "";
StyleContext context = new StyleContext();
StyledDocument document = new DefaultStyledDocument(context);
Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
if (foreground != null)
    StyleConstants.setForeground(style, foreground);
...
Stringcolor(String string, AttributeSet set)
color
if (set == null)
    return string;
if (string.trim().length() == 0) {
    return string.replace(" ", "&nbsp;").replace("\n", "<br>"); 
StringBuilder buf = new StringBuilder(string);
if (StyleConstants.isBold(set)) {
    buf.insert(0, "<b>"); 
...
ColorconvertHtmlColorNameToColor(String htmlColorName)
convert Html Color Name To Color
StyleSheet sheet = new StyleSheet();
return sheet.stringToColor(htmlColorName.toLowerCase());
JColorChoosercreateColorChooser(Component component)
create Color Chooser
JColorChooser colorChooser;
colorChooser = new JColorChooser();
colorChooser.setBorder(BorderFactory.createTitledBorder("Color"));
colorChooser.setColor(component.getBackground());
colorChooser.setPreviewPanel(new JPanel());
return colorChooser;
JTextComponentcreateDisabledTextComponent(String text, Color color, boolean textField)
create Disabled Text Component
JTextComponent field;
if (textField) {
    field = new JTextField(text);
} else {
    field = new JTextArea(DEFAULT_TEXT_COMPONENT_ROWS, DEFAULT_TEXT_COMPONENT_COLUMNS);
    ((JTextArea) field).setLineWrap(true);
    ((JTextArea) field).setWrapStyleWord(true);
    field.setText(text);
...
JTextComponentcreateDisabledTextField(String text, Color color)
create Disabled Text Field
return createDisabledTextComponent(text, color, true);
JSeparatorcreateHorizontalLine(Color background)
create Horizontal Line
final boolean customPaint = isNimbus() || isAquaLookAndFeel();
JSeparator separator = new JSeparator() {
    public Dimension getMaximumSize() {
        return new Dimension(super.getMaximumSize().width, 1);
    public Dimension getPreferredSize() {
        return new Dimension(super.getPreferredSize().width, 1);
    public void paint(Graphics g) {
        if (customPaint) {
            g.setColor(getDisabledLineColor());
            g.fillRect(0, 0, getWidth(), getHeight());
        } else {
            super.paint(g);
};
separator.setBackground(background);
return separator;
JLabelcreateLabel(final String label, final int alignment, final Color background, final Color foreground)
Creates a label with the given properties already preset.
JLabel presetLabel = new JLabel(label, alignment);
presetLabel.setForeground(foreground);
presetLabel.setBackground(background);
presetLabel.setOpaque(true);
return presetLabel;
JLabelcreateLabel(JComponent parent, String text, Color color)
Creates and returns a label with the given text and color.
JLabel label = createLabel(parent, text);
label.setForeground(color);
return label;