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.
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.
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.
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.
Joined: Jun 20, 2011
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.