Search code examples
javaswingjtextfieldcalculatorparseint

How to get text from a JTextfield and use it as an int


I have all of this in one file. I am trying to get the int from the text field jtfNumber1 and jtfNumber2 and then display the result of the action based on which button is pressed into into jtfResult. I have the buttons working. They currently display a test message. I am stuck with getting the string from the textfield.

public class Calculator extends JFrame {

    // Create Buttons and fields
    private JButton jbtAdd = new JButton("Add");
    private JButton jbtSub = new JButton("Subtract");
    private JButton jbtMul = new JButton("Multiply");
    private JButton jbtDiv = new JButton("Divide");
    private JTextField jtfNumber1 = new JTextField(8);
    private JTextField jtfNumber2 = new JTextField(8);
    private JTextField jtfResponse = new JTextField(8);

    public Calculator() {
        //Create Panel
        JPanel panel = new JPanel();
        panel.add(new JLabel("Number 1"));
        panel.add(jtfNumber1);
        panel.add(new JLabel("Number 2"));
        panel.add(jtfNumber2);
        panel.add(new JLabel("Result"));
        panel.add(jtfResponse);
        panel.add(jbtAdd);
        panel.add(jbtSub);
        panel.add(jbtMul);
        panel.add(jbtDiv);
        add(panel);
        BtnListener listener = new BtnListener();
        jbtAdd.addActionListener(listener);
        jbtSub.addActionListener(listener);
        jbtMul.addActionListener(listener);
        jbtDiv.addActionListener(listener);
    }

    class JtfListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int x = Intenger.parseInt(jtfNumber1.getText());
        }
    }

    class BtnListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtAdd) {
                System.out.println("Test Test Test");
            } else if (e.getSource() == jbtSub) {
                System.out.println("Test Test Test");
            } else if (e.getSource() == jbtMul) {
                System.out.println("Test Test Test");
            } else if (e.getSource() == jbtDiv) {
                System.out.println("Test Test Test");
            }
        }
    }

    public static void main(String[] args) {
        JFrame frame = new Calculator();
        frame.setTitle("Calculator");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Solution

  • You've almost done, a few changes in actionPerformed method

    public void actionPerformed(ActionEvent e) {
        int int1 = Integer.parseInt(jtfNumber1.getText());
            int int2 = Integer.parseInt(jtfNumber2.getText());
        if (e.getSource() == jbtAdd) {
            jtfResponse.setText(String.valueOf(int1+int2));
        } else if (e.getSource() == jbtSub) {
            jtfResponse.setText(String.valueOf(int1-int2));
        } else if (e.getSource() == jbtMul) {
            jtfResponse.setText(String.valueOf(int1*int2));
        } else if (e.getSource() == jbtDiv) {
            jtfResponse.setText(String.valueOf(int1/int2));
        }
    }
    

    You can directly implement ActionListner to your class

    public class Calculator extends JFrame implements ActionListener
    

    and overwrite actionPerformed method. Also your main method should be like

    public static void main(String[] args) {
        //AWT event dispatching thread
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new Calculator();
                frame.setTitle("Calculator");
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
    

    See Why SwingUtilities invokelater is needed