Output:
Code:
/** * File: jtextfieldBackground.java * Tiltle: How to Change JTextField Background Color * Author: http://java-program-sample.blogspot.com */ //Java Core Package import javax.swing.*; //Java Extension Package import java.awt.*; public class jtextfieldBackground extends JFrame { //Initializing JTextField private JTextField field; //Setting up GUI public jtextfieldBackground() { //Setting up the Title of the Window super("How to Change JTextField Background Color"); //Set Size of the Window (WIDTH, HEIGHT) setSize(350,100); //Exit Property of the Window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Constructing JTextField with a size of 20 with a specified string "Enter Text Here..." field = new JTextField("Enter Text Here...",20); //Changing JTextField Background Color field.setBackground(Color.RED); //Setting up the container ready for the components to be added. Container pane = getContentPane(); setContentPane(pane); //Adding the JTextField component to the container with a BorderLayout NORTH pane.add(field, BorderLayout.NORTH); /**Set all the Components Visible. * If it is set to "false", the components in the container will not be visible. */ setVisible(true); } //Main Method public static void main (String[] args) { jtextfieldBackground jtfb = new jtextfieldBackground(); } }
Important Part of the Program:
//Constructing JTextField with a size of 20 with a specified string "Enter Text Here..." field = new JTextField("Enter Text Here...",20); //Changing JTextField Background Color field.setBackground(Color.RED);