Results 1 to 12 of 12
- 03-20-2011, 02:32 PM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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:
In the same class i can use this to display a name i fill in, it works fine.Java Code:public String getName() { String name = nameTextField.getText(); return name; }
Then i have a controller klasse where i wanna use this method to. So i test this really simple command:
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?Java Code:Panel start = new Panel(); System.out.println(start.getName());
Thanks!
Kind regards,
André
- 03-20-2011, 02:38 PM #2
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.
- 03-20-2011, 03:09 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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?
because this doens't work, i think i'm doing something wrong. Or i didn't fully understand.Java Code:public StartGame(){ StartForm(this); }
Kind regards,
André
-
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.
- 03-20-2011, 07:22 PM #5
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
As Fubarable said
Java Code:[B]YourPanelClass[/B] start = new [B]YourPanelClass[/B](); System.out.println(start.getName());
-
- 03-20-2011, 07:29 PM #7
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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.
- 03-20-2011, 07:34 PM #8
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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é
- 03-20-2011, 07:36 PM #9
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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.
- 03-20-2011, 07:49 PM #10
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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.
- 03-20-2011, 08:01 PM #11
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
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 } }
- 03-20-2011, 08:04 PM #12
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
Similar Threads
-
Problem to access data when a class calls another class
By ea09530 in forum New To JavaReplies: 0Last Post: 04-04-2010, 10:06 AM -
How to show class initialize data in JSP ?
By tking88 in forum New To JavaReplies: 0Last Post: 03-07-2010, 09:17 AM -
help me to store data in a class
By neeraj.singh in forum New To JavaReplies: 2Last Post: 08-06-2009, 08:31 AM -
accessing the text of textfield of one class in another
By vasavi.singh in forum Advanced JavaReplies: 3Last Post: 04-05-2009, 02:54 PM -
update textField in a window extending Dialog class
By Ramona in forum New To JavaReplies: 18Last Post: 11-22-2008, 05:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks