![]() |
||
![]() |
![]() Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses. |
|
|
JAVA, JSP, SERVLETS, TOMCAT, SERVLETS MANAGER, |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
JTextField | What this section covers: basic text fields. |
JFormattedTextField
|
A JTextField subclass
that allows you to specify the legal set of characters
that the user can enter.
See
How to Use Formatted Text Fields.
|
JPasswordField
|
A JTextField subclass
that does not show the characters that the user types.
See
How to Use Password Fields.
|
JComboBox
| Can be edited, and provides a menu of strings to choose from. See How to Use Combo Boxes. |
JSpinner
| Combines a formatted text field with a couple of small buttons that enables the user to choose the previous or next available value. See How to Use Spinners. |
The following example displays a basic text field and a text area. The text field is editable. The text area is not editable. When the user presses Enter in the text field, the program copies the text field's contents to the text area, and then selects all the text in the text field.

Click the Launch button to run TextDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.

TextDemo.java.
The following code
creates and sets up the text field:
The integer argument passed to thetextField = new JTextField(20);
JTextField constructor,
20 in the example,
indicates the number of columns in the field.
This number is used along with metrics provided by
the field's current font
to calculate the field's preferred width.
It does not limit the number of characters the user
can enter.
To do that, you can either use a
formatted text field
or a
document listener,
as described in
Text Component Features.
Note: We encourage you to specify the number of columns for each text field. If you do not specify the number of columns or a preferred size, then the field's preferred size changes whenever the text changes, which can result in unwanted layout updates.
The next line of code
registers a TextDemo object
as an action listener for the text field.
ThetextField.addActionListener(this);
actionPerformed method
handles action events from the text field:
private final static String newline = "\n";
...
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
}
Notice the use of JTextField's getText
method to retrieve the text currently contained by the text field.
The text returned by this method
does not include a newline character
for the Enter key that fired the action event.
You have seen how a basic text field can be used.
Because the JTextField class inherits from
the JTextComponent class,
text fields are very flexible and can be customized almost
any way you like.
For example, you can add a document listener
or a document filter
to be notified when the text changes, and
in the filter case you can
modify the text field accordingly.
Information on text components can be found in
Text Component Features.
Before customizing a JTextField,
however,
make sure that one of the other
components based on text fields
will not do the job for you.
Often text fields are paired with labels that describe the text fields. See Examples That Use Text Fields for pointers on creating these pairs.
TheTextFieldDemoexample introduces a text field and a text area. You can find the entire code for this program inTextFieldDemo.java.As you type characters in the text field the program searches for the typed text in the text area. If the entry is found it gets highlighted. If the program fails to find the entry then the text field's background becomes pink. A status bar below the text area displays a message whether text is found or not. The Escape key is used to start a new search or to finish the current one. Here is a picture of the
TextFieldDemoapplication.Click the Launch button ro run TextFieldDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index. To highlight text, this example uses a highlighter and a painter. The code below creates and sets up the highlighter and the painter for the text area. This code adds a document listener to the text field's document.final Highlighter hilit; final Highlighter.HighlightPainter painter; ... hilit = new DefaultHighlighter(); painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR); textArea.setHighlighter(hilit);Document listener'sentry.getDocument().addDocumentListener(this);insertUpdateandremoveUpdatemethods call thesearchmethod, which not only performs a search in the text area but also handles highlighting. The following code highlights the found text, sets the caret to the end of the found match, sets the default background for the text field, and displays a message in the status bar.The status bar is ahilit.addHighlight(index, end, painter); textArea.setCaretPosition(end); entry.setBackground(entryBg); message("'" + s + "' found. Press ESC to end search");JLabelobject. The code below shows how themessagemethod is implemented.If there is no match in the text area, the following code changes the text field's background to pink and displays a proper information message.private JLabel status; ... void message(String msg) { status.setText(msg); }Theentry.setBackground(ERROR_COLOR); message("'" + s + "' not found. Press ESC to start a new search");CancelActionclass is responsible for handling the Escape key as follows.class CancelAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { hilit.removeAllHighlights(); entry.setText(""); entry.setBackground(entryBg); } }
The following tables list the commonly usedJTextFieldconstructors and methods. Other methods you are likely to call are defined in theJTextComponentclass. Refer to The Text Component API.You might also invoke methods on a text field inherited from the text field's other ancestors, such as
setPreferredSize,setForeground,setBackground,setFont, and so on. See The JComponent Class for tables of commonly used inherited methods.The API for using text fields falls into these categories:
- Setting or Obtaining the Field's Contents
- Fine Tuning the Field's Appearance
- Implementing the Field's Functionality
Setting or Obtaining the Field's Contents Method or Constructor Purpose JTextField()
JTextField(String)
JTextField(String, int)
JTextField(int)Creates a text field. When present, the intargument specifies the desired width in columns. TheStringargument contains the field's initial text.void setText(String)
String getText()
(defined inJTextComponent)Sets or obtains the text displayed by the text field.
Fine Tuning the Field's Appearance Method Purpose void setEditable(boolean)
boolean isEditable()
(defined inJTextComponent)Sets or indicates whether the user can edit the text in the text field. void setColumns(int);
int getColumns()Sets or obtains the number of columns displayed by the text field. This is really just a hint for computing the field's preferred width. void setHorizontalAlignment(int);
int getHorizontalAlignment()Sets or obtains how the text is aligned horizontally within its area. You can use JTextField.LEADING,JTextField.CENTER, andJTextField.TRAILINGfor arguments.
Implementing the Field's Functionality Method Purpose void addActionListener(ActionListener)
void removeActionListener(ActionListener)Adds or removes an action listener. void selectAll()
(defined inJTextComponent)Selects all characters in the text field.
This table shows a few of the examples that use text fields and points to where those examples are described. For examples of code that are similar among all varieties of text fields such as dealing with layout, look at the example lists for related components such as formatted text fields and spinners.
Example Where Described Notes TextDemo This section An application that uses a basic text field with an action listener. TextFieldDemo This section An application that uses a text field and a text area. A search is made in the text area to find an entry from the text field. DialogDemo How to Make Dialogs CustomDialog.javaincludes a text field whose value is checked. You can bring up the dialog by clicking the More Dialogs tab, selecting the Input-validating dialog option, and then clicking the Show it! button.TextSamplerDemo Using Text Components Lays out label-text field pairs using a GridBagLayoutand a convenience method:addLabelTextRows(JLabel[] labels, JTextField[] textFields, GridBagLayout gridbag, Container container)TextInputDemo How to Use Formatted Text Fields Lays out label-text field pairs using a SpringLayoutand aSpringUtilitiesconvenience method:makeCompactGrid(Container parent, int rows, int cols, int initialX, int initialY, int xPad, int yPad)
Alden Hosting offers private JVM (Java Virtual Machine), Java Server Pages (JSP), Servlets, and Servlets Manager with our Web Hosting Plans WEB 4 PLAN and WEB 5 PLAN , WEB 6 PLAN .
At Alden Hosting we eat and breathe Java! We are the industry leader in providing affordable, quality and efficient Java web hosting in the shared hosting marketplace. All our sites run on our Java hosing platform configured for optimum performance using Java 1.6, Tomcat 6, MySQL 5, Apache 2.2 and web application frameworks such as Struts, Hibernate, Cocoon, Ant, etc.
We offer only one type of Java hosting - Private Tomcat. Hosting accounts on the Private Tomcat environment get their very own Tomcat server. You can start and re-start your entire Tomcat server yourself.
![]() |
|
http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP
Servlets
Tomcat
mysql
Java
JSP at JSP.aldenWEBhosting.com
Servlets at servlets.aldenWEBhosting.com
Tomcat at Tomcat.aldenWEBhosting.com
mysql at mysql.aldenWEBhosting.com
Java at Java.aldenWEBhosting.com
Web Hosts Portal
Web Links
Web Links
Web Hosting
JSP Solutions Web Links
JSP Solutions Web Hosting
Servlets Solutions Web Links
Servlets Solutions Web Hosting
Web Links
Web Links
.
.
.
.
.
.
.
.
.
.
jsp hosting
servlets hosting
web hosting
web sites designed
cheap web hosting
web site hosting
myspace web hosting