Java How to get the most recently updated textfield value

  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Value
Click For Summary
The discussion focuses on implementing a Java Swing application that allows users to update four text fields representing different units of measurement. The main requirement is to capture the most recently updated field's value and name while reverting the previously updated field back to its default value of zero. The user interacts with the application by editing the fields and clicking a "Calculate" button, which should trigger the calculation based on the latest input. The provided code outlines the structure of the `ConversionCalculator` class, which includes the setup of text fields, buttons, and action listeners. Key points include the need to modify the `actionPerformed` method to correctly handle button actions and the suggestion to use `e.getActionCommand()` for identifying button clicks. Additionally, there is a mention of needing functionality to track which field is currently being updated, possibly through methods like `.isUpdating` or `.isChanging`. The discussion emphasizes the importance of correctly capturing user input and managing the state of the text fields effectively.
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:
Technology news on Phys.org
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K