0

i am trying to write a code where you have three fields pop up, and it will multiply the first two inputs and auto-update the third field and change the value and the text property of the last field while you enter the values into the first fields?

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Solver implements ActionListener {

    void actionPerformed(ActionEvent E){



        JTextField xField = new JTextField(5);
        JTextField yField = new JTextField(5);
        JTextField zField = new JTextField(5);

        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("x:"));
        myPanel.add(xField);
        myPanel.add(Box.createHorizontalStrut(15));
        myPanel.add(new JLabel("y:"));
        myPanel.add(yField);
        myPanel.add(Box.createHorizontalStrut(15));
        myPanel.add(new JLabel("z:"));
        myPanel.add(zField);

        int result = JOptionPane.showConfirmDialog(null, myPanel,
            "please enter X and Y value", JOptionPane.OK_CANCEL_OPTION);
        double x = Double.parseDouble(xField.getText());
        double y = Double.parseDouble(yField.getText());
        if(result == JOptionPane.OK_OPTION) {
            System.out.println(x+10.0);
            System.out.println(y + 10);

        }
    }
}

right now i have it printing both initial values added by 10 just to be sure they are set to doubles, but if someone could help me to make the zField change to whatever arithmetic that would be appreciated(and if someone can get rid of the error with the actionPerformed conflicting with ActionLister that would be greatly appreciated as well.)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    Perhaps add a **DocumentListener** or **DocumentFilter** for your JTextField's to pick up when JTextField data is changed. [See Here](https://stackoverflow.com/questions/25477133/interacting-with-multiple-textfields-with-keylistener-for-java). – DevilsHnd - 退職した Feb 12 '18 at 02:51

3 Answers3

2

You could...

Attach one or more ActionListeners to the JTextFields you are displaying and perform the required arithmetic when it's actionPerformed method is called.

The problem with this is, the user is then expected to press the "action" key (typically Enter) to trigger the ActionListener

See How to write an action listener for more details.

You could...

Use a FocusListener and monitor for focusLost events on the text fields and perform the required arithmetic when focus is lost.

The problem with this is the user needs to leave the last field before the focusLost event is triggered and it's not immediately obvious

See How to write a focus listener for more details

You could...

Use a DocumentListener which would notify you in real time as the fields are updated.

You could then perform the required arithmetic operations once you've established that both fields are in a valid state (ie not empty and contain appropriate numbers)

See How to write a Document Listener for more details

After thoughts...

Regardless of which direction you might choose, I'd personally create a single, custom class which implemented the required interface and which required a reference to each of the fields. This way it would be able to perform the required operations and update the required fields independenlty

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0
import javax.swing.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JTquestion {

    public static class Solver extends JFrame {
       public Solver(){

            setTitle("please enter X and Y value");
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(10,10,10,10);

            JTextField xField = new JTextField(5);
            JTextField yField = new JTextField(5);
            JTextField zField = new JTextField(5);
            JButton ok = new JButton("ok");

            JPanel myPanel = new JPanel(new GridBagLayout());
            myPanel.add(new JLabel("x:"));
            myPanel.add(xField,c);
            myPanel.add(new JLabel("y:"));
            myPanel.add(yField,c);
            myPanel.add(new JLabel("z:"));
            myPanel.add(zField,c);

            c.gridy = 1;

            ok.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    double x = Double.parseDouble(xField.getText());
                    double y = Double.parseDouble(yField.getText());

                    xField.setText(Double.toString(x));
                    xField.setText(Double.toString(y));

                    zField.setText(Double.toString(x*y));
                }

            });

            c.gridx = 3;
            c.gridy = 2;
            c.insets = new Insets(10,0,10,10);
            c.fill = GridBagConstraints.HORIZONTAL;
            myPanel.add(ok, c);
            add(myPanel);
            setVisible(true);
            pack();
            this.setSize(330, 150);
            this.setResizable(false);

        }
    }

    public static void main(String[] args){
        Solver s = new Solver();
    }
}
Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34
0

What you need is a key listener. Check out this java tutorial https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

  • No, this is precisely what they don't need. A `KeyListener` is never a good choice for for text components, not when you consider the `DocumentListener` or `DocumentFilter` APIs. `KeyListener` won't register text pasted into the field for starters – MadProgrammer Feb 12 '18 at 10:46