How to create a java swing jtextfield with detail examples

Java swing JTextField is an object that allows the user to enter a line of text. Usually used to input data with short information.

Example 1: Creating a simple java swing jtextfield

java-swing-jtextfield
The output:
java-swing-jtextfield

Java swing JTextField initialization functions

- JTextField (): Create a new java swing JTextField
JTextField (Document doc, String text, int columns): Create a java swing JTextField that uses a text-based storage model with text and column numbers (coloumns).
JTextField (int columns): Create an empty java swing JTextField with the width is columns.
JTextField (String text): Create java swing JTextField with given text.
JTextField (String text, int columns): Create java swing JTextField with given text and given width.

Example 2: Get, set and process data from java swing JTextField

In this example we are going to make a small demo like the opening image of the post, which adds 1 JButton to re-enter the data. - A demo of profit-taking (Do not notice if the data is wrong but will automatically jump to JTextField if it has no data).

File name: Profit.java
java-swing-jtextfield

Continue Profit.java
java-swing-jtextfield
Continue Profit.java
java-swing-jtextfield
End of Profit.java
java-swing-jtextfield













the output:
java-swing-jtextfield

Things to notice about get and set process from java swing jtextfield

To get data (which is essentially a string) from java swing JTextField, we use the getText () method.
- To set data for java swing JTextField we use the setText (String text) method.
- The requestFocus () method makes the cursor jump (focus) to the java swing JTextField.
- SetEditable (boolean edit) method to set whether data is entered for java swing JTextField.

Example 3: Aligning and creating events for java swing JTextField

In this example we will create a java swing JTextField that when entered, our text is aligned right (default is the left margin), when pressed enter will display the data entered into a JLabel.

File name: AlignAndAction.java
java-swing-jtextfield
Continue AlignAndAction.java
java-swing-jtextfield
The output:
java-swing-jtextfield
Just like JButton, java swing JTextField captures the events of the mouse and even of the keys on the keyboard. To capture the events of the keys we use the addKeyListener () method and do the same as the JButton event handler but with KeyListener, not with ActionListener.
Use the setHorizontalAlignment method to align the java swing JTextField.

Some other methods of java swing JTextField

  • getSelectedText (): retrieves selected text in java swing JTextField
  • setSelectionStart (int selectionStart) and setSelectionEnd (int selectionEnd) set start and end positions to select text. Without the setSelectionStart method, the text is selected from the cursor's current position (smaller than selectionEnd) to selectionEnd. Without the setSelectionEnd method, the text is selected from the selectionStart position to the current position of the cursor (greater than selectionStart).
  • getCaretPosition () and setCaretPosition (position) get and set the current position of the pointer.

Comments