Beginners Tutorial for Javax Swing JTextField-Getting Started

Hello Friends , welcome to the tutorial of an another swing component Javax Swing JTextField . Since we have already learnt about JLabel and JFrame classes of java swing therefore this tutorial is going to be lot more easier than the previous ones because we will be dealing with some methods and steps which we have already discussed in the previous tutorials. you can go to my previous tutorials by clicking the links given below :

JFrame Tutorial

JLabel Tutorial

Now ,Let’s begin our Tutorial Javax Swing JTextField.

Javax Swing JTextField

What is JTextField ??

  • It is the simplest swing  text component.
  • It is  probably the most widely used text component.
  • It allows us to enter  a single line of text .

So this was a brief description of JTextField class .Now we will learn how to create JTextField.

Creating JTextField

Steps

  • For creating JTextField we need to create the object of the JTextField class. Example- JTextField text=new JTextField();
  • Next we will set the Location and size of JTextField using setBounds() method.
  • Now we will add TextField  to the frame’s content pane by using the add() method and will be passing the object of JTextField class as an argument in the add() method.
  • Programming code is given below :
  • Output  :
Javax Swing JTextField
Javax Swing JTextField-Fig-1
  • As you can see in the above image we have created the JTextField in which we can write any single line of text .

Changing Font Style and Font Size of JTextField

  • For changing Font Style and Font Size of JTextField we will use the exact same steps that we have used in JLabel while we were changing the Font Style and Font Size of JLabel.
  • Progrmming code is given below :
  • Output  :
changing font style and font size example
Javax Swing JTextField-Fig-2

Setting The Font Color of  JTextField

Steps

  • For setting the Font Color of JTextField we will use a method setForeground() which is already defined in the JTextField class.
  • The argument of the setForeground() method is a  Color constant  which is defined in the Color class.(Example-Color.RED).
  • We will call the setForeground() method using the object of the JTextField class .
  • Programming code is given below :
  • Output  :
changing Font color of the JTextField
Javax Swing JTextField-Fig-3
  • As we can  see in above image we have changed the Font color of the JTextField.

Changing The Background Color of JTextField

Steps

  • For Changing the background color of JTextField we will use a method setBackground() method and will be following the exact same steps when we were changing the foreground color of JTextField.
  • Programming code is given below :
  • Output  :
Changing color of the background in JTextField
Javax Swing JTextField-Fig-4
  • As we can see in the above image we have changed the Background color of the JTextField.

Creating Our own Color in Font and Background Of JTextField

We learnt how to change the Font color and Background color of the JTextField using the setForeground() and SetBackground() methods. But we can only use predefined colors .Now we will learn how to create our own color using the RGB (Red,Green,Blue)  values  and will apply it to the JTextField.

Steps

  • First of all we need to create an object of the Color class and in it’s constructor we will pass three values .The three values are the  decimal code of the RGB values. You can find the  RGB decimal codes through this link.
  • Now if we want to change the Background color of the JTextField then we will pass the object of Color class as an argument in the setBackground() method or if we want to change the Font color of the JTextField then we will pass the object of the Color class as an argument in the setForeground() method.
  • Programming code is given below :
  • Output  :
Creating Our own Color in Font and Background Of JTextField
Javax Swing JTextField-Fig-5
  • Now as we can see in the above image we have changed the Background color using the RGB values . we can use it for changing the Font color also as mentioned in the above.

So friend here an another tutorial of  swing component is completed.Let me know and  Do comments if you are having any doubts regarding this Javax Swing JTextField tutorial.Thank You.

Leave a Comment