Thursday, May 10, 2012

Transparent JTextField | The Simple and Basic way to do it

Design Free Website with Free Internet Marketing Tools

Program Description:

Today I have created a very simple Java Program on how to create a transparent JTextField. This is just a basic way on how to create a 100% transparent JTextField which means you won't see anything but can type a text or an input. Even if you change the background color still you won't see anything because it is totally transparent. To know how, see the code below and the sample screenshots.

Output:


Code:

/**
 * File: simpleTransparentJTF.java
 * Tiltle: Transparent JTextField | The Simple and Basic way to do it.
 * Author: http://java-program-sample.blogspot.com/
 */

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

public class simpleTransparentJTF extends JFrame {
 
 //Constructing JTextField
 JTextField field = new JTextField("Type your Text Here...",20);
 
 //Initiazlizing the class Font to set our JTextField text style
 Font defualtFont;

 //Setting up GUI
    public simpleTransparentJTF() {
     
     //Setting up the Title of the Window
     super("Transparent JTextField");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(370,85);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     //Constructing class Font property
     defualtFont = new Font("Arial", Font.BOLD, 18);
     
     //Setting JTextField Properties
     field.setFont(defualtFont);
     field.setPreferredSize(new Dimension(150,40));
     field.setForeground(Color.BLACK);
     
     //Step 1: Remove the border line to make it look like a flat surface.
     field.setBorder(BorderFactory.createLineBorder(Color.white, 0));
     
     //Step 2: Set the background color to null to remove the background.
     field.setBackground(null);

     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
     
     //Change the background color of the container to see
     //if the JTextField is really transparent.
     pane.setBackground(Color.YELLOW);
     
     //Adding JTextField to our container
     pane.add(field);

     //Setting up the container layout
     FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
     pane.setLayout(flow);

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
     
     //Disable Frame Size
     setResizable(false);
    }
    
 //Main Method
    public static void main (String[] args) {
     simpleTransparentJTF pjtf = new simpleTransparentJTF();
 }
}

To test if it is really transparent, you can change the background color of the window by just changing the color if this line below:

//Change the background color of the container to see
     //if the JTextField is really transparent.
     pane.setBackground(Color.YELLOW);

Important Part of the Program:

//Step 1: Remove the border line to make it look like a flat surface.
     field.setBorder(BorderFactory.createLineBorder(Color.white, 0));
     
     //Step 2: Set the background color to null to remove the background.
     field.setBackground(null);

Note: This techniques won't give you a very nice output if you are going to put a graphic image at the back of the JTextField because it will just ignore the image. Do not use images as background of your window if you are going to use this technique. You can use different background colors using the class Color.

If you have any questions or comments about the program, feel free to post it here.

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