问题
I have a TabPane with test button which calls this code:
Button bt1 = new Button("Select");
bt1.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(final ActionEvent event)
{
TreeClass.getConnectedAgentsMap();
TreePane.getTreeView().getSelectionModel().clearAndSelect(3);
}
});
This code selects TreeNode into TreeView:
cell.setOnMouseClicked((MouseEvent me) ->
{
if (!cell.isEmpty())
{
/// some action
}
});
As you can see this event is triggered when mouse selects tree row. I tried to call the tree cell action with this code:
cell.selectedProperty().addListener(new ChangeListener<Boolean>()
{
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue)
{
/// Some Action
}
});
But it's not the proper way to use it because several times new Tab is opened. Is there a way when I click the button to call action event?
回答1:
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();
}
Some of the code is borrowed from this website.
来源:https://stackoverflow.com/questions/24343898/call-event-handler