Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java How to get the most recently updated textfield value

  1. Feb 6, 2017 #1

    BiGyElLoWhAt

    User Avatar
    Gold Member

    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)
    Code (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: Feb 6, 2017
  2. jcsd
  3. Feb 6, 2017 #2

    jedishrfu

    Staff: Mentor

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to get the most recently updated textfield value
Loading...