Search code examples
fantom

Fantom fwt Combo widget seems to fire modify event on construction


I have written this Fantom class

using gfx
using fwt

class Test {

    Window window := Window {
        size = Size( 400, 320 )
        SashPane {
            Combo {
                items = Month.vals
                onModify.add( |e| { echo( "items.size is ${e->widget->items->size}" ) } )
            },
        },
    }

    Void main() {
        window.open
    }
}

when I run it, it produces this output:

items.size is 12
items.size is 12

which means that the modify event is being triggered twice. It occurs at the same time the window pops up on the screen, without me having any chance to modify anything on the Combo widget. Why?

This is causing issues in a real class that uses multiple Combo widgets, some of them related and causing a cascade of events that produces unexpected results.

Is there any way this can be prevented, please?


Solution

  • I can confirm that this is an issue.

    Looking at the Java source code for the FWT Combo, it's pretty small and doesn't seem to do anything wrong, which leads me to believe that it's a issue with the SWT Combo Widget.

    That doesn't help you any, so I had a quick play with the example and found this work around...

    ...add the onModify event listener after the window has been opened, and the widgets constructed. Do this by using the Window.onOpen() event:

    using gfx
    using fwt
    
    class Testy {
        Void main() {
            Window {
                size = Size( 400, 320 )
                combo := null as Combo
                onOpen.add {
                    combo.onModify.add { echo("Hello Mum!") }
                }
                SashPane {
                    combo = Combo { items = Month.vals },
                },
            }.open
        }
    }
    

    Now you should only get a Hello Mum! when the combo is actually modified.