How to get the most recently updated textfield value

  • Context: Java 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Value
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 1K views
BiGyElLoWhAt
Gold Member
Messages
1,637
Reaction score
138
I couldn't find this online, but basically, I have 4 text fields that can be updated as the user wishes, but I want to get the value from the most recently updated one, and have the others revert to their previous values.

Say initially all fields are zero. A user edits one field and hits the calculate button, I want to pass the value of the field and the name of the field to my calculate() method.

Say fields are zero, a user changes one field, then goes and changes another field. I want the first changed field to go back to zero, and to pass the second field name and values to my calculate() (assuming they hit calculate)
Java:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ConversionCalculator extends JFrame implements ActionListener {
 
    String defaultValue = "0.00";
    JPanel center;
    JPanel east;
 
    JLabel cm;
    JLabel in;
    JLabel m;
    JLabel yd;
 
    TextField centimeters;
    TextField inches;
    TextField meters;
    TextField yards;
 
 
    public ConversionCalculator()
    {
     
     
     
        setTitle("Conversion Calculator");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setBounds(200, 200, 800, 100);
     
        center = new JPanel(new GridLayout(2,4)); //2 rows, 4 cols
        east = new JPanel(new GridLayout(3,1));
     
        //Center data
     
        cm = new JLabel("Centimeters: ");
        center.add(cm);
     
        centimeters = new TextField(defaultValue);
        center.add(centimeters);
     
     
        in = new JLabel("Inches: ");
        center.add(in);
     
        inches = new TextField(defaultValue);
        center.add(inches);
     
        m = new JLabel("Meters: ");
        center.add(m);
     
        meters = new TextField(defaultValue);
        center.add(meters);
     
        yd = new JLabel("Yards: ");
        center.add(yd);
     
        yards = new TextField(defaultValue);
        center.add(yards);
     
        //East Data
     
        JButton clear = new JButton("Clear");
        clear.addActionListener(this);
        east.add(clear);
     
        JButton calculate = new JButton("Calculate");
        calculate.addActionListener(this);
        east.add(calculate);
     
        JButton exit = new JButton("Exit");
        exit.addActionListener(this);
        east.add(exit);
     
     
     
     
     
        add(center);
        add(east,BorderLayout.EAST);
     
     
    }

    private void calculate()
    {
     
     
     
     
    }
 
    private void clear()
    {
        centimeters.setText(defaultValue);
        inches.setText(defaultValue);
        meters.setText(defaultValue);
        yards.setText(defaultValue);
    }
 
 

     

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.equals("Clear"))
        {
            clear();
        }
        else if(e.equals("Calculate"))
        {
         
        }
        else if(e.equals("Exit"))
        {
            System.exit(0);
        }
     
    }

}

I believe the thing I'm looking for is something to the effect of .isUpdating or .isChanging or something like that.

Edit* Slight Change to the code. My if else if statements should have the argument e.getActionCommand().equals("argument")
 
Last edited:
on Phys.org