JTextField component represents a text field that you can edit a single line of text. Unlike TextField, JTextField allows you to decorate the border of the text field, and specify the color of selected text. JTextField does not support password functionality. If you want password input, you need to use the JPasswordField instead.
The JTextField component can generate document events. A document event can be received by implementing the DocumentListerner interface and registering the listener by invoking the addDocumentListener(DocumentListener listener) method. The DocumentListener interface has three methods: removeUpdate(DocumentEvent e), insertUpdate(DocumentEvent e), and changedUpdate(DocumentEvent e) to be overridden.
Constructors of the JTextField class:
JTextField()
creates an empty text field.
JTextField(int columns)
creates a text field with specified number of columns.
JTextField(String text)
creates a text field with text.
JTextField(String text,int columns)
creates a text field with text and specified number of columns.
Useful methods of the JTextField:
getText():String
returns the text of the text field.
setBorder(Border bd):void
specifies the border of the text field.
select(int start,int end):void
selects the text in the text field from start to end positions.
setSelectionColor(Color c):void
specifies the color of the selected part.
setSelectedTextColor(Color c):void
specifies the selected text color.
setText(String text):void
sets text to the text field.
Example:
First of all import this classes
The JTextField component can generate document events. A document event can be received by implementing the DocumentListerner interface and registering the listener by invoking the addDocumentListener(DocumentListener listener) method. The DocumentListener interface has three methods: removeUpdate(DocumentEvent e), insertUpdate(DocumentEvent e), and changedUpdate(DocumentEvent e) to be overridden.
Constructors of the JTextField class:
JTextField()
creates an empty text field.
JTextField(int columns)
creates a text field with specified number of columns.
JTextField(String text)
creates a text field with text.
JTextField(String text,int columns)
creates a text field with text and specified number of columns.
Useful methods of the JTextField:
getText():String
returns the text of the text field.
setBorder(Border bd):void
specifies the border of the text field.
select(int start,int end):void
selects the text in the text field from start to end positions.
setSelectionColor(Color c):void
specifies the color of the selected part.
setSelectedTextColor(Color c):void
specifies the selected text color.
setText(String text):void
sets text to the text field.
Example:
First of all import this classes
import java.awt.Color; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.plaf.basic.BasicBorders;
After that goto WindowActivated event and write code like below.
private void formWindowActivated(java.awt.event.WindowEvent evt) { txtDemo.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { System.out.println("a remove insert fire."); //To change body of generated methods, choose Tools | Templates. } @Override public void removeUpdate(DocumentEvent e) { System.out.println("a remove Update fire."); //To change body of generated methods, choose Tools | Templates. } @Override public void changedUpdate(DocumentEvent e) { System.out.println("a change update fire."); //To change body of generated methods, choose Tools | Templates. } }); txtDemo.select(0, 10); txtDemo.setSelectedTextColor(Color.CYAN); txtDemo.setBorder(new BasicBorders.FieldBorder(Color.BLUE, Color.BLACK, Color.GREEN, Color.LIGHT_GRAY)); }
End of all you window look like this:
![]() |
JTextField Example |
0 comments:
Post a Comment