Skip to content
Advertisement

How to get text from same Text Field used multiple times in Java Swing

I’m trying to get text from a JTextField iterated through my code (apparently, I can’t add a different text field from a button). Here’s what the “Add Items” button do:

JavaScript

It adds two new text fields in the panel using FlowLayout. Now, I want to get the text given by the user from text fields with each one assigned to a different variable or maybe into an ArrayList by clickin the “OK” button but the getText() method doesn’t seem to work.

JavaScript

Can’t seem to think of anything right now.

enter image description here

Advertisement

Answer

in this code when you are reinitializing tf and tfv in addButton you lost the reference to previous defined textfiels

JavaScript

so to solve this problem you need to define an ArrayList to hold reference to all defined textfields and then you can access to all of them:

JavaScript

accessing

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement