【问题标题】:JavaFX do nothing when tab is clicked单击选项卡时JavaFX什么都不做
【发布时间】:2019-11-13 09:50:35
【问题描述】:

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

我该怎么做?提前致谢。

【问题讨论】:

  • 你能分享你的代码吗?
  • @Greedo 到目前为止我什么都没有,因为我什至不知道如何开始治疗。
  • 为什么要迷惑用户?无论如何,这不受支持,您可能需要自定义皮肤来更改它处理鼠标事件的方式
  • @kleopatra 如果他在移动到下一个窗格之前​​尝试构建工作流程并验证信息可能不会令人困惑,那么单独构建此功能可能会更容易,而且它也非常易于管理

标签: java events javafx tabs


【解决方案1】:

您可以使用事件过滤器来阻止您不希望选项卡标题区域接收的事件。以下代码阻止了负责更改单击时选项卡的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();
}

【讨论】:

    【解决方案2】:

    如果您想要这种类型的流程,您可以通过隐藏选项卡来实现此目的,以便无法单击它们,然后添加按钮以遍历每个选项卡

    您将需要下面的行来隐藏选项卡

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

    同样提醒您,您可以通过像 tabPane.getSelectionModel().select(index); 这样传递索引或像 tabPane.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));
        }
    }
    

    【讨论】:

    • 玩猜谜游戏,了解 OP 的真正目标 :)
    • @kleopatra 如果您阅读了他的帖子,这是他要求的确切功能这里没有游戏他说“我想手动更改标签,而不是通过用户点击标签”
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    相关资源
    最近更新 更多