JList的ListSelectionEvent的几个问题请教

来源:互联网 发布:strip函数python 编辑:程序博客网 时间:2024/06/18 09:43

在做一个类似qq的小东西,直接上图。


面板中心的是数据库取得的好友列表,做到 JList = friendsList 里面去,然后对这个列表加监听,addListSelectionListener,这个监听很奇怪,在点击鼠标和释放点击的时候都会响应一次,但是问题是,其中的一个被点击了,只有在不点击别的地方的情况下,再次点击的话是不响应的,请问这样的情况要怎么处理,还有这里的双击某个用户的话要怎么添加响应。有没有大神能小弟一把,感激涕零~~   要是有交流的可以直接加QQ 345451003

为了方便理解简单说明一下:

好友列表取出的时候是一个List,把这个List逐一添加到自定义的一个Row这个类中,然后用 Vector<Row> rows = new Vector<Row>() 的方法吧数据添加的JList中的

这里是自定义的一个Row

static public class Row extends JDesktopPane {public JLabel icon;public JLabel username;public JLabel name;public JLabel email;public User u;public Row(User user) {super();u = user;FlowLayout fl = new FlowLayout();fl.setAlignment(FlowLayout.LEFT);this.setSize(280, 1);this.setLayout(fl);icon = new JLabel();icon.setIcon(new ImageIcon("images/panda (2).png"));username = new JLabel(user.getUserName());name = new JLabel(user.getName());email = new JLabel(user.getEmail());add(icon);add(username);add(name);add(email);}public User getUser() {return this.u;}}


往 JList 中添加数据的方法

Vector<Row> rows = new Vector<Row>();try {java.util.ArrayList<User> userList = (ArrayList<pandaChat.entity.User>) dbProcess.getAllUserslist();for (int j = 0; j < userList.size(); j++) {User u = userList.get(j);Row r = new Row(u);r.setBackground(Color.ORANGE);r.setBorder(BorderFactory.createLineBorder(Color.BLACK));rows.add(r);}} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}

这里是对 JList friendsList的监听

        friendsList.addListSelectionListener(new ListSelectionListener() {            @Override            public void valueChanged(ListSelectionEvent e) {                // TODO Auto-generated method stub                                if (e.getValueIsAdjusting()) {                    // 鼠标点击在点的时刻                                        java.util.ArrayList<Row> selecteditem = (ArrayList<Row>) friendsList.getSelectedValuesList();                    Row r1 = selecteditem.get(0);                    r1.setBackground(Color.GREEN);                    User u = r1.getUser();                    User.printUser(u);     //这里是自己定义的检查 user 的一个输出                                    } else {                    // 鼠标点击释放的时刻                    java.util.ArrayList<Row> selecteditem = (ArrayList<Row>) friendsList.getSelectedValuesList();                                        Row r2 = selecteditem.get(0);                    r2.setBackground(Color.ORANGE);                }            }        });

Row


0 0
原创粉丝点击