Results 1 to 6 of 6
  1. #1
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Question JTextField Question

    when you create a text field you can add what it would say and size in the parameters

    like so:
    Java Code:
    JTextField text = new JTextField("Enter Text",15);
    is it possible for the text inside the field to delete itself when the user clicks on it, enters something, or presses delete?

  2. #2
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    let me understand what you want.

    when they click it, you want to clear it? (mouseListener)
    when they press enter, you want it to clear?(keyListener)
    when they press delete, you want it to clear?(also keyListener)
    code:

    Java Code:
    import java.applet.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class TextFieldTest extends JApplet implements KeyListener, MouseListener
    {
        JTextField field;
        public void init ()
        {
            setSize (100, 100);
            getContentPane ().setBackground (Color.white);
            getContentPane ().setLayout (null);
    
            field = new JTextField ("EnterText");
            field.setBounds (0, 0, 100, 30);
            field.addMouseListener (this);
            field.addKeyListener (this);
    
            getContentPane ().add (field);
        }
    
    
    
        public void keyPressed (KeyEvent e)
        {
            if (e.getKeyCode () == 10 || e.getKeyCode () == 127)
            {
                field.setText ("");
            }
        }
    
    
        public void mousePressed (MouseEvent e)
        {
            if (e.getSource () == field)
                field.setText ("");
        }
    
    
        public void mouseReleased (MouseEvent e)
        {
        }
    
    
        public void mouseClicked (MouseEvent e)
        {
        }
    
    
        public void mouseEntered (MouseEvent e)
        {
        }
    
    
        public void mouseExited (MouseEvent e)
        {
        }
    
    
        public void keyTyped (KeyEvent e)
        {
        }
    
    
        public void keyReleased (KeyEvent e)
        {
        }
    
    }
    The methods that have no code in them are required whenever implementing these interfaces.

    if you have any questions about this, feel free :P

    and someone might post as easier solution, this is how i would do it though.

  3. #3
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    i never thought of it that way but thanks:)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Cruncher View Post
    when they press enter, you want it to clear?(keyListener)
    ActionListener for this one.

  5. #5
    Cruncher is offline Member
    Join Date
    Mar 2010
    Posts
    88
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    ActionListener for this one.
    i was thinking that but, if you're already implementing the keylistener aanyway, why not just use that?

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

Similar Threads

  1. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 04:11 PM
  2. JTextField question
    By Chasingxsuns in forum New To Java
    Replies: 5
    Last Post: 07-14-2009, 03:39 AM
  3. Replies: 1
    Last Post: 01-30-2009, 07:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 05:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •