Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

API : EventHandler and ugly code

865322Aug 26 2011 — edited Sep 14 2011
Hello,

I'm playing with javafx2 api since I 've been disapointed by flex and silverlight api.

My reference is forever the swing api and I want to know how to write beautiful code which will respect all best practices (pmd,checkstyle, findbugs, javancss)

The problem is the genericsable class EventHandler ...

In swing I can have a class that implements MouseListener and ActionListener interface, So if my control implements these interfaces I can have an actionPerformed, mouseMoved .... methods which will process all actions.
public class MyClass extends JPanel implements ActionListener, MouseListener {
 
...

mybutton.addActionListener(this);
mycontrol.addMouseListener(this);

public void actionPerformed(ActionEvent e){
    if(e.getSource.equals(mybutton)){
         // mybutton stuff
    }
}

public void mouseMoved(MouseEvent e){
    if(e.getSource.equals(mycontrol)){
         // mycontrol stuff
    }
}
The JavaFX2 API use the same approach but since the eventHandler use a custom generics type, I must implements EventHandler (without type <XX>) and do a lot of cast everywhere to fill the gap.
mybutton.setOnAction((EventHandler)this);
then
public class ControlPanel extends Region implements EventHandler<Event> {

...

bt1.setOnAction((EventHandler)this);
bt2.setOnAction((EventHandler)this);
progressbar.setOnMouseClicked(this);


    public void handle(Event event) {
        
        if(MouseEvent.class.equals(event.getClass())){
            f(event.getSource().equals(progressbar)){
                 //stuff;
            }
        }else if(ActionEvent.class.equals(event.getClass())){
            if(event.getSource().equals(bt1)){
                 //stuff;
            }else if(event.getSource().equals(bt2)){
                //stuff;
            }
        }
    }
IN fact with javafx2 it's more difficult to write code not using anonymous class which will be a very big problem for software maintainability and source code quality.

Moreover the SetONXXX method which have a Runnable argument need anonymous class or dedicated inner class to manage threading priorities and I don't see any solution to make it cleaner.
It would be nicer to use a custom Runnable class which have some fields used to identify the source and the kind of event.

At the moment, a javafx2 developers have 2 options :
_ Write a lot of anonymous class anywhere in its source code, the class will be unreadable because the code is not organized (components initialization, action processes, business logic)
_ create a huge amount of dedicated class (runnable and eventhandler) which all reference the main control ...

JavaFx2 code will be more complex and can be less adopted or misloved ....

What do you think about this ?

Edited by: Seb on 27 août 2011 00:15
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 12 2011
Added on Aug 26 2011
20 comments
337 views