2014-06-21 90 views
0

我有測試按鈕來TabPane調用該代碼:呼叫事件處理

Button bt1 = new Button("Select"); 

     bt1.setOnAction(new EventHandler<ActionEvent>() 
     { 
      @Override 
      public void handle(final ActionEvent event) 
      { 
       TreeClass.getConnectedAgentsMap(); 

       TreePane.getTreeView().getSelectionModel().clearAndSelect(3); 

      } 
     }); 

這個代碼選擇樹節點到TreeView控件:

cell.setOnMouseClicked((MouseEvent me) -> 
       { 
        if (!cell.isEmpty()) 
        { 
         /// some action 
        } 
       }); 

正如你可以看到這個事件被觸發時鼠標選擇樹行。 我試着打電話給這個代碼樹細胞動作:

cell.selectedProperty().addListener(new ChangeListener<Boolean>() 
       { 
        @Override 
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) 
        { 
         /// Some Action 

        } 
       }); 

但它不使用它,因爲幾次新標籤打開的正確方法。當我點擊按鈕來調用行動事件時有沒有辦法?

+0

請你能解釋一下你想做什麼,你想在沒有的情況下點擊事件嗎? – Cobbles

+0

我想在樹上點擊鼠標點擊事件。首先,我想選擇我想模擬鼠標點擊的三個節點。 –

+0

http://stackoverflow.com/questions/24258995/how-to-programmatically-simulate-arrow-key-presses-in-java-fx/24259248#24259248 – Cobbles

回答

0
final Point2D windowCoord = new Point2D(SCENE.getWindow().getX(), SCENE.getWindow().getY()); 
final Point2D sceneCoord = new Point2D(SCENE.getX(), Main.getStage().getScene().getY()); 
final Point2D nodeCoord = MYCONTROL.localToScene(0.0, 0.0); 

final double x = Math.round(windowCoord.getX() + sceneCoord.getX() + nodeCoord.getX()); 
final double y = Math.round(windowCoord.getY() + sceneCoord.getY() + nodeCoord.getY()); 
try { 
    Robot robot = new Robot(); 
    robot.mouseMove(new Double(x).intValue()+1, new Double(y).intValue()); 
    robot.mousePress(InputEvent.BUTTON1_MASK); 
    robot.mouseRelease(InputEvent.BUTTON1_MASK); 
} catch (AWTException ex) { 
    ex.printStackTrace(); 
} 

一些代碼是從this website借來的。