Swing JTextField Class

Advertisements


Introduction

The class JTextField is a component which allows the editing of a single line of text.

Class declaration

Following is the declaration for javax.swing.JTextField class:

public class JTextField
   extends JTextComponent
      implements SwingConstants

Field

Following are the fields for javax.swing.JList class:

  • static String notifyAction -- Name of the action to send notification that the contents of the field have been accepted.

Class constructors

S.N.Constructor & Description
1JTextField()
Constructs a new TextField.
2JTextField(Document doc, String text, int columns)
Constructs a new JTextField that uses the given text storage model and the given number of columns.
3JTextField(int columns)
Constructs a new empty TextField with the specified number of columns.
4JTextField(String text)
Constructs a new TextField initialized with the specified text.
5JTextField(String text, int columns)
Constructs a new TextField initialized with the specified text and columns.

Class methods

S.N.Method & Description
1protected void actionPropertyChanged(Action action, String propertyName)
Updates the textfield's state in response to property changes in associated action.
2void addActionListener(ActionListener l)
Adds the specified action listener to receive action events from this textfield.
3protected void configurePropertiesFromAction(Action a)
Sets the properties on this textfield to match those in the specified Action.
4protected PropertyChangeListener createActionPropertyChangeListener(Action a)
Creates and returns a PropertyChangeListener that is responsible for listening for changes from the specified Action and updating the appropriate properties.
5protected Document createDefaultModel()
Creates the default implementation of the model to be used at construction if one isn't explicitly given.
6protected void fireActionPerformed()
Notifies all listeners that have registered interest for notification on this event type.
7AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JTextField.
8Action getAction()
Returns the currently set Action for this ActionEvent source, or null if no Action is set.
9ActionListener[] getActionListeners()
Returns an array of all the ActionListeners added to this JTextField with addActionListener().
10Action[] getActions()
Fetches the command list for the editor.
11int getColumns()
Returns the number of columns in this TextField.
12protected int getColumnWidth()
Returns the column width.
13int getHorizontalAlignment()
Returns the horizontal alignment of the text.
14BoundedRangeModel getHorizontalVisibility()
Gets the visibility of the text field.
15Dimension getPreferredSize()
Returns the preferred size Dimensions needed for this TextField.
16int getScrollOffset()
Gets the scroll offset, in pixels.
17String getUIClassID()
Gets the class ID for a UI.
18boolean isValidateRoot()
Calls to revalidate that come from within the textfield itself will be handled by validating the textfield, unless the textfield is contained within a JViewport, in which case this returns false.
19protected String paramString()
Returns a string representation of this JTextField.
20void postActionEvent()
Processes action events occurring on this textfield by dispatching them to any registered ActionListener objects.
21void removeActionListener(ActionListener l)
Removes the specified action listener so that it no longer receives action events from this textfield.
22void scrollRectToVisible(Rectangle r)
Scrolls the field left or right.
23void setAction(Action a)
Sets the Action for the ActionEvent source.
24void setActionCommand(String command)
Sets the command string used for action events.
25void setColumns(int columns)
Sets the number of columns in this TextField, and then invalidate the layout.
26void setDocument(Document doc)
Associates the editor with a text document.
27void setFont(Font f)
Sets the current font.
28void setHorizontalAlignment(int alignment)
Sets the horizontal alignment of the text.
29void setScrollOffset(int scrollOffset)
Sets the scroll offset, in pixels.

Methods inherited

This class inherits methods from the following classes:

  • javax.swing.text.JTextComponent

  • javax.swing.JComponent

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object

JTextField Example

Create the following java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >

SwingControlDemo.java
package com.tutorialspoint.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class SwingControlDemo {
    
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public SwingControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingControlDemo  swingControlDemo = new SwingControlDemo();      
      swingControlDemo.showTextFieldDemo();
   }

   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    

      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextFieldDemo(){
      headerLabel.setText("Control in action: JTextField"); 

      JLabel  namelabel= new JLabel("User ID: ", JLabel.RIGHT);
      JLabel  passwordLabel = new JLabel("Password: ", JLabel.CENTER);
      final JTextField userText = new JTextField(6);
      final JPasswordField passwordText = new JPasswordField(6);      

      JButton loginButton = new JButton("Login");
      loginButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            String data = "Username " + userText.getText();
            data += ", Password: " 
            + new String(passwordText.getPassword()); 
            statusLabel.setText(data);        
         }
      }); 

      controlPanel.add(namelabel);
      controlPanel.add(userText);
      controlPanel.add(passwordLabel);       
      controlPanel.add(passwordText);
      controlPanel.add(loginButton);
      mainFrame.setVisible(true);  
   }
}

Compile the program using command prompt. Go to D:/ > SWING and type the following command.

D:\SWING>javac com\tutorialspoint\gui\SwingControlDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\SWING>java com.tutorialspoint.gui.SwingControlDemo

Verify the following output

Swing JTextField

Advertisements
Advertisements