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 :
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import javax.swing.*; import java.awt.*; public class myTextField { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myTextField"); f.setVisible(true); f.setSize(500,500); Container c= f.getContentPane(); c.setLayout(null); //creating object of JTextField class JTextField text=new JTextField(); //Setting size and Location of JTextField using setBounds() method text.setBounds(10,100,500,50); // adding TextField to the frame's content pane f.add(text); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import javax.swing.*; import java.awt.*; public class myTextField { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myTextField"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JTextField text=new JTextField(); text.setBounds(10,100,500,50); //creating object of Font class and passing values in constructor Font fo=new Font("ARIAL",Font.ITALIC,28); //applying it into the JTextField using setFont() method text.setFont(fo); f.add(text); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import javax.swing.*; import java.awt.*; public class myTextField { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myTextField"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JTextField text=new JTextField(); text.setBounds(10,100,500,50); Font fo=new Font("ARIAL",Font.ITALIC,28); text.setFont(fo); //changing the color of the font using setForeground method text.setForeground(Color.RED); f.add(text); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import javax.swing.*; import java.awt.*; public class myTextField { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myTextField"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JTextField text=new JTextField(); text.setBounds(10,100,500,50); Font fo=new Font("ARIAL",Font.ITALIC,28); text.setFont(fo); text.setForeground(Color.RED); //changing Background color using the setBackground() method text.setBackground(Color.YELLOW); f.add(text); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import javax.swing.*; import java.awt.*; public class myTextField { public static void main(String args[] ) { JFrame f= new JFrame(); f.setTitle("myTextField"); f.setVisible(true); f.setSize(1000,500); Container c= f.getContentPane(); c.setLayout(null); JTextField text=new JTextField(); text.setBounds(10,100,500,50); Font fo=new Font("ARIAL",Font.ITALIC,28); text.setFont(fo); //creating object of the color class and passing RGB values in its constructor Color clr=new Color(102,205,170); text.setForeground(Color.YELLOW); //passing object of the Color class as an argument in setBackground() method text.setBackground(clr); f.add(text); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } |
- Output :
- 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.