INTRODUCTION OF JTEXTFIELD

Introduction of JTextField


JTextField Constructor

    JTextField()

    Constructs a new TextField.

    JTextField(int columns)

    Constructs a new empty TextField with the specified number of columns.

    JTextField(String text)

    Constructs a new TextField initialized with the specified text.

    JTextField(String text, int columns)

    Constructs a new TextField initialized with the specified text and columns.

Example:JTextField Constructor

 
            
                package com.swing.sdj;
                import java.awt.Container;
                import java.awt.FlowLayout;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import javax.swing.JFrame;
                import javax.swing.JOptionPane;
                import javax.swing.JTextField;
                public class SdjTextDemo extends JFrame
                {
                    public static void main(String[] args)
                    {
                        SdjTextDemo jframe=new SdjTextDemo();
                        jframe.setDefaultCloseOperation(SdjTextDemo.EXIT_ON_CLOSE);
                    }
                    JTextField jtfText1, jtfUneditableText;
                    String disp = "";
                    TextHandler handler = null;
                    public SdjTextDemo()
                    {
                        super("SDJ Text Field Test Demo");
                        Container container = getContentPane();
                        container.setLayout(new FlowLayout());
                        jtfText1 = new JTextField(10);
                        jtfUneditableText = new JTextField("Uneditable text field", 20);
                        jtfUneditableText.setEditable(false);
                        container.add(jtfText1);
                        container.add(jtfUneditableText);
                        handler = new TextHandler();
                        jtfText1.addActionListener(handler);
                        jtfUneditableText.addActionListener(handler);
                        setSize(325, 100);
                        setVisible(true);
                    }
                    private class TextHandler implements ActionListener
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            if (e.getSource() == jtfText1)
                            {
                                disp = "text1 : " + e.getActionCommand();
                            }
                            else if (e.getSource() == jtfUneditableText)
                            {
                                disp = "text3 : " + e.getActionCommand();
                            }
                            JOptionPane.showMessageDialog(null, disp);
                        }
                    }
                } 
                

Output:

Naming Convention
SDJ TextField Demo

    Enter text or anything inside text-input and hit the enter

Naming Convention
SDJ TextField Demo
Naming Convention
SDJ TextField Demo