如何使用一个 ListSelectionEvent 处理多个 jList
我的类 frmMain
中有三个 jList。我创建了一个名为 ListActions
的类。下面的代码适用于一个 jList。它返回一个 jList 的单击值。
我如何区分其他三个jList?或者我需要为每个侦听器创建一个单独的类?
我需要根据单击的 jList 执行操作。我尝试查看是否可以访问所单击的 jList 的变量名称,但找不到执行此操作的方法...
class ListActions implements ListSelectionListener {
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
JList list = (JList) evt.getSource();
int iSelectedDatabase = list.getSelectedIndex();
Object objSelectedDatabase = list.getModel().getElementAt(iSelectedDatabase);
String sSelectedDatabase = objSelectedDatabase.toString();
JOptionPane.showConfirmDialog(null, sSelectedDatabase);
}
}
}
谢谢, - 杰森
I have three jLists in my class frmMain
. I have created a class called ListActions
. The code below is working for one jList. It returns the value clicked for one jList.
How do I distinguish between the three other jList? Or do I need to create a seperate class for each listener?
I need to perform an action based on which jList was clicked. I attempted to see if I could access the variable name of the jList that was clicked, but couldn't find a way to do this...
class ListActions implements ListSelectionListener {
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
JList list = (JList) evt.getSource();
int iSelectedDatabase = list.getSelectedIndex();
Object objSelectedDatabase = list.getModel().getElementAt(iSelectedDatabase);
String sSelectedDatabase = objSelectedDatabase.toString();
JOptionPane.showConfirmDialog(null, sSelectedDatabase);
}
}
}
Thanks,
- Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JList 继承自 组件。
因此,您可以使用 getName () 方法来获取您的
Component
的名称并知道调用了哪一个。JList inherits from Component.
Therefore, you can use the getName() method to get the name of your
Component
and know which one has been called.