• Post Reply Bookmark Topic Watch Topic
  • New Topic

getText of dynamically added JTextFields individually

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am adding dynamically subPanel to jPanel1 (with 4 jTextFields, 2 jComboBoxes and 2 jButton).



Here is code of AddButton:


I could getText of JTextField using this code:


But the result is not that I expected. I want to get access to each JTextFields and JcomboBoxes but I don't know how to get it.
So I would like to ask how can I getText() and getSelectedItem() of each JTextFields and JComboBoxes individually?

Thank you in advance.
 
Campbell Ritchie
Marshal
Posts: 52565
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What does
me = this;

mean?
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you

I am sorry my Java skills not so well, I borrowed add subPanel part from there: http://java-buddy.blogspot.jp/2014/02/java-swing-example-add-and-remove-ui.html
 
Campbell Ritchie
Marshal
Posts: 52565
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of things you find on the net. That code on that link has some peculiar features: for example part of it is updated to use Java8 features and part not. The part I quoted is particularly odd. You are adding a reference to the same object as a field of that object. That might create a circular dependency. What actually happens in the code on the link is that you are creating a sort of linked list of panels, so you can add them on top of one another like a stack, and remove the most recently created. An interesting idea but not one I would use myself.

I am not convinced it is a good idea to have GUIs which appear and disappear. Let alone add and remove components dynamically. That will give a very peculiar appearance. If you want to change the display, you should consider a card layout or a tabbed pane or similar.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the best way to add dynamically JTextFields and JComboBoxes to JPanel. Maybe necessary store all JTextFields and JComboBoxes in JComponent and add JComponent to JPanel dynamically create them. But in this case I don't know how to access to each JTextFields
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In you SubPanel class you need to create "getter" methods.

When you create the text fields and combo boxes for the SubPanel you must give the components names. So usually you create a "getter" method to access the component. So lets say the two text fields on the SubPane are used to enter a persons first name and last name. So in your SubPanel class you would create two text fields:


Then you need to create getter fields for each component:

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob Camick Thank you for reply. In console I can't get text of firstName. Maybe I am missing something.

 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all class names start with an upper case character. The class name is SubPanel NOT "subpanel".

In the ActionListener you can't create a new SubPanel because the component is not displayed on the GUI so of course there will be no data in the text fields.

The ActionListener needs to access a SubPanel that is already visible on the GUI. So In you MainPanel class you also need a method to access a SubPanel that you add to the MainPanel. Maybe something like:



So now in your ActionListener whenever you want to get the text for the first name your code might be something like:



If you need more help then post a proper SSCCE that demonstrates the problem.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I just started to learn Java by myself and hope to improve in coderanch.
I have a question, in this code should I give the value of int:

 
Campbell Ritchie
Marshal
Posts: 52565
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you pass an int like 3 you will get the No 3 sub‑panel. (If you use 0‑based indices like in arrays, then No 3 is actually the 4th panel and the 1st panel is No 0). So the answer to your question is yes.

But why would you want to do that at all? Why would anybody want multiple panels on top of one another?
 
Campbell Ritchie
Marshal
Posts: 52565
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you are seeking sub‑panels in a List, so the first panel will be No 0.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally lost in this getText problem.
I don't know why but I am getting this error:
 
Campbell Ritchie
Marshal
Posts: 52565
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code posted on the link does not use the me reference specifically; in fact you can remove that field and replace all instances of me with this and not notice any difference.

And the mistaken spelling of the subPanel class was in the link you provided.
 
Campbell Ritchie
Marshal
Posts: 52565
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try to find No 0 of an empty List it will not be there, so you will suffer that Exception.
You can only find No 0 if there is 1 element in the List (or more).
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I deleted all of the "me" from SubPanel.
I get a stuck in this code getSubPanel(int index), because by Button I am adding new SubPanel() dynamically so if I want to getText from TextField, I cannot give fixed value of int because there can be added more than 20 or 50 SubPanels. I don't know how to be with getSubPanel(index), because this index gives me error.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot give fixed value of int because there can be added more than 20 or 50 SubPanels.


Exactly. The problem with your question is you don't know what text field you want the text from.

If you want it from a specific panel, then somehow you need to know the index of the panel.

If you want it from all panels, then you need to create a loop and get the text from all panels.

