Results 1 to 6 of 6
Thread: JTextField Question
- 05-09-2010, 01:50 AM #1
JTextField Question
when you create a text field you can add what it would say and size in the parameters
like so:
is it possible for the text inside the field to delete itself when the user clicks on it, enters something, or presses delete?Java Code:JTextField text = new JTextField("Enter Text",15);
- 05-09-2010, 03:21 AM #2
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
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:
The methods that have no code in them are required whenever implementing these interfaces.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) { } }
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.
- 05-09-2010, 03:24 AM #3
i never thought of it that way but thanks:)
-
- 05-09-2010, 04:11 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 88
- Rep Power
- 0
- 05-09-2010, 07:44 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Similar Threads
-
JTextField
By gancio in forum AWT / SwingReplies: 20Last Post: 08-26-2009, 03:11 PM -
JTextField question
By Chasingxsuns in forum New To JavaReplies: 5Last Post: 07-14-2009, 02:39 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JtextField
By kashifu in forum Advanced JavaReplies: 2Last Post: 06-27-2008, 04:25 PM -
help with JTextfield
By gary in forum New To JavaReplies: 4Last Post: 07-11-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks