Java JTextField

Java JTextField

Java JTextField
JTextField is a swing component that allows users to edit a single line text.Note the difference with JTextArea allows multiple line text.

How to Declare JTextField class

[public class JTextField extends JTextComponent implements SwingConstants]

Some of the used JTextField constructors

  • JTextField()-creates a new JTextField.
  • JTextField(String s)-creates a JTexField with a string inside
  • JTextField(String s, int cols)-creates  JTextField with a text initialized with a specified number of columns.
  • JTextField(int cols)-creates a JTextField with specified number of columns.

JTextField commonly used methods

  • void addActionListener(ActionListener listener)-adds action listener to JTextField that makes it possible to receive events from the field.
  • Action getAction()-returns the currently action that is set.
  • void setFont(Font f)-sets JTextfield font.
  • void removeActionListener(ActionListener l)-used to remove action listener so that it does not receive more events.

JTextField example

[
import javax.swing.JFrame;
import javax.swing.JTextField;
public class JavaJTextField {

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



        JTextField field=new JTextField("Eric Loves programming in Java");
        field.setBounds(50,10, 200,30);
        frame.add(field);
        JTextField field2=new JTextField("This is my blog");
        field2.setBounds(50,150, 200,30);

        frame.add(field2);
        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);



    }

}
]

JTextField with actionListener.

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

public class JavaJTextField {
    public static JTextField field,field2,field3;
    public static JButton add,subtract;

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



        field=new JTextField();
        field.setBounds(50,50, 150,20);
        frame.add(field);
        field2=new JTextField();
        field2.setBounds(50,100, 150,20);
        frame.add(field2);
        field3=new JTextField();
        field3.setBounds(50,150, 150,20);
        field3.setEditable(false);
        frame.add(field3);

        add=new JButton("+");
        add.setBounds(50,200,50,50);
        frame.add(add);
        add.addActionListener(new ButtonListener());

        subtract=new JButton("-");
        subtract.setBounds(120,200,50,50);
        frame.add(subtract);
        subtract.addActionListener(new ButtonListener());

        frame.setSize(400,400);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);
        frame.setVisible(true);



    }

    public static class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String s1=field.getText();
            String s2=field2.getText();
            int a=Integer.parseInt(s1);
            int b=Integer.parseInt(s2);
            int c=0;
            if(event.getSource()==add){
                c=a+b;
            }else if(event.getSource()==subtract){
                c=a-b;
            }
            String result=String.valueOf(c);
            field3.setText(result);


        }

    }
}
]

Output:

JTextField with actionListener.


See also: Java JButton 

               JTextField getText

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
Java JTextField
Java JTextField
https://2.bp.blogspot.com/-2gR8OP3wHTU/WLqDkpvv2KI/AAAAAAAAHBE/woqpgTdnzDYniTytAGBRI7kD9RNxz041QCLcB/s200/java-jtextfield-example.PNG
https://2.bp.blogspot.com/-2gR8OP3wHTU/WLqDkpvv2KI/AAAAAAAAHBE/woqpgTdnzDYniTytAGBRI7kD9RNxz041QCLcB/s72-c/java-jtextfield-example.PNG
Java Codes
http://www.download-all.net/2017/03/java-jtextfield.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/03/java-jtextfield.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