You can't just randomly pick a panel. Somewhere in your logic you need to know the index.

Maybe you should be using a JTable.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course I need the text of exact dynamically added jTextField.
For example all my jTextFields storing in SubPanel:


And as you suggested in MainPanel class we have a method to access a SubPanel:

And on saveButton:

am I in right way?

 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why do you keep using the new keyword in your listener code. I already told you not to do this previously.

If you create a new MainPanel(), then the panel will be empty. You need to keep a reference to the MainPanel when you initially create the MainPanel.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If write like this:

it gives me error: non-static method getSubPanel(0) cannot be referenced from a static context
so should I write:

 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my last response. If you don't know what a reference is, I can't teach you Java basics.

Somewhere in you code your have to:

1) create a JFrame
2) create a MainPanel and add the panel to the frame (so you need to keep the reference to the MainPanel so you can access it in your ActionListener).
3) create a SubPanel and add the panel to the main panel
4) repeat step 3.
5) create a JButton and add the button to the frame.

So the simplified code might be something like:



So now you need to add an ActionListener to the button to loop through all the SubPanels on the main panel. The code might be something like:



The point is if you want to access your GUI components you need a reference to the component. In this case you need to keep a reference to the MainPanel so you can get all the SubPanels.

I hope that helps.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that the add ActionListener code that you suggested have some disadvantages:
1. loop will never advance (i will always be 0)
2. it is blindly casting the contents of main without actually knowing what's on it
3. MainPanel already has a List of SubPanels.

Anyway thank you very much for taking your time.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. loop will never advance (i will always be 0)

Of course it will increment, it is a standard loop. You just implemented the code incorrectly.

3. MainPanel already has a List of SubPanels.


I know, I'm the one that suggested you create the getSubPanels() method. But every time you post code you try to access the component from the Container, so I just gave up and did it your way.

2. it is blindly casting the contents of main without actually knowing what's on it


Agreed, because you keep posting code that wants to access the components of the Container (instead of using the getSubPanel() method as I suggested) so I assumed you only ever added SubPanels to that container.

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it work. But in output I am getting repeated values of jTextField, for example:
1) added: 1st jTextField; typed: "1"; saved, output: 1
2) added: 2nd jTextField; typed: "2"; saved, output: 1, 1, 2
3) added: 3rd jTextField; typed: "3"; saved, output: 1, 1, 2, 1,2,3

The output is repeating jTextField values.

Is it possible to get just: 1, 2, 3?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every time you invoke your "Saved" method you need to reset the "saved" string to null.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your previous answer:

3) create a SubPanel and add the panel to the main panel
4) repeat step 3.

I didn't understand section 4), why necessary create 3 SubPanel, because it was possible just add SubPanels via the addSubPanel method

and reset the string means:
?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because it was possible just add SubPanels via the addSubPanel method


Exactly. Your requirement is to add sub panels to the main panel. So I assume you have a button that the use clicks to add a sub panel. When the user clicks the button you invoke the "addSubPanel(..)" method.

and reset the string means:


Again, I don't know you exact logic, but I assume you have a "Save" button. When the user clicks the button you set your output string variable to null. Then you invoke your loop to get the text from every sub panel.

Or maybe you are saving the text to a file, in which case you need to write the data to an empty file, not append the data to the existing file.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
string set to null, then invoke loop ... it is all becoming more complicated, because I need to get text from 50 Text Fields and more.
maybe possible to find easiest way, for example write a getter which returns the list of SubPanels and by using it extract the details, am I right?
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example write a getter which returns the list of SubPanels and by using it extract the details, am I right?


Yes of course. You need a loop somewhere to get the text. Again I don't understand the format of the text. If you have 10 sub panels. Do you want an Array to contain the text of each panel. Do you want a String with each text concatenated together? Only you know the requirement.

So yes I would add a method to your MainPanel That method will be responsible for looping all the sub panels and getting the text of each individual panels. It would then format the text and return the result.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to access values of SubPanel by creating method to MainPanel:


and ActionListener of SaveButton:


But in output I am again getting in this form:
1) added: 1st jTextField; typed: "1"; saved; output: 1
2) added: 2nd jTextField; typed: "2"; saved; output: 1, 1, 2
........

I think somewhere I am missing something.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved my problem in another way.
Thank you Rob for your patience.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!