简体   繁体   中英

mouse and keyboard listeners in Swing

This listener works 95% of the time:

    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:

works for me in all cases, sure required to test if is selectedItem, Index or Row greater than -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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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