代码之家  ›  专栏  ›  技术社区  ›  Chris

打开文件时,javafx-tab既不显示也不显示文件内容

  •  1
  • Chris  · 技术社区  · 7 年前

    我正在尝试使用javafx创建一个文本编辑器。我想能够打开一个文件从窗口进入主窗口作为一个选项卡。到目前为止,我已经创建了必要的菜单项和一个选项窗口,其中包含打开文件资源管理器以选择文件的功能。

    当用户按下“选择文件”按钮时,将打开文件资源管理器。当用户选择一个文件时,“打开文件”窗口关闭。然后,主窗口(第三个图像)将向左,但不包含包含包含文件内容的选项卡。

    执行“openfile()”函数时,不会返回错误,但不会打开选项卡。我认为这可能是试图打开“chooseFileButton.setOnAction()”函数中的选项卡的问题,但无法确认。

    如有任何建议/解释,将不胜感激。


    打开文件

    enter image description here

    打开文件(文件选择器)

    enter image description here

    输出:

    enter image description here


    public class Main extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primary) throws Exception {
        primary.setTitle("Chris' Text Editor");
    
        MenuBar menuBar = new MenuBar();
        VBox vbox = new VBox(menuBar);
    
        /* FILE MENU */
        MenuItem openFile = new MenuItem("Open...");
    
        fileMenu.getItems().add(openFile);
    
        Pane rootPane = new Pane();
    
        TextArea editorTextArea = new TextArea();
        editorTextArea.setMinHeight(1000);
        editorTextArea.setMinWidth(1000);
        editorTextArea.setVisible(false);
        rootPane.getChildren().add(editorTextArea);
    
        TabPane tabPane = new TabPane();
        tabPane.setSide(Side.TOP);
    
        openFile.setOnAction(new EventHandler<ActionEvent>() {
    
            @Override
            public void handle(ActionEvent event) {
    
                Label fileLabel = new Label();
                fileLabel.setText("No File selected...");
    
                GridPane grid = new GridPane();
                Scene contextScene = new Scene(grid, 450, 300);
    
                /* NEW WINDOW */
                Stage openFileWindow = new Stage();
                openFileWindow.setTitle("Open File");
                openFileWindow.setScene(contextScene);
    
                /* SET WINDOW MODAL */
                openFileWindow.initModality(Modality.WINDOW_MODAL);
    
                /* SET PARENT WINDOW */
                openFileWindow.initOwner(primary);
    
                /* CHOOSE FILE DIRECTORY BUTTON */
                openFileWindow.setX(primary.getX() + (primary.getX() / 2));
                openFileWindow.setY(primary.getX() + (primary.getX() / 2));
    
                openFileWindow.show();
    
                /* CHOOSE FILE BUTTON */
                Button chooseFileButton = new Button();
                chooseFileButton.setText("Choose File");
    
                chooseFileButton.setOnAction(new EventHandler<ActionEvent>() {
                    public void handle(ActionEvent event) {
                        FileChooser chooseFile = new FileChooser();
                        File selectedFile = chooseFile.showOpenDialog(openFileWindow);
    
                        if(selectedFile != null) {
                            String filePath = selectedFile.getPath();
                            fileLabel.setText(filePath);
                            String fileContent = openFile2(filePath);
    
                            /* CREATE NEW TAB */
                            Tab newTab = new Tab();
                            newTab.setContent(editorTextArea);
                            newTab.setText(filePath);
                            tabPane.getTabs().add(newTab);
    
                            editorTextArea.setVisible(true);
    
                            /* POPULATE TEXT AREA WITH FILE CONTENTS */
                            editorTextArea.appendText(fileContent);
    
                            /* FOCUS ON TAB */
                            SingleSelectionModel<Tab> selection = tabPane.getSelectionModel();
                            selection.select(newTab);
    
                            openFileWindow.close();
                        }
                    }
                });
    
                grid.setAlignment(Pos.CENTER);
                grid.setHgap(10);
                grid.setVgap(10);
    
                grid.add(chooseFileButton, 0, 0);
                grid.add(fileLabel, 0, 1);
    
            }
    
    
        });
    
        menuBar.getMenus().add(fileMenu);   
    
        Scene scene = new Scene(vbox, 1000, 750);
        primary.setScene(scene);
        primary.show();
    }
    

     public String openFile2(String filePath) {
        StringBuilder content = new StringBuilder();
    
        try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)){
            stream.forEach(s -> content.append(s).append("\n"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return content.toString();
    
     }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Chris Smith    7 年前

    你从未添加 TabPane 到现场:

    vbox.getChildren().add(tabPane);