I am new to JAVA, I am trying to convert an input from a JTextField into an integer, I have tried loads of options but nothing is working, the eclipse is always giving me an error and the errors are not making sense to me.
import java.awt.Graphics; import java.awt.Color;
public class circle extends Shape{
public int x;
public int y;
public int Radius;
public circle (int Radius, int x, int y, Color c){
super(c);
this.x = x;
this.y = y;
this.Radius = Radius;
}
public void draw(Graphics g){
g.setColor(super.getColor());
g.fillOval(x-Radius, y-Radius, Radius * 2, Radius * 2);
}
}
You have two main errors that immediately leap to mind:
A better solution would be to parse the text held by the JTextField, again as dantuch suggests, but to do so in a listener of some sort, perhaps an ActionListener that has been triggered by a JButton push.
In place of:
try this:
You cannot parse
TextField
toInteger
, but you can parse its value contained - text.I've implemented number fields based on JFormattedTextField.
They also support a min and a max value.
Maybe you find them useful (the library is open source):
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JRealNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JDoubleField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JFloatField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedRealNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedDoubleField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLocalizedFloatField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JWholeNumberField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JByteField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JIntegerField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JLongField.html
http://softsmithy.sourceforge.net/lib/docs/api/org/softsmithy/lib/swing/JShortField.html
Tutorial:
http://softsmithy.sourceforge.net/lib/docs/tutorial/swing/number/index.html
Homepage:
http://www.softsmithy.org
Download:
http://sourceforge.net/projects/softsmithy/files/softsmithy/
Maven:
You need to parse the value of the
TextField
:Similarly
etc...