Java JTextField getText

Java JTextField getText

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
{
    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.





COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JTextField getText
Java JTextField getText
Java JTextField getText
https://2.bp.blogspot.com/-GB3mKzn2iy4/WLhrd1TrH8I/AAAAAAAAG-g/Pzemueb_qkcQCHUtaRBC2yt6apbkEipgACLcB/s200/Java-JTextField-getText-method-min.PNG
https://2.bp.blogspot.com/-GB3mKzn2iy4/WLhrd1TrH8I/AAAAAAAAG-g/Pzemueb_qkcQCHUtaRBC2yt6apbkEipgACLcB/s72-c/Java-JTextField-getText-method-min.PNG
Java Codes
http://www.download-all.net/2017/03/java-jtextfield-gettext.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jtextfield-gettext.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy