Hello Javafx geeks,

Today I’m wondering where is the blockinMouse property in Javafx 2.0?  Is it on developing progress ? Or it’s just disappear . According to javafx bug reporting site I’ve found that one guy was playing with blockinMouse using JavaFX 2.0 which as posted on Jan 2011.

In source code I’ve found that guy was doing some stuffs with rectangle Node I’ll show small snippet code :

final Rectangle nodeAbove = new Rectangle(50, 50, 100, 100);

nodeBelow.setFill(Color.RED);
nodeBelow.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {

	public void handle(MouseEvent t) {
		System.out.println("RED clicked");
	}
});

nodeAbove.setFill(Color.GREEN);
nodeAbove.setBlocksMouse(false);


Now the surprising comes here. How did he get access to setBlocksMouse(boolean) why not us?

I finally precise to make a small test on how to use blockinMouse property in Javafx 2.0 which seems little bit crazy right! I managed to use test these things on two Circle Nodes. Every Circle Node will be made Draggable so as to get the things more clearer. Ok now the code goes here.

  • DraggableNode (Please get this class from here)
  • BlocksMouseNode
/**
 /**
 *
 * @author Narayan
 */
public class BlocksMouseNode extends Parent{
    private boolean blocksMouse;
    private static final int CLICKED=1,
            PRESSED=2,RELEASED=3,MOVED=4,
            DRAGGED=5,ENTERED=6,EXITED=7;
    
        @Override
        public ObservableList getChildren(){
            return super.getChildren();
        }

    
    // The method for registering the MouseEvent to the Node which 
    // is just below this node
    public void register(ObjectProperty<EventHandler<? super MouseEvent>> property,
            final int i, final boolean blocks){
        property.set(new EventHandler<MouseEvent>(){
            @Override
            public void handle(MouseEvent event) {
                if(blocks){
                    setMouseTransparent(true);   
                    Node n = getScene().getRoot().pickNode(event.getSceneX(),event.getSceneY());
                    if(n !=null){
                        switch(i){
                                case CLICKED: if(n.getOnMouseClicked()!=null)
                                                n.getOnMouseClicked().handle(event);
                                              break;
                                case PRESSED: if(n.getOnMousePressed()!=null)
                                                n.getOnMousePressed().handle(event);
                                              break;
                                case RELEASED: if(n.getOnMouseReleased()!=null)
                                                n.getOnMouseReleased().handle(event); 
                                               break;
                                case MOVED: if(n.getOnMouseMoved() !=null)
                                             n.getOnMouseMoved().handle(event);
                                            break;
                                case DRAGGED: if(n.getOnMouseDragged()!=null)
                                                n.getOnMouseDragged().handle(event);
                                              break;
                                case ENTERED: if(n.getOnMouseEntered()!=null)
                                                n.getOnMouseEntered().handle(event);
                                              break;
                                case EXITED:if(n.getOnMouseExited()!=null)
                                                n.getOnMouseExited().handle(event);
                                             break;
                        }
                        
                        if(n.getOnMouseEntered()!=null)
                            n.getOnMouseEntered().handle(event);
                    }                
                    setMouseTransparent(false);   
                }
            }
        });
    }
    
   

    //Registering All the MouseEvents
    private void registerMouseEvents(boolean block){        
          register(onMouseClickedProperty(),CLICKED,block);
          register(onMouseMovedProperty(),MOVED,block);
          register(onMouseReleasedProperty(),RELEASED,block);
          register(onMousePressedProperty(),PRESSED,block);
          register(onMouseDraggedProperty(),DRAGGED,block);
          register(onMouseEnteredProperty(),ENTERED,block);
          register(onMouseExitedProperty(),EXITED,block);
    }
    //-----------------
    //ACCESSOR METHODS
    //-----------------
    public void setBlocksMouse(boolean block){
        blocksMouse = block;                
        registerMouseEvents(blocksMouse);     
    }   
    
    public boolean getBlocksMouse(){
        return blocksMouse;
    }
}

The above BlocksMouseNode seems very mess. You can minimize it if you have any good way to. Let’s try this node for our realworld application. A sample example of using this customized Node .

/**
 *
 * @author Narayan
 */
public class BlocksMouseTest extends Application{

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group g = new Group();       
        final Scene scene = new Scene(g,500,500);
        
        //Creating Two Circles
        final Circle top = new Circle();
        Circle bottom = new Circle();
        
        //Some attributes setting
        top.setRadius(20);        
        top.setCenterX(200);
        top.setCenterY(200);
        top.setFill(Color.YELLOW);
        
        bottom.setRadius(30);        
        bottom.setCenterX(200);
        bottom.setCenterY(200);        
        bottom.setFill(Color.BLUE);
        
        
        //Some mouse events stuffs
        top.setOnMousePressed(new EventHandler(){ 
            public void handle(MouseEvent event) {  
                System.out.println("TOP Pressed" );               
            }
            
        }); 
        bottom.setOnMouseEntered(new EventHandler(){

            public void handle(MouseEvent event) {
                System.out.println("BOTTOM Entered");
            }
            
        });
        bottom.setOnMousePressed(new EventHandler(){

            public void handle(MouseEvent event) {
                System.out.println("BOTTOM Pressed" );
            }
            
        });
        
        //Keeping Circle inside BlocksMouseNode
        BlocksMouseNode blockMouseNode = new BlocksMouseNode();
        blockMouseNode.setBlocksMouse(true);
        blockMouseNode.getChildren().addAll(top);
        
        //Again making those nodes Draggable
        DraggableNode bottomPane = new DraggableNode();
        bottomPane.getChildren().addAll(bottom);
       
        DraggableNode topPane = new DraggableNode();
        topPane.getChildren().addAll(blockMouseNode);
        
        //finally added to root
        g.getChildren().addAll(bottomPane,topPane);
        
        primaryStage.setScene(scene);
        primaryStage.setVisible(true);
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

Project Source Code : BlocksMouse Project

I can hardly show the output of this project so please try with your self.
Thanks For Watching this blog and utilizing your time. Please leave comment if you have any thing to say.
Have a :) good day.