Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java swing JTextField set PlaceHolder [duplicate]

I created a JTextField and now I want to set the placeholder on that JTextField, but I don't know how? Please help. Here is my code:

JTextField database=new JTextField("Enter Data Base Name"); database.setPreferredSize(database.getPreferredSize()); database.setText(""); 
like image 298
Renish Khunt Avatar asked Apr 25 '13 11:04

Renish Khunt


People also ask

What is the difference between JTextField and JTextArea?

The main difference between JTextField and JTextArea in Java is that a JTextField allows entering a single line of text in a GUI application while the JTextArea allows entering multiple lines of text in a GUI application.

How can we make JTextField accept only numbers in Java?

By default, a JTextField can allow numbers, characters, and special characters. Validating user input that is typed into a JTextField can be difficult, especially if the input string must be converted to a numeric value such as an int. In the below example, JTextField only allows entering numeric values.

Can you enter more than one line in a JTextField?

Only one line of user response will be accepted. If multiple lines are desired, JTextArea will be needed. As with all action events, when an event listener registers an event, the event is processed and the data in the text field can be utilized in the program.

What is JTextField in Java Swing?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt. TextField where it is reasonable to do so.


2 Answers

Try this class:

package playground;  import java.awt.*;  import javax.swing.*; import javax.swing.text.Document;  @SuppressWarnings("serial") public class PlaceholderTextField extends JTextField {      public static void main(final String[] args) {         final PlaceholderTextField tf = new PlaceholderTextField("");         tf.setColumns(20);         tf.setPlaceholder("All your base are belong to us!");         final Font f = tf.getFont();         tf.setFont(new Font(f.getName(), f.getStyle(), 30));         JOptionPane.showMessageDialog(null, tf);     }      private String placeholder;      public PlaceholderTextField() {     }      public PlaceholderTextField(         final Document pDoc,         final String pText,         final int pColumns)     {         super(pDoc, pText, pColumns);     }      public PlaceholderTextField(final int pColumns) {         super(pColumns);     }      public PlaceholderTextField(final String pText) {         super(pText);     }      public PlaceholderTextField(final String pText, final int pColumns) {         super(pText, pColumns);     }      public String getPlaceholder() {         return placeholder;     }      @Override     protected void paintComponent(final Graphics pG) {         super.paintComponent(pG);          if (placeholder == null || placeholder.length() == 0 || getText().length() > 0) {             return;         }          final Graphics2D g = (Graphics2D) pG;         g.setRenderingHint(             RenderingHints.KEY_ANTIALIASING,             RenderingHints.VALUE_ANTIALIAS_ON);         g.setColor(getDisabledTextColor());         g.drawString(placeholder, getInsets().left, pG.getFontMetrics()             .getMaxAscent() + getInsets().top);     }      public void setPlaceholder(final String s) {         placeholder = s;     }  } 
like image 95
Peter Tseng Avatar answered Sep 18 '22 08:09

Peter Tseng


JTextField searchText; 

...

In constructor:

searchText = new JTextField("Search"); searchText.setForeground(Color.GRAY); searchText.addFocusListener(new FocusListener() {     @Override     public void focusGained(FocusEvent e) {         if (searchText.getText().equals("Search")) {             searchText.setText("");             searchText.setForeground(Color.BLACK);         }     }     @Override     public void focusLost(FocusEvent e) {         if (searchText.getText().isEmpty()) {             searchText.setForeground(Color.GRAY);             searchText.setText("Search");         }     }     }); 
like image 23
Graywolf Avatar answered Sep 18 '22 08:09

Graywolf