A Swing Component: JTextField

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.
go to previous page   go to home page   go to next page

Answer:

You may have forgotten to register a listener with the button. Without a listener, the events are ignored.

A Swing Component: JTextField

A JTextField is a box that contains a line of text. The user can type text into the box and the program can get it and then use it as data. The program can write the results of a calculation to a JTextField. Here is a label and a text field (both inside of an applet):

Embedded Java applet removed: modern browsers do not support Java applets. The surrounding source code is retained for historical study.

To enter text into the field you must first click the mouse pointer inside of it. This gives the field the keyboard focus (usually just called focus). When a component has focus, characters from the keyboard are directed to that component. A component gets focus by various means. The user might click it, or the program might give it focus.

Enter some text. Edit it using back space and delete. Copy some text from the web page and paste it into the field. All this works. But the field is not connected to any application code so nothing is done with the text.

QUESTION 2:

(Review: ) Where is the class definition for JTextField?