Results 1 to 8 of 8
- 08-04-2011, 05:17 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
JFields,variables, declaration, access
Ok, let's say I have a simple Window with 1 text field and a button. Diagram:
class Window{
constructor Window{
JTextFiled name("nomine")
JButon button("store")
button.addActionListener(handler)
}
class Handler implements actionListener{...}
// triggered when 'store' button is pressed
}
If I try to store the values from JTextField directly in constructor I get only "nomine" stored, because obviously, it is jet to be filled.
If I try to access it from another method in the Window class, let's say getNames(), I get an error 'cannot find symbol: variable name'.
1. How am I supposed to access these variables?
2. What should I use if I want to input numers (birthyear, height...) ?
- 08-04-2011, 05:28 AM #2
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
It is not accessible to the other methods because it is declared within the constructor. If you want access to a variable in more than one method you must declare it outside of the method.
- 08-04-2011, 06:10 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
Oh, great, thanx :) So I've made :
class Window{
public static String[] textData = new String[3];
constructor Window{...}
class actionListener{...
textData[0]=JTF1.getText();
textData[1]=JTF2.getText();
textData[2]=JTF3.getText();
//some method from other class...whatever
}
}
So, when I press the button ("store") the values are copied from text fields into 'textData' array. Only then is action performed.
I can access that array from all the methods of the Window class, and even from other classes within the package :)
(correct me if I'm wrong)
So, how do I involve numbers here?
Should I somehow transform 'String to Double' or use different kind of 'input fields' ?
- 08-04-2011, 06:22 AM #4
Anything entered into a textfield is text. If you enter the digit 3 then it will be the String "3" and not the int 3. So storing it into your array is fine unless you actually want the int 3 to do some math with it. In that case you need to parse the String into an int.
- 08-04-2011, 06:31 AM #5
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
Ok, and after parsing String into Int, when I use that Int in the equation with at least one Double, it will be 'seen' as a Double?
This works for importing numbers from text fields into integer array:
numberData[2]=Integer.parseInt(JTF1.getText());Last edited by Ike; 08-04-2011 at 06:41 AM.
- 08-04-2011, 06:47 AM #6
If you want the value as a double then parse it as a double instead of an int.
- 08-04-2011, 07:02 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 21
- Rep Power
- 0
Oh, stupid me.
Ok, now I know how to input Strings, Ints and Doubles, and how to manipulate them from various methods. Thanx guys.
Have enough material to play for a week :) See you when next problem occurs.
- 08-04-2011, 07:53 AM #8
Similar Threads
-
Access object variables
By AcousticBruce in forum New To JavaReplies: 6Last Post: 01-10-2011, 09:41 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
How to manage/access variables between classes
By dan0 in forum New To JavaReplies: 2Last Post: 04-03-2009, 12:53 AM -
Private or Protected access for super class variables
By Madushan in forum New To JavaReplies: 3Last Post: 03-14-2009, 07:22 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks