单击选项卡时,JavaFX不执行任何操作
在我的选项卡窗格中单击选项卡时,我想取消选项卡更改。我想手动更改选项卡,而不是通过用户单击选项卡单击选项卡时,JavaFX不执行任何操作,java,events,javafx,tabs,Java,Events,Javafx,Tabs,在我的选项卡窗格中单击选项卡时,我想取消选项卡更改。我想手动更改选项卡,而不是通过用户单击选项卡 我该怎么做?提前感谢。如果您想要这种类型的流,您可以通过隐藏选项卡来实现,这样就不能单击它们,然后添加按钮来遍历每个选项卡 您需要在下面的行中隐藏选项卡 tabPane.setStyle("-fx-tab-max-height: -2;");//You can still see tabs at -1 not sure why 注意如果你想隐藏标题空间(如下面我的应用程序所示),你需要通过css来
我该怎么做?提前感谢。如果您想要这种类型的流,您可以通过隐藏选项卡来实现,这样就不能单击它们,然后添加按钮来遍历每个选项卡 您需要在下面的行中隐藏选项卡
tabPane.setStyle("-fx-tab-max-height: -2;");//You can still see tabs at -1 not sure why
注意如果你想隐藏标题空间(如下面我的应用程序所示),你需要通过css来隐藏,你可以忽略上面的一行
.tab-pane {
-fx-tab-max-height: 0 ;
}
.tab-pane .tab-header-area {
visibility: hidden ;
}
您还需要了解按钮是如何设置的,以进入下一个选项卡和上一个选项卡,因为我不确定您的应用程序会是什么样子,我只是用按钮来完成的,但您可以使用相同的操作来移动选项卡
Button buttonPlus = new Button("+");
buttonPlus.setOnAction(event -> {
int selectedIndex = tabPane.getSelectionModel().getSelectedIndex();
if(selectedIndex<tabPane.getTabs().size()-1)
tabPane.getSelectionModel().select(++selectedIndex);
});
Button buttonMinus = new Button("-");
buttonMinus.setOnAction(event -> {
int selectedIndex = tabPane.getSelectionModel().getSelectedIndex();
if(selectedIndex>0)
tabPane.getSelectionModel().select(--selectedIndex);
});
您可以使用事件筛选器阻止不希望选项卡标题区域接收的事件。以下代码阻止了负责在单击时更改选项卡的鼠标按下事件。内容区域内的任何单击都不会被阻止
@Override
public void start(Stage stage) throws IOException {
Button btn = new Button("Click");
TabPane tp = new TabPane(new Tab("tab1", new StackPane(btn)), new Tab("tab2"));
btn.setOnAction(event-> {
tp.getSelectionModel().select(1); // change tab programmatically
});
tp.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
Node n = event.getPickResult().getIntersectedNode();
boolean outsideContentArea = true;
// iterate from node actually clicked to the TabPane
// and look for the content area
while (outsideContentArea && (n != tp)) {
if (n.getStyleClass().contains("tab-content-area")) {
outsideContentArea = false;
}
n = n.getParent();
}
if (outsideContentArea) {
// stop event propagation to any part
// of the TabPane outside the content area
event.consume();
}
});
Scene scene = new Scene(tp, 300, 300);
stage.setScene(scene);
stage.show();
}
你能分享你的代码吗?@Greedo我到目前为止什么都没有,因为我甚至不知道如何开始处理这个问题。你为什么要混淆你的用户?无论如何,这是不被支持的,您可能需要一个自定义皮肤来更改它处理鼠标的方式events@kleopatra如果他试图在移动到下一个窗格之前构建工作流并验证信息,这可能不会让人感到困惑。这可能比单独构建此功能更容易,而且也非常容易管理。也许向导会更好而不是标签?请看关于OP真正想要什么的猜谜游戏:)@kleopatra如果你读了他的帖子,这正是他要求的功能这里没有游戏他说“我想手动更改标签,而不是通过用户点击标签”
@Override
public void start(Stage stage) throws IOException {
Button btn = new Button("Click");
TabPane tp = new TabPane(new Tab("tab1", new StackPane(btn)), new Tab("tab2"));
btn.setOnAction(event-> {
tp.getSelectionModel().select(1); // change tab programmatically
});
tp.addEventFilter(MouseEvent.MOUSE_PRESSED, event -> {
Node n = event.getPickResult().getIntersectedNode();
boolean outsideContentArea = true;
// iterate from node actually clicked to the TabPane
// and look for the content area
while (outsideContentArea && (n != tp)) {
if (n.getStyleClass().contains("tab-content-area")) {
outsideContentArea = false;
}
n = n.getParent();
}
if (outsideContentArea) {
// stop event propagation to any part
// of the TabPane outside the content area
event.consume();
}
});
Scene scene = new Scene(tp, 300, 300);
stage.setScene(scene);
stage.show();
}