1

Code below allows to change default TextFieldUI to allow you to have hints shown on the JTextField. The problem I'm having is, I'm using nimbus look and feel for my program and when I change this textfieldUI, everything works fine, as it should, but textfield look and feel changes to ugly looking one. I think, since I'm overriding defaults I should set look and feel in this code manually, but just don't know how to do it!

import java.awt.Color;
java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.plaf.basic.BasicTextFieldUI;
import javax.swing.text.JTextComponent;

public class HintTextFieldUI extends BasicTextFieldUI implements FocusListener {

private String hint;
private boolean hideOnFocus;
private Color color;

public Color getColor() {
    return color;
}

public void setColor(Color color) {
    this.color = color;
    repaint();
}

private void repaint() {
    if(getComponent() != null) {
        getComponent().repaint();           
    }
}

public boolean isHideOnFocus() {
    return hideOnFocus;
}

public void setHideOnFocus(boolean hideOnFocus) {
    this.hideOnFocus = hideOnFocus;
    repaint();
}

public String getHint() {
    return hint;
}

public void setHint(String hint) {
    this.hint = hint;
    repaint();
}
public HintTextFieldUI(String hint) {
    this(hint,false);
}

public HintTextFieldUI(String hint, boolean hideOnFocus) {
    this(hint,hideOnFocus, null);
}

public HintTextFieldUI(String hint, boolean hideOnFocus, Color color) {
    this.hint = hint;
    this.hideOnFocus = hideOnFocus;
    this.color = color;
}

@Override
protected void paintSafely(Graphics g) {
    super.paintSafely(g);
    JTextComponent comp = getComponent();
    comp.setu
    if(hint!=null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))){
        if(color != null) {
            g.setColor(color);
        } else {
            g.setColor(comp.getForeground().brighter().brighter().brighter());              
        }
        int padding = (comp.getHeight() - comp.getFont().getSize())/2;
        g.drawString(hint, 2, comp.getHeight()-padding-1);          
    }
}

@Override
protected void installListeners() {
    super.installListeners();
    getComponent().addFocusListener(this);
}
@Override
protected void uninstallListeners() {
    super.uninstallListeners();
    getComponent().removeFocusListener(this);
}

public void focusGained(FocusEvent e) {
    // TODO Auto-generated method stub
    if(hideOnFocus) repaint();
}

public void focusLost(FocusEvent e) {
    // TODO Auto-generated method stub
    if(hideOnFocus) repaint();  
}

}

tckmn
  • 57,719
  • 27
  • 114
  • 156
user1423481
  • 65
  • 1
  • 1
  • 5
  • Don't you think that you could/should do all this without extending TextFieldUI but rather extend `JTextField` and override `paintComponent()`? – Guillaume Polet Mar 03 '13 at 14:27
  • Also see [Java JTextField with input hint](http://stackoverflow.com/q/1738966/1048330) – tenorsax Mar 03 '13 at 16:24
  • i looked at that aleary, but it does not show how to set look&feel of overridden textfieldUI. I tried all that code, textfiled comes out to hava simple style,not of nimbus style. Any more suggestions will be apprciated! – user1423481 Mar 03 '13 at 16:51

1 Answers1

1

I don't know what the nimbus style is like, but maybe Text Prompt will work for you.

camickr
  • 321,443
  • 19
  • 166
  • 288