draw Empty Prompt JTextField - Java Swing

Java examples for Swing:JTextField

Description

draw Empty Prompt JTextField

Demo Code


import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JTextField;
import sun.swing.SwingUtilities2;

public class Main{
    public static void drawEmptyPrompt(Graphics g, JTextField textField,
            Rectangle vr) {//from   w w  w  .jav a 2  s. c om
        if (!(textField instanceof MetroPromptTextField)) {
            return;
        }
        MetroPromptTextField tf = (MetroPromptTextField) textField;
        if (tf.getEmptyPrompt() != null && textField.getText().isEmpty()
                && textField.isEnabled() && !textField.hasFocus()) {
            MetroLookAndFeel
                    .getLogger()
                    .info(String
                            .format("Painting text field background (want to draw empty prompt) %d %d.",
                                    vr.x, vr.y));
            g.setColor(Color.LIGHT_GRAY);
            g.setFont(textField.getFont().deriveFont(Font.ITALIC));
            // g.setFont(g.getFont().deriveFont(Font.ITALIC));
            FontMetrics fm = g.getFontMetrics();
            SwingUtilities2.drawString(textField, g, tf.getEmptyPrompt(),
                    vr.x, vr.y + fm.getAscent());
        }
    }
}

Related Tutorials