Creating text boxes using JTextField in Java - ProgrammerTech
Language:

Search

Creating text boxes using JTextField in Java

  • Share this:
Creating text boxes using JTextField in Java

Create a JTextField text box

The JTextField class is used to create a text box using the swing library, i.e. a class that we create must be given an object, text boxes are almost devoid of an interface or form in the programming world of text boxes because they are a way to show data in front of the user. When we interact with a program and see data in front of us, this data will be shown to you by a text box. We will learn how to create a text box using the JTextField class, we will see this in the following example.

Example 1

package buttton;
import javax.swing.*;
public class Buttton {
    public static void main(String[] args) {
        JFrame F=new JFrame("welcome");
        JButton B=new JButton("click button");
        JTextField J=new JTextField();
        F.setSize(400, 400);
        B.setBounds(200, 100, 100, 50);
        J.setBounds(200, 50, 100, 25);
        F.add(B);
        F.add(J);
        F.setLayout(null);
        F.setVisible(true);
    }
}

At first, we called the entire swing library via the import command. And we entered the main program and included the JFrame interface class, and after that we included the JButton class, and then. We included the textbox class ie the input field via JTextField. After that we specified that the interface we want is a square with coordinates 400 * 400 and also specify the coordinates of the button "Length. Width" and specify the coordinates of the text field x, y, length and width. Then we add the button we created to the interface via F.add(B) and we also add the text field to the interface via F.add(J). Button sizes and points are set to null and the interface is shown via F.setVisible.

And when you run this program, it will appear as follows.

1-en-1.webp

 

Create an Event in Java

Create an event, in this section we will learn how to add an event and the event in programming means that when we click on. Something a certain process is performed and it is important when programming interfaces to be familiar with the events, they are very simple. We will explain a simple example based on the previous example by adding an event that is executed inside the text box when the button is pressed.

Example 2

package buttton;
import javax.swing.*;
import java.awt.event.*;
public class Buttton {
    public static void main(String[] args) {
        JFrame F=new JFrame("welcome");
        JButton B=new JButton("click button");
        JTextField J=new JTextField();
        F.setSize(400, 400);
        B.setBounds(200, 100, 100, 50);
        J.setBounds(200, 50, 100, 25);
        F.add(B);
        F.add(J);
        F.setLayout(null);
        F.setVisible(true);
        B.addActionListener((ActionEvent e) ->{
            J.setText("Muhammad");
        });
    }
}

At first, we called the entire swing library via the import command, and we called the library we use to handle events, which is awt.event. We entered the main program and included the JFrame interface class, and after that we included the JButton class and From. Then we included the textbox class ie the input field via JTextField. After that we specify that the interface we want is a square with coordinates 400*400 and we also specify coordinates. button "Length and Width" and we set the coordinates of the text field x, y, length and width. Then we add the button we created to the interface via F.add(B) and we also add the text field to the interface via F.add(J). Button sizes and points are null and the interface is shown via F.setVisible.

The event we want to add will be on the button in the interface. We included the button class and used the interface addActionListener, then opened one bracket. Then we include the event library which is ActionEvent and give it the object e which will check the event and then we put a minus sign and a less than -> sign. And in the body of this function that we created, we write the event we want, which is to show a sentence in the text box by selecting the object J. We used setText to mean add a sentence and the sentence was Muhammad. And when you run this program, it will appear as follows. When you press the button, the sentence “Muhammad” will appear in the text field.

2-en-2.webp

 

Arithmetic operations in interfaces in Java

Addition within an interface in the swing library, Addition in text fields in Java, we will talk about how to add two numbers within a GUI interface with three text boxes. We talked about it in the previous lesson and the JButton button, and in the first box there is a number and the second box is a number, and when the button is pressed, they are combined in the third text box.

Example 3

package buttton;
import java.awt.event.*;
import javax.swing.*;
public class Buttton{

     public static void main(String []args){
        JFrame F=new JFrame("two digit addition program");
        JButton B=new JButton("click");
        JTextField J1=new JTextField();
        JTextField J2=new JTextField();
        JTextField J3=new JTextField();
        F.setSize(500, 500);
        B.setBounds(150, 120, 60, 50);
        J1.setBounds(100, 50, 100, 25);
        J2.setBounds(200, 50, 100, 25);
        J3.setBounds(150, 80, 100, 25);
        F.add(B);
        F.add(J1);
        F.add(J2);
        F.add(J3);
        F.setLayout(null);
        F.setVisible(true);
        B.addActionListener((ActionEvent e)->{
            int x1= Integer.parseInt(J1.getText());
            int x2= Integer.parseInt(J2.getText());
            int x3=x1+x2;
            J3.setText(Integer.toString(x3));
     });
     }
}

We called the entire swing library via the import command and called the library we use to handle events, which is awt.event. And we entered the main program and included the JFrame interface class, and after that we included the JButton class, and then. We included three classes of text box ie input field via JTextField which are J1,J2,J3. After that we specified that the interface we want is square with coordinates 500 * 500 and we also specified the coordinates of the "Length and Width" button. And we specified the coordinates of the text fields 'x', the 's' and 'y', the length and width. Then we add all the elements we created to the interface and align the interface with the elements. Added via setLayout we set it to null and shown the interface via F.setVisible.

And the event we want to add will be on the button in the interface and we have included the button class. That is, when you press it, the first number is added to the second number. We will start with the object of the button, which is B, this is the class of the event, which belongs to the .event package, and then . We open a parenthesis and write ActionEvent and then the letter e that will inspect the event. And inside this function, we wrote the code we want, which is to get the number from the first text box and the second text box and put them in the third text box. We created a variable of type int because it is an all process and we used the getText function to get the value from the first text box and the same thing for the second text box. Then we put the command to get the two values ​​to be added to the third square x3.

When you run the code, it will appear as follows.

3-en.webp

 

And when you type numbers inside the first box and the second box and press the button, the two values ​​will be summed. directly and puts them in the third box as in the following picture.

4-en-1.webp

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com