Results 1 to 5 of 5
- 02-28-2009, 12:22 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
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) {}
-
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.
- 02-28-2009, 01:42 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
ah! It was missing "implements MouseListener" but now I've put that in it works ... thank you!
-
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.
- 03-01-2009, 05:01 PM #5
Similar Threads
-
[SOLVED] How to destroy a process when a button is clicked?
By pranav13 in forum AWT / SwingReplies: 11Last Post: 02-13-2009, 12:52 PM -
IE crashes when calendar icon on JSP is clicked - only for some browsers
By dnyan123 in forum JavaServer Pages (JSP) and JSTLReplies: 5Last Post: 11-12-2008, 09:19 AM -
i need help for MouseListener
By sfaxianovic in forum New To JavaReplies: 2Last Post: 08-21-2008, 03:30 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM -
code for number of clicks clicked on a web page???
By Sveta_cool in forum Advanced JavaReplies: 2Last Post: 02-01-2008, 02:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks