Assigning multiple values from a JFormattedTextField to multiple variables
-
07-07-2021 - |
Frage
If I had a JFormattedTextField
like this
MaskFormatter formatter = new MaskFormatter("#,#");
JFormattedTextField textField = new JFormattedTextField(formatter);
and if I had variables
int x = 0;
int y = 0;
how can I store the first number in the textfield to x
, and the second number to y
?
Lösung
Assuming the first & second numbers are those either side of the comma ,
in the JFormattedTextField
, you could do:
String[] numbers = textField.getText().split(",");
int x = Integer.parseInt(numbers[0]);
int y = Integer.parseInt(numbers[1]);
Andere Tipps
The mask does not change how the inner value is stored, it just tells how to represent/input it.
So you still have a .getText()
which returns a String in the format of your election. Process that String (split()
, StringTokenizer
) as you see fit.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow