Java JTextfield Class Example

JTEXTFIELD

Java Swing Tutorial Explaining the JTextField Component. JTextField allows editing/displaying of a single line of text. New features include the ability to justify the text left, right, or center, and to set the text’s font. When the user types data into them and presses the Enter key, an action event occurs. If the program registers an event listener, the listener processes the event and can use the data in the text field at the time of the event in the program. JTextField is an input area where the user can type in characters. If you want to let the user enter multiple lines of text, you cannot use Jtextfield’s unless you create several of them. The solution is to use JTextArea, which enables the user to enter multiple lines of text.

JTEXTFIELD SOURCE CODE

// A program to demonstrate the use of JTextFields's
//Import Statements
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JTextFieldDemo extends JFrame {

 //Class Declarations
 JTextField jtfText1, jtfUneditableText;
 String disp = "";
 TextHandler handler = null;
 //Constructor
 public JTextFieldDemo() {
  super("TextField Test Demo");
  Container container = getContentPane();
  container.setLayout(new FlowLayout());
  jtfText1 = new JTextField(10);
  jtfUneditableText = new JTextField("Uneditable text field", 20);
  jtfUneditableText.setEditable(false);
  container.add(jtfText1);
  container.add(jtfUneditableText);
  handler = new TextHandler();
  jtfText1.addActionListener(handler);
  jtfUneditableText.addActionListener(handler);
  setSize(325, 100);
  setVisible(true);
 }
 //Inner Class TextHandler
 private class TextHandler implements ActionListener {

  public void actionPerformed(ActionEvent e) {
   if (e.getSource() == jtfText1) {
    disp = "text1 : " + e.getActionCommand();
   } else if (e.getSource() == jtfUneditableText) {
    disp = "text3 : " + e.getActionCommand();
   }
   JOptionPane.showMessageDialog(null, disp);
  }
 }
 //Main Program that starts Execution
 public static void main(String args[]) {
  JTextFieldDemo test = new JTextFieldDemo();
  test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
}// End of class TextFieldTest
Output
Download jTextField Source Code

ANOTHER EXAMPLE: JTEXTFIELD SOURCE CODE

public class JTextFieldDemo2 extends JFrame implements ActionListener {

 JTextField jtfInput;
 JTextArea jtAreaOutput;
 String newline = "\n";
 public JTextFieldDemo2() {
  createGui();
 }
 public void createGui() {
  jtfInput = new JTextField(20);
  jtfInput.addActionListener(this);
  jtAreaOutput = new JTextArea(5, 20);
  jtAreaOutput.setEditable(false);
  JScrollPane scrollPane = new JScrollPane(jtAreaOutput,
    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
    JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
  GridBagLayout gridBag = new GridBagLayout();
  Container contentPane = getContentPane();
  contentPane.setLayout(gridBag);
  GridBagConstraints gridCons1 = new GridBagConstraints();
  gridCons1.gridwidth = GridBagConstraints.REMAINDER;
  gridCons1.fill = GridBagConstraints.HORIZONTAL;
  contentPane.add(jtfInput, gridCons1);
  GridBagConstraints gridCons2 = new GridBagConstraints();
  gridCons2.weightx = 1.0;
  gridCons2.weighty = 1.0;
  contentPane.add(scrollPane, gridCons2);
 }
 public void actionPerformed(ActionEvent evt) {
  String text = jtfInput.getText();
  jtAreaOutput.append(text + newline);
  jtfInput.selectAll();
 }
 public static void main(String[] args) {
  JTextFieldDemo2 jtfTfDemo = new JTextFieldDemo2();
  jtfTfDemo.pack();
  jtfTfDemo.addWindowListener(new WindowAdapter() {

   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  jtfTfDemo.setVisible(true);
 }
}
Output
Download jTextField Source Code

JAVA JTEXTFIELD HIERARCHY

javax.swing
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
All Implemented Interfaces: 
Accessible, ImageObserver, MenuContainer, Scrollable, Serializable, SwingConstants
Direct Known Subclasses: 
DefaultTreeCellEditor.DefaultTextField, JFormattedTextField, JPasswordField

JTEXTFIELD CONSTRUCTOR

JTextField()
Constructs a new TextField.
JTextField(Document doc, String text, int columns)
Constructs a new JTextField that uses the given text storage model and the given number of columns.
JTextField(int columns)
Constructs a new empty TextField with the specified n
umber of columns.
JTextField(String text)
Constructs a new TextField initialized with the specified text.
JTextField(String text, int columns)
Constructs a new TextField initialized with the specified text and columns.

2 comments:

  1. I’ve also been meditating on the identical idea personally lately. Happy to see somebody on the same wavelength! Nice article. Money6x

    ReplyDelete
  2. The when I just read a blog, I’m hoping which it doesnt disappoint me as much as brussels. After all, I know it was my method to read, but When i thought youd have something intriguing to state. All I hear is actually a handful of whining about something you could fix in case you werent too busy in search of attention. ASG 55

    ReplyDelete