Java JTextField setText

Java JTextField setText

JTextField Object has several methods.Some of these methods are JTextField getText, JTextField setToolTipText etc

 JTextField setText Example

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

public class JavaJTextField {
    public static JTextField field;
    public static JButton add;

    public static void main(String args[])
        JFrame frame=new JFrame("JTextField Example");

        field=new JTextField();

        field.setBounds(50,50, 150,20);

        add=new JButton("Save");

        add.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent event)
                String name=JOptionPane.showInputDialog(null, "Enter your name",




JTextField setText Code Explanation

When Save button is clicked,a JOptionPane.showInputDialog shows up requesting user to enter his/her name.
The name is saved as String name and using [field.setText(name);] we are able to set Text in the JTextField.

Other methods used in this program:

  • [field.setEnabled(false);]-Prevents user from directly entering text.
  • [add.setBounds(50,100,100,20);]-sets JButton position on JFrame.
  • [add.addActionListener(new ActionListener())]-Adds ActionListener to the JButton 



android android Apps android emulators antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment 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 node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
Java Codes: Java JTextField setText
Java JTextField setText
Java JTextField setText
Java Codes
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