Java - 从 JList 更新文本字段
我正在使用 Java 制作一个地址簿 GUI,并且有一个 JList
,它显示我的 ArrayList
中的所有人员姓名(由 updateinfo< 填充) /code> 方法如下所述)。我希望这样,当我单击
JList
上的某个项目时,TextField
就会使用该人的详细信息进行更新。之前我只使用按钮,因此使用 actionListeners
。我想我明白 JList
必须使用 ListSelectionListener
但我似乎无法实现这一点。我在下面添加了我的代码片段。有人可以帮忙吗?为了与我的 actionlisteners
保持连续性,我希望将其作为内部类,但这并不重要
JList jl;
DefaultListModel list;
list = new DefaultListModel();
this.jl = new JList(this.list);
//add ListSelectionListener????
updateList();
this.add(this.jl, layout);
I am making an address book GUI with Java and I have a JList
that displays all the names of the people in my ArrayList
(this is populated by the updateinfo
method mentioned below). I want it so when I click an item on the JList
, the TextField
s are then updated with that persons details. Before I have only used buttons and therefore actionListeners
. I think I understand that a JList
must use ListSelectionListener
but I cannot seem to implement this. I have added a snippet of my code below. Can somebody please help?? For continuity with my actionlisteners
I would like to have it as an inner class but this is not vital
JList jl;
DefaultListModel list;
list = new DefaultListModel();
this.jl = new JList(this.list);
//add ListSelectionListener????
updateList();
this.add(this.jl, layout);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以添加侦听器,然后只查询当前选定的索引。
我为你做了一个样本,希望你觉得它有用。
这是相关部分:
请记住,这不是唯一的方法,这只是您的一个起点。
这是完整的工作示例:
这是显示的内容:
示例 http://img177.imageshack .us/img177/6294/capturadepantalla200911k.png
You can add the listener and then just query the currently selected index.
I did a sample for you, I hope you find it useful.
This is the relevant section:
Bear in mind that's not the only way to go, this is just an starting point for you.
Here's the complete working sample:
This is what is displayed:
sample http://img177.imageshack.us/img177/6294/capturadepantalla200911k.png
您只需将选择侦听器添加到列表中,如下所示:
使用像这里这样的匿名类是最快的方法。虽然读起来有点困难,但这是一个典型的模式。
(刚刚读到您更喜欢内部类,但我无法在手头没有 IDE 的情况下即时编写代码...)
You just add the selection listener to the list, like that:
Using an anonymous class like here is the quickest way. It's a bit hard to read but a typical pattern.
(just read you preferred an inner class, but I can't code that here on the fly with no IDE at hand...)
是的,您将需要为此使用 ListSelectionListener,您可能还希望将列表设置为单选(ListSelectionModel.SINGLE_SELECTION)。这将允许用户仅选择列表中的一项。然后,您可以添加 listSelectionListener,并在事件的 valueChanged 中执行类似以下操作(不准确的语法)。
Yes you will want to use a ListSelectionListener for this, you will also probably want to set the list to single selection(ListSelectionModel.SINGLE_SELECTION). This will allow the user to only select one item in the list. You can then add you listSelectionListener, and in the valueChanged of the event do something like the following(not exact syntax).
好了,那么就从阅读JList API开始吧。您将找到有关“如何使用列表”的 Swing 教程的链接,其中包含一个工作示例。
另外,在本教程中,您将找到有关“如何编写列表选择侦听器”的部分,其中包含第二个示例。
从教程开始解决您的基本问题。
Well, then start by reading the JList API. You will find a link to the Swing tutorial on "How to Use Lists", which contains a working example.
Also in the tutorial you will find a section on "How to Write a List Selection Listener" which contains a second example.
Start with the tutorial for your basic questions.