somebody know how to make the text which is in the text field on click to disapear
ex: If saying "Search" when ill click on the text field the text to disapear and i can write ...
Thanks ! :)

3 Years
Discussion Span
Last Post by bguild
Featured Replies
  • 1

    I think a [FocusListener](http://docs.oracle.com/javase/tutorial/uiswing/events/focuslistener.html) is what you need. Also re-read [this post](http://www.daniweb.com/software-development/java/threads/446807/save-in-file-and-read-from-it#post1928659). Read More

  • 2

    After a few experiments I find that I get good results with implementing a custom `JPanel` to hold a `JTextField`. Instead of putting the text in the `JTextField`'s `Document`, I override `paint` on the `JPanel` so that I can draw over the children of the panel and use that to … Read More


You can just do it by javascript.
Onclick event just change the element value with "".
For eg:-

function makeBlank()

<input type="text" value="Search" onclick="makeBlank();" id="search">

Is this for Java using applet/swing or J2EE?
If it is for J2EE then add it in your html page.Otherwise i will reply for Java swing/applet.
Please show your code ,so that we can help.

Edited by IIM


look i have notes program and i want at the textfield for name to write "Enter name" and in noteArea to write "Place some notes" and when you will click on one of theese to disapear and you continue writing so its not for online purposes hope you understand what i mean ..


dude there is no code for what im looking for just simple program made by NetBeans but nvm here is the code for my project ...

this is for Save Button

private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        FileReader freader = null;
        try {
            File folder = new File(mainAccName);
            String notename = nameOfNote.getText();
            String noteareaa = noteArea.getText();
            if (notename.isEmpty() || noteareaa.isEmpty()) {
                if (notename.isEmpty()) {
                    if (noteareaa.isEmpty()) {
                        LabelInfo.setText("You must have name and note");
                    } else {
                        LabelInfo.setText("You dont have name");
                } else {
                    LabelInfo.setText("You dont have note");
            File file = new File(folder, "Notes.txt");
            FileWriter filewriter = new FileWriter(file);
            BufferedWriter out = new BufferedWriter(filewriter);
            out.write("Name: " + notename);
            out.write("Description: " + noteareaa);
        } catch (Exception ex) {
            noteArea.setText("Something bad happened !\n");
            noteArea.setText("The text can't be saved !");

This is for Review Button

private void ReviewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            File folder = new File(mainAccName);
            String Notename = nameOfNote.getText();
            String notearea = noteArea.getText();
            Desktop desktop = null;
            if (Desktop.isDesktopSupported()) {
                desktop = Desktop.getDesktop();
            File file = new File(folder, "Notes.txt");
            FileReader filereader = new FileReader(file);
            BufferedReader out = new BufferedReader(filereader);
            desktop.open(new File(folder, "Notes.txt"));
        } catch (IOException ioe) {

After a few experiments I find that I get good results with implementing a custom JPanel to hold a JTextField. Instead of putting the text in the JTextField's Document, I override paint on the JPanel so that I can draw over the children of the panel and use that to draw the text I want over the JTextField if the field is empty and doesn't have focus. I also add a FocusListener that calls repaint() on my custom JPanel for the field losing or gaining focus.

This way you don't have to deal with any complications from having the text that you want to disappear being actually in the field. You don't have to worry about confusing your text with text that the user actually entered, and if the user leaves the field blank your text reappears.

Edited by bguild

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.