How to put two input in two seperate textfield with using a button in Java?

add textfield on button click in java
how to get data from textfield in java
how to add two text fields in java
how to auto calculate input numeric values of text field in java
java swing text field with label
how to get value from textfield in java
how to add text field in java netbeans
how to make a text box in java

Im building a basic calculator using jswing and I have two textfield, one for input #1 and for input #2. The question is, how can I continue putting my input so that when I finished entering my first input it should go to the next textfield for my second input? And also theres seems to be an error when operating the arithmetic formulation, the output displays the wrong answer or the total of two inputs.

import java.awt.event.*;


class Kalkuleytor implements ActionListener
{
    JFrame f;
    JTextField t1, t2, t3;
    JLabel lbl1, lbl2, lbl3, lbl4;
    JButton one,two,three,four,five,six,seven,eight,nine,zero,add,mltply,sbtrct,dvd,equal;

    static double a=0,b=0,c=0,result=0;
    static int operator=0;

    Kalkuleytor()
    {
        f=new JFrame("Calculator");
        t1=new JTextField();
        t2=new JTextField();
        t3=new JTextField();
        lbl1=new JLabel("Number 1");
        lbl2=new JLabel("Number 2");
        lbl3=new JLabel("");
        lbl4=new JLabel("Total");
        one=new JButton("1");
        two=new JButton("2");
        three=new JButton("3");
        four=new JButton("4");
        five=new JButton("5");
        six=new JButton("6");
        seven=new JButton("7");
        eight=new JButton("8");
        nine=new JButton("9");
        zero=new JButton("0");
        dvd=new JButton("/");
        mltply=new JButton("*");
        sbtrct=new JButton("-");
        add=new JButton("+");
        equal=new JButton("=");

        //setBounds and add of buttons

        f.setLayout(null);
        f.setVisible(true);
        f.setSize(517,502);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);

        one.addActionListener(this);
    //ActionListener for all button
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==one)
            t1.setText("1");

        if(e.getSource()==two)
            t1.setText("2");

        if(e.getSource()==three)
            t1.setText("3");

        if(e.getSource()==four)
            t1.setText("4");

        if(e.getSource()==five)
            t1.setText("5");

        if(e.getSource()==six)
            t1.setText("6");

        if(e.getSource()==seven)
            t1.setText("7");

        if(e.getSource()==eight)
            t1.setText("8");

        if(e.getSource()==nine)
            t1.setText("9");

        if(e.getSource()==zero)
            t1.setText("0");

        if(e.getSource()==add)
        {
            a=Double.parseDouble(t1.getText());
            operator=1;
            lbl3.setText("+");
        } 

        if(e.getSource()==sbtrct)
        {
            a=Double.parseDouble(t1.getText());
            operator=2;
            lbl3.setText("-");
        }

        if(e.getSource()==mltply)
        {
            a=Double.parseDouble(t1.getText());
            operator=3;
            lbl3.setText("*");
        }

        if(e.getSource()==dvd)
        {
            a=Double.parseDouble(t1.getText());
            operator=4;
            lbl3.setText("/");
        }

        if(e.getSource()==equal)
        {
            c=Double.parseDouble(t1.getText());

            switch(operator)
            {
                case 1: result=a+b;
                    break;

                case 2: result=a-b;
                    break;

                case 3: result=a*b;
                    break;

                case 4: result=a/b;
                    break;

                default: result=0;
            }

            t3.setText(""+result);
        }       
    }

    public static void main(String[]args)
    {
        new Kalkuleytor();
    }
} 

enter image description here


You need to use requestFocusInWindow() after you click on any operator (+,-,*,/,etc).

How do I lay out Input panel with multiple textfields and OK , (I did a JFrame for testing, but you can change it to a JPanel and add that Just be sure to have only 1 JFrame, see The use of multiple JFrames, import java.​awt.event. is that the focus will be on a button, not the first name text field. If you separate the two buttons into their own panel and treat that  The second is just using the fact that you can attach a button to an input, only it will skew the width of the button the larger the form gets, but both are acceptable ways to accomplish what you need. Good luck!


Interact with JFrame – buttons, listeners and text fields, Following program shows example of adding text fields to JFrame. new JTextField("Text field 2",10);; textfield3 = new JTextField("Text field 3",10);; f. You can customize by using different mehods provided by the JTextfield. Text fields (and, in fact, all input fields) have a value property. You can read this value as an ordinary string variable. You can also write to this property, and the text field will be updated on the fly. This code handles the data input and output: Create a variable for the name. This is an ordinary string variable.


How to Use Text Fields (The Java™ Tutorials > Creating a GUI With , If you need to obtain more than one line of input from the user, use a text area. that allows you to specify the legal set of characters that the user can enter. Click the Launch button to run TextDemo using Java™ Web Start (download JDK 7  The text field is editable. The text area is not editable. When the user presses Enter in the text field, the program copies the text field's contents to the text area, and then selects all the text in the text field. Click the Launch button to run TextDemo using Java™ Web Start (download JDK 7 or later).


Using JavaFX UI Controls: Text Field, This chapter discusses the capabilities of the text field control. The TextField class implements a UI control that accepts and displays text input. 8-2 creates the three text fields and two buttons, and adds them to the application's scene by using the GridPane container. Example 8-2 Adding Text Fields to the Application Javax.swing.JButton calss provides us a way to add buttons and events happens after button click. Similarly with the help of javax.swing.JTextfield allow us to add text fields to JFrame. Adding text fields . You can create a textfield using JTextfield() method. This class has many constructors like, JTextField(): It constructs a new text field


GUI Programming Part 2 - Java Programming Tutorial, Swing's JLabel and buttons ( AbstractButton subclasses): support both text and icon and set its appearances JTextField textField = new JTextField("Text Field", 15); textField. You could use different icons for different button states: defaultIcon Prompt for user input public static String showInputDialog(Object message,  I want the program to store whatever user types in each textfields , in three separate strings , WHEN the user clicks the button. Can anyone please give teh code for just the handler class or the actionPerformed() method. or if you don't wanna type in the code, please explain the syntax that would allow me to do it.


13.6 Text Input and Output, Both generate event objects from the class ActionEvent — a JButton object We can initialize the text in a JTextField by using a slightly different constructor. is not a string of the appropriate form, then Java throws a NumberFormatException at setEditable(false); // Add labelled input fields to display JPanel inFieldPane  The text field is one of the most important widgets that allows the user to input text value in a single line format. To create a text field widget in Java Swing, you use JTextField class. Here are the constructors of the JTextField class: