Results 1 to 5 of 5
  1. #1
    sailor_girl is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default MouseListener - clearing a textField when clicked on?!

    Hi everyone,

    I'm not sure if this is the right place for my question, but I was wondering if you could help me.

    I am trying to create a basic java graphics program and as part of it I have a textField where the user types a description of the image. I have set the textField to initially display "What's in the image?" and would like this text to vanish when the user clicks on the textField.

    I tried adding a MouseListener to the textField (it already has an addActionListener which checks the user's entry on submission) but I keep getting the following error:

    "addMouseListener(java.awt.event.MouseListener ) in java.awt.Component cannot be applied to (programName)"

    Is there another way I can achieve what I want to do?!

    I have included the relevant sections of my code below:

    Java Code:
    Box form()
        {
        	//JTextField textField;;
          
            Border border = BorderFactory.createEmptyBorder(10, 10, 10, 10);
            textField = new JTextField(15);
            textField.setText("What's in the picture?");
           
            textField.addActionListener(this);
            textField.addMouseListener(this);
    
            Box box = Box.createVerticalBox();
            box.setBorder(border);
            box.add(textField);
            /*box.add(options());*/
            return box;
        }
        
        public void mouseClicked(MouseEvent e) {
        	textField.setText("");
        }
        
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}

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

    Default

    I'm not sure that you truly have included the relevant code. For instance, does that class in fact state at the top: implements MouseListener? I think that you should create a small compilable program that is small enough to post here, that demonstrates your problem, and post it here.

  3. #3
    sailor_girl is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default

    ah! It was missing "implements MouseListener" but now I've put that in it works ... thank you!

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

    Default

    You're welcome!

    About this time I usually go into a diatribe on how you shouldn't have your GUI class implement MouseListener, ActionListener, or similar interfaces, that you will be far better off using separate distinct inner classes, either anonymous or not depending on size.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Now that you have your mouse listener working, are you sure you didn't want to listen for a focus event? What if the user tabs to the field? Also, you might want to check the text before you erase it. The user might have typed something there and want to change it, not retype it...

Similar Threads

  1. Replies: 11
    Last Post: 02-13-2009, 12:52 PM
  2. IE crashes when calendar icon on JSP is clicked - only for some browsers
    By dnyan123 in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 11-12-2008, 09:19 AM
  3. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM
  4. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM
  5. code for number of clicks clicked on a web page???
    By Sveta_cool in forum Advanced Java
    Replies: 2
    Last Post: 02-01-2008, 02:55 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
  •