How to get the most recently updated textfield value

  • Java
  • Thread starter BiGyElLoWhAt
  • Start date
  • Tags
    Value
In summary, the conversation is about creating a conversion calculator in Java with 4 text fields that can be updated by the user. The goal is to get the value from the most recently updated field and pass it to the calculate() method, while also reverting the other fields to their previous values. The conversation also mentions using the action listener interface to capture field changes and provides a code example.
  • #1
BiGyElLoWhAt
Gold Member
1,622
131
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:
Technology news on Phys.org

1. How can I get the most recently updated value from a textfield?

To get the most recently updated value from a textfield, you can use the onchange event listener in JavaScript. This event will fire whenever the value of the textfield is changed, allowing you to retrieve the updated value.

2. Can I use jQuery to get the most recently updated textfield value?

Yes, you can use jQuery's change() method to get the most recently updated value from a textfield. This method will trigger an event when the value of the textfield changes and allow you to retrieve the updated value.

3. Is there a way to get the most recently updated value from a textfield without using JavaScript?

No, you will need to use JavaScript or a JavaScript library like jQuery to get the most recently updated value from a textfield. This is because HTML does not have a built-in mechanism for tracking changes to input fields.

4. How can I get the most recently updated value from multiple textfields?

If you have multiple textfields on your page and want to get the most recently updated value from each of them, you can give each textfield a unique id and use the document.getElementById() method in JavaScript to retrieve the updated value for each textfield.

5. Can I get the most recently updated value from a textfield in real-time?

Yes, you can use the oninput event listener in JavaScript to get the most recently updated value from a textfield in real-time. This event will fire every time a user enters or deletes a character in the textfield, allowing you to retrieve the updated value immediately.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
913
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
9
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top