Retrieving values from fields

65 Views Asked by At

I cannot seem to retrieve the values from each of the fields created. I currently in my actionPerformed() method have f set to 1, but I would like to retrieve the values from all the created fields. It may be the case that not all fields are used.

Please see code below

package classes;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Classes extends JFrame implements ActionListener {

    private JTextField ivol, fvol;
    private JLabel ilabel, flabel;
    private JButton button;
    private final JTextField vol1HH[] = new JTextField[10];
    private final JLabel vollabel[] = new JLabel[10];
    private int f;

    public static void main(String[] args) {
        Classes RV = new Classes();
        RV.setSize(1000, 1200);
        RV.createGUI();
        RV.setVisible(true);

        double sum = add((double) 10, (double) 20.1, (double) 30.1, (double) 40.1);

        System.out.println(sum);

    }

    public static int add(Double... numbers) {

        int result = 0;
        for (Double number : numbers) {
            result += number;
        }
        return result;
    }

    public void createGUI() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(null);

        ivol = new JTextField(20);
        ivol.setBounds(150, 50, 100, 20);

        fvol = new JTextField(20);
        fvol.setBounds(150, 1000, 100, 20);

        ilabel = new JLabel("Initial Order Volume");
        ilabel.setBounds(10, 50, 150, 20);

        flabel = new JLabel("Final Order Volume");
        flabel.setBounds(10, 1000, 150, 20);

        button = new JButton("Remaning Volume");
        button.setBounds(10, 1050, 150, 20);
        button.addActionListener(this);

        window.add(ivol);
        window.add(fvol);
        window.add(ilabel);
        window.add(flabel);
        window.add(button);

        for (int y = 100; y < 1000; y += 50) {
            for (int f = 0; f < 10; f++) {
                vol1HH[f] = new JTextField(10);
                vol1HH[f].setName("volHH" + f);
                vollabel[f] = new JLabel("HH34");
                vol1HH[f].setBounds(150, y, 100, 20);
                vollabel[f].setBounds(80, y, 100, 20);
                window.add(vol1HH[f]);
                window.add(vollabel[f]);
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //double vol;
        f = 1;
        double vol = Double.parseDouble(vol1HH[f].getText());
        //if(e.getSource() == button) {
        fvol.setText(Double.toString(vol));
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

If you want to retrieve all records from textfields and store it some where. You need to have a variable for storing.

public class Classes extends JFrame implements ActionListener {
    //Your other variable declarations
    String [] fieldRecords = new String[10];  //Declare array for storing textfields' data
}

Now, simply create a method to transfer all input be it empty of filled into the fieldRecords[].

public void retrieve(){
    for(int x=0; x<vol1HH.length; x++)
        fieldRecords[x] = vol1HH[x].getText();
}

When ever you need to retrieve your records, simply call retrieve(). For example when you press the button - hence you can place it in the actionPerformed().

public void actionPerformed(ActionEvent e) {
    //Your other actions..
    retrieve();   //Send all textfield data into fieldRecord[]
}

With the data saved into the array. You can print it or set it back onto the textfields any time you need.

Example:

public void display(){
    for(int x=0; x<fieldRecords.length; x++)
        System.out.println(fieldRecords[x]);
}


public void load(){
    for(int x=0; x<vol1HH.length; x++)
        vol1HH.setText(fieldRecords[x]);
}