ValueChanged gui List executed twice

When making a selection in this simple gui list, I get a valueChanged to execute twice once when the mouse is down and once when the mouse is up.

import groovy.swing.SwingBuilder import java.awt.* import java.swing.* import javax.swing.* def main(){ new SwingBuilder().edt { frame(title:'Testing', pack:true, show:true) { vbox { panel(){ textbox = label(text:'null') } panel(){ listing = list(valueChanged:{ mess(listing.selectedValue);// this code runs twice }, listData: ['test','another','test','and','again']) } } } } } def mess(mytext){ new SwingBuilder().edt { frame(title:'Message', pack:true, show:true){ vbox { panel(){ label(text:mytext) } } } } } main(); 

I searched for other questions like this in stackoverflow, to no avail, if this is a duplicate, sorry and I will delete it, but I do not believe that it is. All I'm trying to do is make it not start with the mouse.

+4
source share
1 answer

Yes, with Swing JList you get two ValueChanged events when the user clicks on a row.

The first click will have event.valueIsAdjusting == true to indicate that the user is in the process of changing the value, and the second event will have event.valueIsAdjusting == false to show the selection (see this error report * here and in the documentation here )

Change:

  valueChanged:{ mess(listing.selectedValue);// this code runs twice } 

To:

  valueChanged:{ event -> if( !event.valueIsAdjusting ) mess(listing.selectedValue) } 

Gotta fix it ...

(* It should be noted that this is not an error, as can be seen from the closing state) :-)


change

To clear the selection, you can change main() to:

 def main() { def data = ['test','another','test','and','again'] def codeFired = false new SwingBuilder().edt { frame(title:'Testing', pack:true, show:true) { vbox { panel(){ textbox = label(text:'null') } panel(){ listing = list listData: data, valueChanged: { event -> if( !event.valueIsAdjusting && !codeFired ) { mess( listing.selectedValue ) codeFired = true listing.clearSelection() codeFired = false } } } } } } } 
+4
source

All Articles