开发者

How to handle multiple jLists with one ListSelectionEvent

开发者 https://www.devze.com 2023-04-08 19:13 出处:网络
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.

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 perfor开发者_开发百科m 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号