Vargen
Vargen

Reputation: 726

Handling mouse events in javafx

Can i set a function when creating an object like i can with variables?

Given i have a container class and a CustomButton class:

function doSomething():Void{}

var button:CustomButton = CustomButton{
   posX : 50;
   posY = 100;
   onMouseClicked: doSomething;

}

Short story: i need the main container object to handle mouse events that occurs in objects placed in the containers.

Upvotes: 0

Views: 1336

Answers (2)

Eric Wendelin
Eric Wendelin

Reputation: 44369

The easiest change for you is to change:

function doSomething():Void{}

to

function doSomething(e:MouseEvent):Void{}

The action property is nice but I'm sure you want some custom rollover effect or something using onMouseEntered etc.

Upvotes: 1

Matthew Hegarty
Matthew Hegarty

Reputation: 4306

If I've understood your requirement correctly, I think it can be achieved with some syntax changes. Obviously you can extend Button if you need a custom version:

function doSomething():Void{
    println("clicked");
}

var button:Button = Button{
    text: "Click Me"
    translateX: 50;
    translateY: 100;
    action: doSomething
}
Stage {
    title : "ButtonTest"
    scene: Scene {
        width: 200
        height: 200
        content: [ button ]
    }
}

Upvotes: 1

Related Questions