Results 1 to 12 of 12
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default Getting data from textfield into another class

    I have a public frame and a public panel class, the panel class is called panel
    In my panel class i do this:


    Java Code:
    public String getName()
        {
            String name = nameTextField.getText();
            return name;
        }
    In the same class i can use this to display a name i fill in, it works fine.

    Then i have a controller klasse where i wanna use this method to. So i test this really simple command:

    Java Code:
    Panel start = new Panel();
    System.out.println(start.getName());
    The output is and empty string. How can i get the data i put into a form textfield in another class? In the way i'm showing you. What did i forget?

    Thanks!

    Kind regards,

    André

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    a solution is to have a constructor in the controller class that take a refence of the public frame or of the type String. assuming that the controller class is instantiate before the public frame class, you can pass the reference by a call PublicFrameClass(this) or by PublicFrameClass(String).
    Last edited by j2me64; 03-20-2011 at 02:41 PM.

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Hi J2Me64,

    My controller class is StartForm.
    With a reference to my Panel.

    Panel panel = new Panel();

    In the Panel i call a controller for starting a game, so its not on forehand.
    I call this controller class StartGame.


    How do i set this reference?

    Java Code:
     public StartGame(){
          StartForm(this);
      }
    because this doens't work, i think i'm doing something wrong. Or i didn't fully understand.

    Kind regards,

    André

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to have a reference to the Panel that is actually being displayed for that method to make any sense. You're creating a completely new Panel object in your controller and it has no relation to the displayed Panel object.

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    As Fubarable said

    Java Code:
    [B]YourPanelClass[/B] start = new [B]YourPanelClass[/B]();
    System.out.println(start.getName());

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by milovan View Post
    As Fubarable said

    Java Code:
    [B]YourPanelClass[/B] start = new [B]YourPanelClass[/B]();
    System.out.println(start.getName());
    No, this not what I'm suggesting and is what the OP is already doing, so this won't help, I'm afraid. What the OP needs to do is to pass a reference to the currently visualized Panel object into the other class that needs to call its methods, that is the key here.

  7. #7
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Sorry man, I just took a look at his code and didn't read his post so...
    Last edited by milovan; 03-20-2011 at 07:34 PM.

  8. #8
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Thanks for your reply Fubarable. Your right i was making a new panel object.

    I have solved it from the other side. Make and instance to the controller (handler) and fill it from the panel to the handler. This works fine.

    But i'm very interested now.

    So do you have and example of making an instance to the object thats already alive (visible). Like you said. Is this possible.

    Kind regards,

    André

  9. #9
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    Yeah Milovan,

    I that is what i meant. This i didn't know. I think this is what Fubarable meant to.
    Does this really work like this:

    public StartGame(){
    StartForm(myPanel);
    }

    Kind regards,

    André
    Last edited by aborgeld; 03-20-2011 at 07:39 PM.

  10. #10
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    I tried the last option, this doesn't work:
    But if anybody knows, i like to see an example of how you instantiate the object who's already alive (visible) and not a new one.

  11. #11
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Check if this would help

    Java Code:
    class Example{
        JTextArea txt = new JTextArea(...);
        ....
        AnotherClass class = new AnotherClass(txt);
        ...
    }

    And another class that needs to use the same JTextArea

    Java Code:
    class AnotherClass{
    
        JTextArea txt;
    
        public AnotherClass(JTextArea newTxt){
            this.txt = newTxt;
            ...
            // do something with txt
        }
    }

  12. #12
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    And the newTxt position you fill with the txtFld.getTxt option.
    This is how i solved it.
    Thanks for the reply's.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  2. How to show class initialize data in JSP ?
    By tking88 in forum New To Java
    Replies: 0
    Last Post: 03-07-2010, 09:17 AM
  3. help me to store data in a class
    By neeraj.singh in forum New To Java
    Replies: 2
    Last Post: 08-06-2009, 08:31 AM
  4. accessing the text of textfield of one class in another
    By vasavi.singh in forum Advanced Java
    Replies: 3
    Last Post: 04-05-2009, 02:54 PM
  5. Replies: 18
    Last Post: 11-22-2008, 05:25 PM

Posting Permissions

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