Pages

Get JTextField text

Source code below will show you how to get text from JTextField.

**********************************************************************
COMPLETE SOURCE CODE FOR : GetJTextFieldText.java
**********************************************************************


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.GridLayout;

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

public class GetJTextFieldText implements ActionListener
{
//Create button with text ( Click me to get JTextField contents )
JButton button=new JButton("Click me to get JTextField contents");

//Create JTextField
JTextField textField=new JTextField(10);

//Create a window with title ( Get JTextField text )
JFrame frame=new JFrame("Get JTextField text");

public GetJTextFieldText()
{
//Add action listener to button
button.addActionListener(this);

//Set JFrame layout to GridLayout
frame.setLayout(new GridLayout(2,1));

//Add created text field into JFrame
frame.add(textField);

//Add created button into JFrame
frame.add(button);

//Set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Set JFrame size
frame.setSize(400,100);

//Make JFrame visible
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
//Method getText will get text from text field
JOptionPane.showMessageDialog(frame,"JTextField text is : "+textField.getText());
}
}

public static void main(String[]args)
{
GetJTextFieldText gjbt=new GetJTextFieldText();
}
}


**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************
Related Posts Plugin for WordPress, Blogger...