JFXTabePane关闭选项不起作用

时间:2017-01-25 03:05:57

标签: java javafx javafx-2 javafx-8

我正在尝试在tabpane中添加关闭选项,就像浏览器一样。你能告诉我如何在这个tabpane中添加关闭功能吗?我试过这一行,但没有解决我的问题tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);

我正在使用javafx jfoenix库进行用户界面。

package tabsDemo;

import java.awt.Color;
import java.awt.Dimension;
import java.math.BigInteger;
import java.security.SecureRandom;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SingleSelectionModel;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane.TabClosingPolicy;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTabPane;
import com.jfoenix.controls.JFXTextField;

public class TabsDemo extends Application {

    private String msg = "Tab 0";

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Tabs");
        Group root = new Group();
        Scene scene = new Scene(root, 800, 600);

        JFXButton b1 = new JFXButton();
        b1.setId("back");
        b1.setGraphic(new ImageView(new Image("/tabsDemo/back.png")));
        b1.setMinWidth(20);
        b1.setMinHeight(20);
        b1.setMaxWidth(20);
        b1.setMaxHeight(20);

        JFXButton b2 = new JFXButton();
        b2.setId("farword");
        b2.setGraphic(new ImageView(new Image("/tabsDemo/forward.png")));
        b2.setMinWidth(20);
        b2.setMinHeight(20);
        b2.setMaxWidth(20);
        b2.setMaxHeight(20);

        JFXButton b3 = new JFXButton();
        b3.setId("refresh");
        b3.setGraphic(new ImageView(new Image("/tabsDemo/refresh.png")));
        b3.setMinWidth(20);
        b3.setMinHeight(20);
        b3.setMaxWidth(20);
        b3.setMaxHeight(20);

        JFXTextField t1 = new JFXTextField();
        t1.setMinWidth(100);
        // t1.setPrefWidth(900);
        // t1.setMaxWidth(1000);
        t1.setMinHeight(30);
        t1.setMaxHeight(30);

        GridPane gridPane = new GridPane();
        gridPane.setHgap(4);
        gridPane.add(b1, 0, 0);
        gridPane.add(b2, 1, 0);
        gridPane.add(b3, 2, 0);
        gridPane.add(t1, 3, 0);
        gridPane.setHgrow(t1, Priority.ALWAYS);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(gridPane);

        JFXTabPane tabPane = new JFXTabPane();
        Tab tab1 = new Tab();
        tab1.setText("Tab1");
        tab1.setContent(borderPane);

        VBox vbox = new VBox();
        vbox.getChildren().addAll(new JFXButton("B1"), new JFXButton("B2"), new JFXButton("B3"), new JFXButton("B4"));
        Tab tab2 = new Tab();
        tab2.setText("Tab2");
        tab2.setContent(vbox);

        tabPane.getTabs().addAll(tab1, tab2);
        tabPane.setPrefSize(800, 600);

        // I add here the closing option for tab but it's not working
        tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);

        SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel();
        selectionModel.select(1);

        JFXButton button = new JFXButton("Add New Tab");

        button.setOnMouseClicked((o) -> {
            Tab temp = new Tab();
            int count = tabPane.getTabs().size();
            temp.setText(msg + count);
            temp.setContent(new Label("Tab 0" + count));
            tabPane.getTabs().add(temp);
        });

        borderPane.setRight(button);

        tabPane.setMaxSize(800, 600);

        /*
         * HBox hbox = new HBox(); hbox.getChildren().addAll(button, tabPane);
         * hbox.setSpacing(50); hbox.setAlignment(Pos.CENTER);
         * hbox.setStyle("-fx-padding:20");
         */
        BorderPane rootBorderpane = new BorderPane();
        rootBorderpane.setCenter(tabPane);

        root.getChildren().addAll(rootBorderpane);
        scene.getStylesheets().add(TabsDemo.class.getResource("jfoenix-components.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setTitle("JFX Tabs Demo");
    }

    private SecureRandom random = new SecureRandom();

    public String nextSessionId() {
        return new BigInteger(50, random).toString(16);
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

您的JFXTabPane是JFoenix库中的自定义组件,因此无法保证所有标准JavaFX设置(例如您的TabClosingPolicy)都已完全实现。

我检查了他们的GitHub存储库,看起来有关于此缺失功能的unresolved issue。有人分叉了存储库,显然解决了问题并提交了pull request,但它仍处于打开状态。换句话说,更改不包含在当前版本中,因此错误仍然存​​在。

我的建议是坚持使用标准的JavaFX组件,它应该提供创建简单应用程序所需的一切,包括可关闭的选项卡,它们运行良好,如下例所示。

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("JFX Tabs Demo");

    TabPane tabPane = new TabPane();
    tabPane.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
    tabPane.getTabs().add(new Tab("Test"));

    Scene scene = new Scene(tabPane, 800, 600);

    primaryStage.setScene(scene);
    primaryStage.show();
}

It works