Thursday, July 28, 2011

Change JTextField Foreground Color

Java E-Book How to Program Java

Output:

Code:

/**
 * File: jtextfieldForeground.java
 * Tiltle: How to Change JTextField Foreground Color
 * Author: http://java-program-sample.blogspot.com
 */

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class jtextfieldForeground extends JFrame {
 
 //Initializing JTextField
 private JTextField field;

 //Setting up GUI
    public jtextfieldForeground() {
     
     //Setting up the Title of the Window
     super("How to Change JTextField Foreground Color");

     //Set Size of the Window (WIDTH, LENGTH)
     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 Foreground Color
     field.setForeground(Color.BLUE);

     //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) {
     jtextfieldForeground jtff = new jtextfieldForeground();
 }
}

Important Part of the Program:

//Initializing JTextField
 private JTextField field;

//Constructing JTextField with a size of 20 with a specified string "Enter Text Here..."
     field = new JTextField("Enter Text Here...",20);
     
     //Changing JTextField Foreground Color
     field.setForeground(Color.BLUE);

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Hostgator Discount Code