1

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?

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • if you don't find it [here](http://docs.oracle.com/javase/7/docs/api/java/util/EventListener.html), then roll **your own** wrapper class ;] – mre Jul 25 '12 at 20:50
  • Not that I'd know of, however may I suggest creating an own queue for such events, which gets checked upon timer or something? Simply put, your error handling determines, whether to process something 'right away' or 'wait a little'. Upon 'Wait a little', you would put it into a buffer of some sort, and at the end of your error handling, you check if there's something in that buffer and process the keystrokes/mouseclicks. – ATaylor Jul 25 '12 at 20:50
  • 1
    I don't think there is, I think that is the only aggregate one. I've never had trouble with it either, except that when you programatically set the list, it also fires. Maybe it would help if there was some context for your problem? – vextorspace Jul 25 '12 at 20:51
  • The actual code goes far beyond a SSCEE, so to avoid flak I just asked the abbreviated version. This listener seems to fire when another JList is clicked, which effects an Observed class, which this class listens to...etc...etc. From the responses, I'm **reasonably** satisfied that the API is doing what it's supposed to be doing, and that there's nothing out of the box to do what I want. I'll just combine a few keyboard/mouse type listeners for the time being. – Thufir Jul 25 '12 at 20:58
  • I'm [repeating myself](http://stackoverflow.com/q/10467258/262852) and re-asking the same question :( – Thufir Jul 26 '12 at 01:03

2 Answers2

3

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");
                }                    
            }
        }
    });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • not included in the question was the information that the JList isn't visible or accessible to the user when it's getting selected, and I surmise that the JList has stale data in it. Probably my design has an underlying flaw to allow that to happen. – Thufir Jul 25 '12 at 21:25
  • @Thufir so you _know_ you are doing something wrong, and try to hack around that fact by doing something wronger ;-) Better to track-down and fix the underlying error and keep your code clean ... – kleopatra Jul 26 '12 at 09:55
0

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.

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 3
    pretty sure that this is _not_ what you need: low-level listeners are always the wrong solution, most of the time they'll miss part of what you need to react to (you _are_ binding some other stuff to the selection, aren't you? That selection might have changed programmatically, or triggered by model changes, or ...) Learn to use the selectionChange notification - see @mKorbel) – kleopatra Jul 26 '12 at 09:51
  • @kleopatra is right. The 95% sounds like a race between adding the listener and other initialization. You might re-check [Initial Threads](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) as well as in-thread order. Expedience incurs a debt; you may actually save time creating an [sscce](http://sscce.org/). – trashgod Jul 28 '12 at 00:10
  • oh, pardon, that statistic isn't quite right. What I should've said was that a certain sequence of clicks would produce errors. – Thufir Jul 28 '12 at 00:25