change swing Text Component Background - Java Swing

Java examples for Swing:JTextComponent

Description

change swing Text Component Background

Demo Code


//package com.java2s;
import java.awt.Component;

import javax.swing.JTextField;

import javax.swing.UIManager;

import javax.swing.text.JTextComponent;

public class Main {
    private static void changeTxtComponentBackground(Component txtComp,
            boolean changeTxtFieldBg, boolean changeTextAreaBg) {
        if (!(txtComp instanceof JTextComponent)) {
            return;
        }/*from  w  w  w  .j  ava 2 s  .  c  om*/
        if (txtComp instanceof JTextField) {
            if (changeTxtFieldBg) {
                txtComp.setBackground(UIManager
                        .getColor("TextField.inactiveBackground"));
            } else {
                txtComp.setBackground(UIManager
                        .getColor("TextField.background"));
            }
        } else {
            if (changeTextAreaBg) {
                txtComp.setBackground(UIManager
                        .getColor("TextField.inactiveBackground"));
            } else {
                txtComp.setBackground(UIManager
                        .getColor("TextField.background"));
            }
        }
    }
}

Related Tutorials