I am doing my first java program with a GUI. I need to change the input from the user entered into a jTextField to a int variable i can use for work.
Asked
Active
Viewed 1,743 times
0
-
String var = val.getText(); – Java_Alert Dec 10 '13 at 05:44
-
2A better choice would be to use a field designed for the work like a `JSpinner` or `JFormattedField`... – MadProgrammer Dec 10 '13 at 05:47
5 Answers
0
Get the text and use Integer.parseInt(your String here);

prmottajr
- 1,816
- 1
- 13
- 22
-
int quarters = Integer.parseInt(jTextField); i get a syntax error on jTextField – sobey Dec 10 '13 at 05:45
-
0
int a = Integer.parseInt(jtextfield.getText());
// `jtextfield` will be your `JTextField` object

AJ.
- 4,526
- 5
- 29
- 41
0
JTextField jTextField=new JTextField(); // initialize textFild
String str=jTextField.getText(); // get text value
Now you can convert this into int value.
int val=Integer.parseInt(jTextField.getText());

Ruchira Gayan Ranaweera
- 34,993
- 17
- 75
- 115
0
jtextfield.gettext()
will return the entered String..
we can parse this string into integer
integer.parseInt(txtfield.getText());
but if we are typing values other than numbers 0-9
in the text filed it show java.lang.NumberFormatException
only numbers can be parsed into the integer
int num=Integer.parseInt("2")// is correct
int num=Integer.parseInt("two")//this will give numberformat exception

Java_Alert
- 1,159
- 6
- 24
- 50

Lijo
- 6,498
- 5
- 49
- 60
0
int result = Integer.parseInt(jTextField.getText());
and apply
class MyIntFilter extends DocumentFilter {
@Override
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
} else {
// warn the user and don't allow the insert
}
}
private boolean test(String text) {
try {
Integer.parseInt(text);
return true;
} catch (NumberFormatException e) {
return false;
}
}
@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);
if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
} else {
// warn the user and don't allow the insert
}
}
@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();
sb.append(doc.getText(0, doc.getLength()));
sb.delete(offset, offset + length);
if (test(sb.toString())) {
super.remove(fb, offset, length);
} else {
// warn the user and don't allow the insert
}
}
}
(This class copied from Link)
PlainDocument doc = (PlainDocument) jTextField.getDocument();
doc.setDocumentFilter(new MyIntFilter());
This way you can restrict your input to numbers and get the string to integer by parsing it