4

First of all, I know I asked a similar question before but this one is different. I'm trying to make a calculator that makes the arithmetic mean of all the numbers that were inputted in the text fields that are editable. I have about 25 text fields however only 14-16 are editable at a time. The user has 3 jspinners to add or remove some editable text fields which are already in the program. My question is, how can you check which text fields are editable from those 25 and manipulate the data?

For moderation purposes, this question is different than the other. In the other question I was not specific at all, and it was different, so please leave this one open I need help.

Bugster
  • 1,552
  • 8
  • 34
  • 56
  • 2
    Please provide an [sscce](http://sscce.org/) that shows your current approach to the [problem](http://stackoverflow.com/q/8680285/230513). For brevity, show the minimum number of text fields and spinners. – trashgod Jan 02 '12 at 17:21
  • The code of the program is 1600+ lines, and is terribly messy. As for my approach I have none, I have tried going through IF statements manually but I just get 14 java errors when I click the button. Minimum number of text fields is 13 and maximum is 25. I have 3 jspinners which are always active. The minimum value of the first is 5 and max is 12. The minimum of the 2nd is 0 and maximum is 3. The minimum of third is 0 and maximum is 5. – Bugster Jan 02 '12 at 17:24
  • @user1115506 like trashgod said, it's difficult to help you if you don't post a testcase. Try going through your code again and extract the relevant pieces and pastebin it. – asgs Jan 02 '12 at 17:43
  • I just tried adding it, it says there's a 3000 chars limit. – Bugster Jan 02 '12 at 17:46
  • Prepare some small *demos* that represent exactly your question and try to understand what is going on, what is happening, what is not happening etc and later embed that functionality to your actual application, in case it succeeds and in case, you find some difficulties, post specifically and you will obviously be able to get help here. Otherwise, it's quite difficult here to help you because it requires some practical stuff you're trying to achieve. – Lion Jan 02 '12 at 17:49
  • did you heard about JTable, create a print_screen stick here by edit your post – mKorbel Jan 02 '12 at 18:08
  • @Lion Your description sounds like an SSCCE. – Andrew Thompson Jan 03 '12 at 00:17

1 Answers1

6

Solve one problem at a time. Start with an working example. Change it to handle multiple fields, as shown below. Finding the average is now a simple change to update().

image

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see https://stackoverflow.com/q/8703464/230513
 * @see https://stackoverflow.com/questions/6803976
 */
public class Adder extends JPanel {

    private static final int MAX = 3;
    private final List<JFormattedTextField> fields =
        new ArrayList<JFormattedTextField>();
    private final NumberFormat format = NumberFormat.getNumberInstance();
    private final JFormattedTextField sum = new JFormattedTextField(format);

    public Adder() {
        this.setLayout(new GridLayout(0, 1));
        for (int i = 0; i < MAX; i++) {
            JFormattedTextField tf = init();
            fields.add(tf);
            this.add(tf);
        }
        sum.setHorizontalAlignment(JFormattedTextField.RIGHT);
        sum.setEditable(false);
        sum.setFocusable(false);
        this.add(sum);
    }

    private JFormattedTextField init() {
        JFormattedTextField jtf = new JFormattedTextField(format);
        jtf.setValue(0);
        jtf.setHorizontalAlignment(JFormattedTextField.RIGHT);
        jtf.addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(FocusEvent e) {
                EventQueue.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        update();
                    }
                });
            }
        });
        jtf.addPropertyChangeListener("value", new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent e) {
                update();
            }
        });
        return jtf;
    }

    private void update() {
        long total = 0;
        for (JFormattedTextField tf : fields) {
            Number v = (Number) tf.getValue();
            total += v.longValue();
        }
        sum.setValue(total);
    }

    private void display() {
        JFrame f = new JFrame("Adder");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Adder().display();
            }
        });
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045