5

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);
    }
 }
MBC870
  • 373
  • 3
  • 8
  • 16
  • 1
    What is your problem? `Integer.parseInt(stringVariable)` should convert a `String` into an `int`. And I see you are using it. – Kazekage Gaara Jun 17 '12 at 12:17
  • What is the error that you are getting? – Fahim Parkar Jun 17 '12 at 12:20
  • 1
    `NOTE: this code does not compile at the moment, before it was but now obviously it won't since it is a mess.`: the solution is to "un-mess" it. Fix any errors of course. If there are any that you don't know how to fix, then you tell us about the errors since we can't read minds. – Hovercraft Full Of Eels Jun 17 '12 at 12:24
  • I have two files the one up here and I am working on another file to implement the action listener, now my concern is what I have to do in this file in order for me to link it to the one up here. If this is done I am sure the error disappears... – MBC870 Jun 17 '12 at 12:31

4 Answers4

14

In place of:

JTextField f1 = new JTextField("-5");

//xaxis1 = Integer.parseInt(f1);

try this:

JTextField f1 = new JTextField("-5");
String text = f1.getText();
int xaxis1 = Integer.parseInt(text);

You cannot parse TextField to Integer, but you can parse its value contained - text.

dantuch
  • 9,123
  • 6
  • 45
  • 68
2

You have two main errors that immediately leap to mind:

  • First you're trying to parse the JTextField itself, not the text it holds (as pointed out by dantuch -- 1+ to him).
  • Next, even if you were to successfully parse the text held by the JTextField, doing so at this point in your program is not productive since you'd be doing it at the time of JTextField creation, and would not have given the user a chance to change the values held by the fields.

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

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:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>lib-core</artifactId>  
    <version>0.1</version>  
</dependency>  
Puce
  • 37,247
  • 13
  • 80
  • 152
0

You need to parse the value of the TextField:

int i = Integer.parseInt("-10");

Similarly

double d = Double.parseDouble("-10.0");

etc...

bluish
  • 26,356
  • 27
  • 122
  • 180
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75