Java Swing: JButton creates new JTextField(s)

自作多情 提交于 2019-12-08 21:03:34
Jens Erat

As you're already storing references to your text fields, just use this array to query the text of the text fields:

tfData[counter-1].getText();

will show you the text of the last added text field.

But you really should initialise your array before, otherwise you won't be able to add any items to it. I think that was your main problem as you commented out your adding-code.

// think about how many text fields you will need (here: 16)
private JTextField tfData[] = new tfData[16];

If you're using arrays, watch for not breaking over its bounds. But better use a list as proposed in the comments before as it grows dynamically and you won't have to deal with array bounds and can even skip counting (the list does that for you, too).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!