0

I'm making a program that calculates rules of three, but I don't want the calculations to be started when I press button but when the 3 needed values are added. Here's the image of my program and my code:

enter image description here

private void calculateActionPerformed(java.awt.event.ActionEvent evt) {                                          
    if (field4.getText().isEmpty()) {
        field4.setText(null);
        field1c = Float.parseFloat(field1.getText());
        field2c = Float.parseFloat(field2.getText());
        field3c = Float.parseFloat(field3.getText());
        float result = (field3c * field2c) / field1c;
        String resultfinal = Float.toString(result);
        field4.setText(resultfinal);
        option = 1;
    } else if (field3.getText().isEmpty()) {
        field1c = Float.parseFloat(field1.getText());
        field2c = Float.parseFloat(field2.getText());
        field4c = Float.parseFloat(field4.getText());
        float result = (field4c * field1c) / field2c;
        String resultfinal = Float.toString(result);
        field3.setText(resultfinal);
        option = 2;
    } else if (field1.getText().isEmpty()) {
        field2c = Float.parseFloat(field2.getText());
        field3c = Float.parseFloat(field3.getText());
        field4c = Float.parseFloat(field4.getText());
        float result = (field3c * field2c) / field4c;
        String resultfinal = Float.toString(result);
        field1.setText(resultfinal);
        option = 3;
    } else if (field2.getText().isEmpty()) {
        field1c = Float.parseFloat(field1.getText());
        field3c = Float.parseFloat(field3.getText());
        field4c = Float.parseFloat(field4.getText());
        float result = (field4c * field1c) / field3c;
        String resultfinal = Float.toString(result);
        field2.setText(resultfinal);
        option = 4;
    }

I'm sure that I have to use threads somewhere but I'm not sure how to implement them to my program.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    You can use a `FocusListener` to monitor the changes in the focus changes and make decisions about when a calculation should be made. You can also attach a `ActionListener` to the fields – MadProgrammer Mar 26 '18 at 00:02
  • Is this GUI using Swing, AWT, Java-FX, SWT.. ? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 26 '18 at 03:30

1 Answers1

-2

First asign to every input a KeyListener so they'll know when you've typed, pressed or released a key on them. Everytime you press or type a key on any input, loop through each one and .getValue(), if more than one has something different than "", then don't calculate.

First create a class that implements KeyListener so your inputs can use it.

class LoopWhenTyped implements KeyListener{

        public void keyTyped(KeyEvent e) {
            for(Component actualComponent : e.getSource().getParent().getComponents){
                 //here's where you're looping
            }
        }

        public void keyPressed(KeyEvent e) {}

        public void keyReleased(KeyEvent e) {}

}

In the class that holds your GUI, create an instance of the class we just created.

private myKeyListener = new LoopWhenTyped();

You must add this listener to every input:

field1.addKeyListener(myKeyListener);
field2.addKeyListener(myKeyListener);
field3.addKeyListener(myKeyListener);
field4.addKeyListener(myKeyListener);

The e variable references a KeyEvent, when you, so e.getSource() returns whatever triggered that event, getParent() will return the component that holds it, and getComponents() returns all the components it holds.

Of course, you must adapt this general solution to your code structure and research a little bit about this classes if you get lost. I hope it helped.

MarksASP
  • 494
  • 6
  • 15