Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By yellowledbet

Thread: JFields,variables, declaration, access

  1. #1
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default 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...) ?

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    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.
    Fubarable likes this.

  3. #3
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default

    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' ?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  5. #5
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default

    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you want the value as a double then parse it as a double instead of an int.

  7. #7
    Ike
    Ike is offline Member
    Join Date
    Aug 2011
    Posts
    21
    Rep Power
    0

    Default

    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.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    Default

    Quote Originally Posted by Ike View Post
    constructor Window{...}
    Huh? This is a Java forum, are you sure you're in the right place?

    db

Similar Threads

  1. Access object variables
    By AcousticBruce in forum New To Java
    Replies: 6
    Last Post: 01-10-2011, 09:41 AM
  2. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 10:50 AM
  3. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 07:41 PM
  4. How to manage/access variables between classes
    By dan0 in forum New To Java
    Replies: 2
    Last Post: 04-03-2009, 12:53 AM
  5. Replies: 3
    Last Post: 03-14-2009, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •