简体   繁体   English

Swing中的鼠标和键盘监听器

[英]mouse and keyboard listeners in Swing

This listener works 95% of the time: 这个听众有95%的时间工作:

    messagesJList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            messagesJListValueChanged(evt);
        }
    });

however, it will sometime register at an inconvenient time. 但是,它有时会在不方便的时候注册。 No doubt, my error handling is the underlying problem. 毫无疑问,我的错误处理是潜在的问题。 That being said, is there an alternative listener which aggregates the various mouse and keyboard listeners, but only those events? 话虽如此,是否有一个替代监听器聚合各种鼠标和键盘监听器,但只有那些事件?

This listener works 95% of the time: 这个听众有95%的时间工作:

works for me in all cases, sure required to test if is selectedItem, Index or Row greater than -1 (no selection) 在所有情况下都适用于我,当然需要测试selectedItem, Index or Row大于-1 (no selection)

    jList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                int selectedRow = jList.getSelectedIndex();
                if (selectedRow> -1) {
                    System.out.println("selection");
                }                    
            }
        }
    });

I just combined: 我刚刚合并:

private void messagesJListKeyReleased(java.awt.event.KeyEvent evt) {
    userSelectedRow();
}

private void messagesJListMouseReleased(java.awt.event.MouseEvent evt) {
    userSelectedRow();
}

so that only if the user actually clicks with mouse or keyboard is the method userSelectedRow() invoked. 因此,只有当用户实际使用鼠标或键盘单击时,才会调用userSelectedRow()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM