单击选项卡时,JavaFX不执行任何操作



当在TabPane中单击选项卡时,我想取消选项卡更改。我想手动更改选项卡,而不是通过用户单击选项卡。

我该怎么做?提前谢谢。

您可以使用事件过滤器来阻止您不希望选项卡标题区域接收的事件。以下代码阻止了负责更改单击选项卡的MOUSE_PRESSED事件。内容区域内的任何单击都不会被阻止。

@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();
}

如果您想要这种类型的流,您可以通过隐藏选项卡以使其无法点击,然后添加按钮来迭代每个选项卡来实现这一点

您需要下面的行来隐藏标签

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);
});

同样,作为提醒,您可以通过传递像sotabPane.getSelectionModel().select(index);这样的索引或通过传递像sotabPane.getSelectionModel().select(tab);这样的特定选项卡来更改选项卡

一旦这些已经创建并添加到场景中,您将能够轻松地点击每个选项卡

查看我下面的申请,如果你有任何问题,请告诉我

public class Main extends Application {
@Override
public void start(Stage stage) {
TabPane tabPane = new TabPane();
tabPane.setStyle("-fx-tab-max-height: -2;");//You can still see tabs at -1
createNewTab(tabPane);
createNewTab(tabPane);
createNewTab(tabPane);
stage.setScene(new Scene(tabPane));
stage.show();
}
private void createNewTab(TabPane tabPane){
Tab tab = new Tab();
tab.setClosable(false);
addNodesToTab(tabPane, tab);
tabPane.getTabs().add(tab);
}
private void addNodesToTab(TabPane tabPane, Tab tab){
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);
});
tab.setContent(new VBox(new Label("Tab:"+tabPane.getTabs().size()),buttonPlus, buttonMinus));
}
}

最新更新