Creating a JTextField Component - Java Swing

Java examples for Swing:JTextField

Description

Creating a JTextField Component

Demo Code


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

public class Main {
  public static void main(String[] args) throws Exception {
    JTextField textfield = new JTextField("Initial Text");

    int cols = 30;
    textfield = new JTextField("Initial Text", cols);

    textfield.addActionListener(new MyActionListener());
  }//from  w  w w  .j a va2 s . co m
}

class MyActionListener implements ActionListener {
  public void actionPerformed(ActionEvent evt) {
    JTextField textfield = (JTextField) evt.getSource();
    System.out.println(textfield.getText());
  }
}

Related Tutorials