How to get the most recently updated textfield value

  • Context: Java 
  • Thread starter Thread starter BiGyElLoWhAt
  • Start date Start date
  • Tags Tags
    Value
Click For Summary
SUMMARY

This discussion focuses on implementing a Java Swing application that captures the most recently updated text field value among four fields: centimeters, inches, meters, and yards. The user can update any field, and upon clicking the "Calculate" button, the application should pass the value and name of the most recently updated field to the calculate() method while resetting the previously updated field to its default value of "0.00". The solution involves using ActionListener to handle button clicks and updating the text fields accordingly.

PREREQUISITES
  • Java programming knowledge, specifically with Swing components
  • Understanding of ActionListener interface in Java
  • Familiarity with event handling in GUI applications
  • Basic knowledge of Java's JPanel and layout managers
NEXT STEPS
  • Implement the logic in the calculate() method to process the most recently updated field
  • Explore Java Swing's JTextField and its event handling capabilities
  • Learn about maintaining state in GUI applications to track field updates
  • Review best practices for using ActionListener in Java Swing applications
USEFUL FOR

Java developers, particularly those working on GUI applications using Swing, and anyone looking to implement dynamic form behavior in their applications.

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

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
6K
Replies
2
Views
2K
  • · 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
5K