Thursday, July 28, 2011

Change JTextField Text Font Style

Design Free Website with Free Internet Marketing Tools

Output:
Code:

/**
 * File: jtextfieldFontStyle.java
 * Tiltle: How to Change JTextField Text Font Style
 * Author: http://java-program-sample.blogspot.com
 */

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

public class jtextfieldFontStyle extends JFrame {
 
 //Initializing JTextField and Class Font
 private JTextField arial, courier, verdana, tahoma;
 private Font arialAtt, courierAtt, verdanaAtt, tahomaAtt;

 //Setting up GUI
    public jtextfieldFontStyle() {
     
     //Setting up the Title of the Window
     super("How to Change JTextField Text Font Style");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(350,140);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     //Constructing JTextField with it's specified strings
     arial = new JTextField("Arial Text Font Style",22);
     courier = new JTextField("Courier Text Font Style",30);
     verdana = new JTextField("Verdana Text Font Style",19);
     tahoma = new JTextField("Tahoma Text Font Style",20);
     
     //Setting the properties of each Font reference
     arialAtt = new Font("Arial", Font.PLAIN, 14);
     courierAtt = new Font("Courier", Font.PLAIN, 14);
     verdanaAtt = new Font("Verdana", Font.PLAIN, 14);
     tahomaAtt = new Font("Tahoma", Font.PLAIN, 14);
     
     //Changing the JTextField Font Style using Font class references
     arial.setFont(arialAtt);
     courier.setFont(courierAtt);
     verdana.setFont(verdanaAtt);
     tahoma.setFont(tahomaAtt);

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     //Setting up the container layout
     FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
     pane.setLayout(flow);

     //Adding the JTextField components to the container
     pane.add(arial);
     pane.add(courier);
     pane.add(verdana);
     pane.add(tahoma);

     /**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) {
     jtextfieldFontStyle jtfs = new jtextfieldFontStyle();
 }
}

Important Part of the Program:

//Initializing JTextField and Class Font
private JTextField arial, courier, verdana, tahoma;
private Font arialAtt, courierAtt, verdanaAtt, tahomaAtt;

//Constructing JTextField with it's specified strings
arial = new JTextField("Arial Text Font Style",22);
courier = new JTextField("Courier Text Font Style",30);
verdana = new JTextField("Verdana Text Font Style",19);
tahoma = new JTextField("Tahoma Text Font Style",20);
     
//Setting the properties of each Font reference
arialAtt = new Font("Arial", Font.PLAIN, 14);
courierAtt = new Font("Courier", Font.PLAIN, 14);
verdanaAtt = new Font("Verdana", Font.PLAIN, 14);
tahomaAtt = new Font("Tahoma", Font.PLAIN, 14);
     
//Changing the JTextField Font Style using Font class references
arial.setFont(arialAtt);
courier.setFont(courierAtt);
verdana.setFont(verdanaAtt);
tahoma.setFont(tahomaAtt);

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