Advanced text box formatting?

First of all, I know that I used to ask a similar question, but that's different. I am trying to make a calculator that does the arithmetic mean of all numbers that have been entered into text fields that are being edited . I have about 25 text fields, however only 14-16 are edited at a time. The user has 3 jspinners to add or remove some editable text fields that are already in the program. My question is: how can you check which text fields are editable from these 25 and manage the data?

For moderation purposes, this question is different from another. In another matter, I was not at all specific, and everything was different, so please leave this open. I need help.

+4
source share
1 answer

Solve one problem at a time. Start with a working example. Modify 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(); } }); } } 
+6
source

Source: https://habr.com/ru/post/1388959/


All Articles