• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

JTextField Component in Java

Tekko1337

Pipeline Engineer
Divine
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
67
Likes
139
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
This is a tutorial in which we will going to create a program that has the JTextField Component using Java. The JTextField is a basic text control component that enables the user to type a small amount of text.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of jTextFieldComponent.java.

2. Import the following packages:
  1. JFrame

    frame =

    new

    JFrame

    (

    "JTextField Component"

    )

    ;

  2. JLabel

    name =

    new

    JLabel

    (

    "Enter name: "

    )

    ;
  3. JTextField

    txtName =

    new

    JTextField

    (

    20

    )

    ;

We have created a JTextField component with variable named txtName. The 20 inside the JTextField components indicate the number of characters allowed on the text field.

To create the size of the textfield, we will use the setPreferredSize method and will access the Dimension class.

  1. txtName.setPreferredSize

    (

    new

    Dimension

    (

    100

    , 30

    )

    )

    ;

The 100 is the width of the textfield dimension and 30 is the height.

3. Now, we will add a KeyEvent and KeyListener to the textfield. We will have a text field that if we input text in there, it will automatically converted into uppercase letters. Have this code below:

  1. txtName.addKeyListener

    (

    new

    KeyAdapter

    (

    )

    {
  2. public

    void

    keyReleased(

    KeyEvent

    e)

    {
  3. JTextField

    textField =

    (

    JTextField

    )

    e.getSource

    (

    )

    ;
  4. String

    text =

    textField.getText

    (

    )

    ;
  5. textField.setText

    (

    text.toUpperCase

    (

    )

    )

    ;
  6. }

We have created a text variable for our TextField. The getText method here gets the string input of the user. The setText method will set the text to the textfield and it will be converted into uppercase letters using toUpperCase method.

4. Now, add the JLabel variable to the frame using the default BorderLayout of West position and the JTextField variable using the default BorderLayout of South position of the getContentPane method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:

  1. frame.getContentPane

    (

    )

    .add

    (

    name,"West"

    )

    ;
  2. frame.getContentPane

    (

    )

    .add

    (

    txtName,"South"

    )

    ;
  3. frame.setSize

    (

    400

    , 200

    )

    ;
  4. frame.setVisible

    (

    true

    )

    ;
  5. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;

Output:

jtextfield.png


Here's the full code of this tutorial:

  1. import

    java.awt.*

    ;

    //used to access the Dimension class
  2. import

    java.awt.event.*

    ;

    //used to access the KeyAdapter and KeyEvent class
  3. import

    javax.swing.*

    ;

    //used to access the JFrame, JLabel, and JTextField class


  4. public

    class

    jTextFieldComponent {

  5. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  6. JFrame

    frame =

    new

    JFrame

    (

    "JTextField Component"

    )

    ;


  7. JLabel

    name =

    new

    JLabel

    (

    "Enter name: "

    )

    ;
  8. JTextField

    txtName =

    new

    JTextField

    (

    20

    )

    ;

  9. txtName.setPreferredSize

    (

    new

    Dimension

    (

    100

    , 30

    )

    )

    ;


  10. txtName.addKeyListener

    (

    new

    KeyAdapter

    (

    )

    {
  11. public

    void

    keyReleased(

    KeyEvent

    e)

    {
  12. JTextField

    textField =

    (

    JTextField

    )

    e.getSource

    (

    )

    ;
  13. String

    text =

    textField.getText

    (

    )

    ;
  14. textField.setText

    (

    text.toUpperCase

    (

    )

    )

    ;
  15. }


  16. }

    )

    ;
  17. frame.getContentPane

    (

    )

    .add

    (

    name,"West"

    )

    ;
  18. frame.getContentPane

    (

    )

    .add

    (

    txtName,"South"

    )

    ;
  19. frame.setSize

    (

    400

    , 200

    )

    ;
  20. frame.setVisible

    (

    true

    )

    ;
  21. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;

  22. }
  23. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

343,183

343,191

Top