File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes retrieving textField value. Different approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "retrieving textField value. Different approach" Watch "retrieving textField value. Different approach" New topic
Author

retrieving textField value. Different approach

ibrahim yener
Ranch Hand

Joined: Jul 22, 2013
Posts: 126

Hello all

First of all i am sorry for title because i could not find anything suits my question.

Okay, let's start...

I have an experimental project and i am trying to add all GUI components into one different Class
and access them through another Class but problem is i cannot get value from components.

Please let me explain on a code.

Let's say we have a Swing class called A.class and content of it as follows



and i have class which holds Graphic Components called B.class and contents is follows


What am i trying to do is, just want to make things are simple. Instead of creating textField, textField_1, textField_2, textField_3 ... etc just create one method holds textField and call it whereever i want
So, things are working here but i cannot get value of textField when Button clicked.


Is there someway to get it?

Regards


WinSystems my Google play account.
Ping Kong
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Hi there.. you dont have a reference to the text field control.. I think you can not get its value like that. Instead, when you get the Text Field control from your reusable method, add it to a java.util.List and then display it on UI simultaneously. You can access the text field from the List at any time to get its value.
ibrahim yener
Ranch Hand

Joined: Jul 22, 2013
Posts: 126

Dear Tarun

Thanks for reply. Here i have couple of questions based on your answer.
Forgive my ignorance because i am learning Java and school just started 2 months ago.


So,
1 - You don't have a reference to the textField control - How can i do that?
2 - Add it to java.util.List - How can i add it into java.util.List
3 - Display it on UI simultaneously - How can i display it?

I really appreciate if you give me explanation.

Regards
Tarun Bolla
Ranch Hand

Joined: Jun 20, 2011
Posts: 89
Creating a reusable method to get a Text Field control (Component) created is good. But you cannot uniquely identify the Text Field until you have a reference variable for it.In the above code, field is a reference variable and you hardcode that reference variable in your code to get the value of that field.If you are creating a reusable method for returning Text Fields on the fly, you should also consider making the references of the newly created text fields available when needed. Consider the below code (Ignore my comment about List, lets take a Map. Maps are the data structures which will hold key value pairs).

Now, you have a place to refer to if you need value of the Text Field (The map). This is one way to do it.
Otherwise you can always hardcode the references in your code and create JTextField_1, JTextField_2, JTextField_3 at global level (Class level) which is absolutely fine and has no drawbacks.
ibrahim yener
Ranch Hand

Joined: Jul 22, 2013
Posts: 126

Dear Tarun
Thanks you so much for your reply. You just gave me new point of view and vision.

regards
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: retrieving textField value. Different approach
 
Similar Threads
refresh textField and clear textArea when button clicked
Refreshing a J-Frame
Caanot get username from a class
Can this old dog learn new tricks? Trying input window, manipulate string, output window
why this is not equal