Java JTextField getText
My previous post I shared about JButton swing component. Today lesson is all about JTextField class getText method.
JTextField getText
[
import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JTextField; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.LookAndFeel; import javax.swing.UIManager; import javax.swing.JOptionPane; public class JavaJTextField { public static void main(String args[]) { try { UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e) { } JFrame frame=new JFrame("JTextField Example"); JButton button=new JButton("Save"); JTextField field=new JTextField(); field.setBounds(50,50, 150,20); frame.add(field); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String text=field.getText(); if(text.isEmpty()) { JOptionPane.showMessageDialog(null, "Please enter your name",
"Input Message", JOptionPane.WARNING_MESSAGE); } else { JOptionPane.showMessageDialog(null, " The Text enter is "+text,
"Input Message", JOptionPane.INFORMATION_MESSAGE); } } }); button.setBounds(100,100,95,30); frame.add(button); frame.setSize(400,400); frame.setLocationRelativeTo(null); frame.setLayout(null); frame.setVisible(true); } }
]
JTextField getText Code Explanation
First, part of this program is the import section.I have imported all the swing components that are needed to run this program.To import all swing components [import javax.swing.*;] we use asterisk
Below code sets the look and feel of frame components to WindowsClassicLookAndFeel
[try
[ JButton button=new JButton("Save");]-this code creates a new JButton Object.
[ JTextField field=new JTextField(); field.setBounds(50,50, 150,20); frame.add(field);] this code creates JTextField object,sets the location with setBounds and adds the JTextField to the Frame using frame.add() method.
When the button is clicked,class JButton ActionListener is implemented.
Text entered in JTextField is retrieved [String text=field.getText();] and incase the field is empty ,JOptionPane showMessageDialog popups requesting the user to enter some text.
[if(text.isEmpty())
and if the field is not empty,the texted entered is displayed in another JOptionPane.
[else
The main lesson in this post is how to getText from a JTextField.
Below code sets the look and feel of frame components to WindowsClassicLookAndFeel
[try
{ UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"); } catch(Exception e) { e.printStackTrace();}]
[ JButton button=new JButton("Save");]-this code creates a new JButton Object.
[ JTextField field=new JTextField(); field.setBounds(50,50, 150,20); frame.add(field);] this code creates JTextField object,sets the location with setBounds and adds the JTextField to the Frame using frame.add() method.
When the button is clicked,class JButton ActionListener is implemented.
Text entered in JTextField is retrieved [String text=field.getText();] and incase the field is empty ,JOptionPane showMessageDialog popups requesting the user to enter some text.
[if(text.isEmpty())
{ JOptionPane.showMessageDialog(null, "Please enter your name","Input Message", JOptionPane.WARNING_MESSAGE);}]
and if the field is not empty,the texted entered is displayed in another JOptionPane.
[else
{ JOptionPane.showMessageDialog(null, " The Text enter is "+text,"Input Message", JOptionPane.INFORMATION_MESSAGE);}]
The main lesson in this post is how to getText from a JTextField.