简体   繁体   English

从JTextField获取输入并使用它搜索arraytable

[英]Taking input from JTextField and using it to search arraytable

I have a arraylist which take a string eg "String searchFor = "London;" and then runs runs in the loop and then shows data related to the string, if I try change the string to String searchFor = tfSearch.getText(); then I get <init> error 我有一个arraylist ,它接受一个string例如"String searchFor = "London;" ,然后在loop运行,然后显示与该字符串相关的数据,如果我尝试将字符串更改为String searchFor = tfSearch.getText();然后我收到<init>错误

I think problem is that the arraylist doesn't wait for the input to be taken from the JTextField 我认为问题是arraylist不等待从JTextField接受输入

If you read the documentation it says: 如果您阅读文档说明:

Throws:
NullPointerException - if the document is null

So, your JTextField must be null . 因此,您的JTextField必须为null

You need to execute getText() on a actionListener , for example. 例如,您需要在actionListener上执行getText() Probably, the way you're doing it now, the JTextField is null yet, and you're not waiting for an input from the user. 可能是您现在的方式, JTextFieldnull ,并且您不必等待用户的输入。 That way, when the user fills that field and clicks Enter , you can get the text from the field. 这样,当用户填写该字段并单击Enter ,您可以从该字段中获取文本。

Check out this example 看看这个例子

Add action listener to JTextField 将动作侦听器添加到JTextField

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM