javafx TitledPane点击事件仅用于标题

时间:2016-04-03 14:21:08

标签: javafx

如果单击TitledPane的标题,我有什么方法可以触发事件吗?

我在图表编辑器中有几个节点,目前它们是可拖动的。 但是我希望它们只在我拖动标题时才拖动,如果我单击窗格上的任何位置。

mouseClick事件似乎不适合我。 有没有人有建议?

2 个答案:

答案 0 :(得分:2)

不要在标题窗格上设置文本,而是创建标签并将其设置为标题窗格的图形。然后,您可以使用标签注册鼠标处理程序:

private TitledPane createClickableTitledPane(String text) {
    Label label = new Label(text);
    label.setOnMouseClicked(e -> System.out.println("Click on "+text));
    TitledPane titledPane = new TitledPane();
    titledPane.setGraphic(label);
    return titledPane ;
}

答案 1 :(得分:1)

StackPane titleRegion = (StackPane) titledPane.lookup(".title");
titleRegion.setOnMouseClicked(System.out::println);

编辑:

有时titledPane.lookup(".title")会返回null,这意味着CSS is not applied to the node。要解决此问题,您需要在包含applyCss()的窗格中使用layout()TitledPane

相关问题