通过搜索一个JTextField的内容,一个ArrayList - arraylist.contains(JTextField中) [英] Search an arraylist by the contents of a JTextField - arraylist.contains(jtextfield)

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

问题描述

目前,我试图写一个系统,它的重点是围绕数据输入到列表和的ArrayList,实施搜索和排序功能。

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:

一个类来存储数据
一个ArrayList类从数据存储类中的一个对象获取数据。
最后,一​​个Swing 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中present。

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?

其次,我会需要将JTextField转换为字符串之前,我可以呼吁ArrayList的一个。载的方法?

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

第三,一旦搜索功能实现,我将如何去了解,如果文本搜索从数组列表中选择一个记录是present

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

Here is my Data storage class: http://pastebin.com/hwyD8r1j

我的ArrayList类:
http://pastebin.com/d3ftLsJb

My arraylist class: http://pastebin.com/d3ftLsJb

我不期待你们把它写对我来说,虽然这将是很好的,哈哈。

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

但在我怎么可能去实现该功能到我的ArrayList是AP preciated任何指针或见解,

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 - arraylist.contains(JTextField中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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