How can I add above type text into a JTextfield???????
I mean the faded texts as "username"..

It should be ok to erase them when entering text into the field..

How can I do it??

thankx in advance.

Attachments uname.JPG 2.8 KB

maybe change the foreground color to light gray so it seems to be shaded

Its a text field and i want to retreive what would be typed in it.
the text "password" in shaded color cannot be included when it cal the method "getText()"

:S

You can extend the JTextField and JPasswordField components as shown in the example below

import java.awt.Color;
import java.awt.Font;
import java.awt.*;
import java.awt.font.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;

class BGTextField extends JTextField {
  BufferedImage img;
  TexturePaint texture;
  String bgText;

  public BGTextField(String bgText)  {
    super();    
    this.bgText = bgText;
  }

  public void paintComponent(Graphics g) {
   	/*Create a buffered image and write the text at the center of the buffered image */
	img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D bufG = img.createGraphics();
	bufG.setColor(Color.white);
	bufG.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
	bufG.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            Font font = g.getFont();
            bufG.setFont(font);
            FontRenderContext frc = bufG.getFontRenderContext();
            float width = (float)font.getStringBounds(bgText, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(bgText, frc);
            float height = lm.getAscent() + lm.getDescent();
            float x = (img.getWidth() - width)/2f;
            float y = (img.getHeight() + height)/2f - lm.getDescent();
            bufG.setColor(new Color(225, 225, 225));
            bufG.drawString(bgText, x, y);
	bufG.dispose();

	
	Rectangle rect = new Rectangle(0, 0, img.getWidth(null), img.getHeight(null));
            texture = new TexturePaint(img, rect);
            setOpaque(false);


	Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(texture);
            g.fillRect(0, 0, getWidth(), getHeight());

            super.paintComponent(g);
  }
}


class BGPasswordField extends JPasswordField {
  BufferedImage img;
  TexturePaint texture;
  String bgText;

  public BGPasswordField(String bgText)  {
    super();    
	this.bgText = bgText;
  }

  public void paintComponent(Graphics g) {
   	/*Create a buffered image and write the text at the center of the buffered image */
	img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D bufG = img.createGraphics();
	bufG.setColor(Color.white);
	bufG.fillRect(0, 0, img.getWidth(null), img.getHeight(null));
	bufG.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            Font font = g.getFont();
            bufG.setFont(font);
            FontRenderContext frc = bufG.getFontRenderContext();
            float width = (float)font.getStringBounds(bgText, frc).getWidth();
            LineMetrics lm = font.getLineMetrics(bgText, frc);
            float height = lm.getAscent() + lm.getDescent();
            float x = (img.getWidth() - width)/2f;
            float y = (img.getHeight() + height)/2f - lm.getDescent();
            bufG.setColor(new Color(225, 225, 225));
            bufG.drawString(bgText, x, y);
	bufG.dispose();


	Rectangle rect = new Rectangle(0, 0, img.getWidth(null), img.getHeight(null));
            texture = new TexturePaint(img, rect);
            setOpaque(false);


	Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(texture);
            g.fillRect(0, 0, getWidth(), getHeight());

            super.paintComponent(g);
  }
}


public class MyExample{
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setLayout(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    JTextField textfield = new BGTextField("username");
    textfield.setLocation(5, 5);
    textfield.setSize(150,20);
    frame.getContentPane().add(textfield);

    JPasswordField pwdfield = new BGPasswordField("password");
    pwdfield.setLocation(165, 5);
    pwdfield.setSize(150,20);
    frame.getContentPane().add(pwdfield);

    frame.setSize(350, 60);
    frame.setVisible(true);

  }

}
This article has been dead for over six months. Start a new discussion instead.