通过 JTextField 的内容搜索数组列表 - arraylist.contains(jtextfield) [英] Search an arraylist by the contents of a JTextField - arraylist.contains(jtextfield)

查看:83
本文介绍了通过 JTextField 的内容搜索数组列表 - arraylist.contains(jtextfield)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在尝试编写一个系统,该系统的重点是将数据输入到列表和数组列表中,并实现搜索和排序功能.

Currently, I'm trying to write a system, which is focused around inputting data into lists and arraylists, and implementing search and sort functionality.

目前在我的系统上,我有:

Currently on my system, I have:

一个存储数据的类从数据存储类中的对象获取数据的数组列表类.最后,一个摆动 GUI 类,其中包含一个显示 Arraylist 的 JList.

A class to store data An arraylist class which gets data from an object within the data storage class. And finally, a swing GUI class, which contains a JList which displays the Arraylist.

我想要做的是使用 JButton actionlistener 搜索 arraylist,然后将搜索结果输出到 JList.

What i'm trying to do is search through the arraylist with a JButton actionlistener, and then output the results on the search to the JList.

JButton 将获取 JTextField 的内容,并检查字符串是否存在于 ArrayList 中.

The JButton would take the contents of a JTextField, and check if the string is present in the ArrayList.

我的问题首先是,我将如何在 arraylist 类中创建搜索函数,并在单独的类中调用 JTextField 的内容?

My question first of all is, how would I go about creating a search function in the arraylist class, and call the contents of a JTextField in a seperate class?

其次,在调用数组列表上的 .contains 方法之前,是否需要将 jtextfield 转换为字符串?

Secondly, would I need to convert the jtextfield to a string before I could call a .contains method on the arraylist?

第三,一旦实现了搜索功能,如果搜索的文本存在,我将如何从arraylist中选择一条记录

and thirdly, once the search function is implemented, how would I go about selecting a record from the arraylist if the text searched for is present

这是我的数据存储类:http://pastebin.com/hwyD8r1j

我的数组列表类:http://pastebin.com/d3ftLsJb

我不希望你们为我写它,虽然那会很好,哈哈.

I'm not expecting you guys to write it for me, although that would be nice, haha.

但是任何关于我如何将这个功能实现到我的数组列表中的指针或见解将不胜感激,

But any pointers or insight on how I could go about implementing this functionality into my arraylist would be appreciated,

哦,如果你需要我发布我的 GUI 课程,尽管问.

Oh and if you need me to post my GUI class, just ask.

推荐答案

在 JTextField 上调用 'getText' 以获取他们输入的字符串.您基本上会执行以下操作.

Call 'getText' on the JTextField to get the string that they've entered. You'll basically do something like the following.

// Somehow you've initialized your array list
List<String> data = ...;

// within your action listener - invoked when the button is clicked.  You'll need to
// make sure the textField is "final"
String selected = textfield.getText();
// Linear search through your strings for one matching your text
for (String datum : data) {
  if (selected.contains(datum)) {
    // do whatever you want here; you found a match
  }
}

这篇关于通过 JTextField 的内容搜索数组列表 - arraylist.contains(jtextfield)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