有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JavaFX:加载文件后更改布局

以下问题:

我有一个带有TableView的GUI来显示一些数据

My GUI

为了避免在将新文件加载到表中时出现一些问题,我调用了一个方法来清除表中的所有数据(此事件发生在我从FileChooser中选择一个文件之后,数据显示之前)

问题是,加载文件后,布局会发生变化

After loading a file

我发现发生这种情况是因为我正在清除TableView属性,但我不知道如何解决这个问题,也找不到解决方案

可运行示例

public class GuiLayout extends Application {

    private Scene mainScene;
    private Stage mainStage;
    private Label filename;
    private VBox mainBox;

    private TableView tableView = new TableView();

    private FileChooser fileChooser;
    private Button save;
    private Button neu;
    private Button settings;
    private Button table;
    private Button row;
    private Button column;
    private Button date;

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        initButton();

        mainStage = new Stage();
        filename = new Label("Nr. 100 - Test Data (Applikation: TEST)");
        mainScene = new Scene(mainVBox(), 1200, 600);

        tableView.prefWidthProperty().bind(mainBox.widthProperty());
        tableView.prefHeightProperty().bind(mainBox.heightProperty());

        mainStage.setScene(mainScene);
        mainStage.show();
    }

    private GridPane mainGrid() {

        GridPane gridPane = new GridPane();
        gridPane.setVgap(10);
        gridPane.setHgap(10);

        gridPane.add(filename, 0, 0);
        gridPane.add(buttonBox(), 0, 1);
        gridPane.add(tableView, 0, 2);

        return gridPane;
    }

    private VBox buttonBox() {

        VBox buttonBox = new VBox();

        HBox firstRowBox = new HBox();
        HBox secRowBox = new HBox();

        firstRowBox.getChildren().addAll(fileChooserFile(), save, neu, settings);
        firstRowBox.setSpacing(5);
        secRowBox.getChildren().addAll(table, row, column, date);
        secRowBox.setSpacing(5);

        buttonBox.getChildren().addAll(firstRowBox, secRowBox);
        buttonBox.setSpacing(5);
        buttonBox.prefWidthProperty().bind(mainBox.widthProperty());

        return buttonBox;
    }

    private VBox mainVBox() {

        mainBox = new VBox();

        mainBox.prefWidthProperty().bind(mainStage.widthProperty().multiply(0.8));

        mainBox.setPadding(new Insets(10, 10, 10 ,10));
        mainBox.getChildren().add(mainGrid());

        return mainBox;
    }

    private void initButton() {

        fileChooser = new FileChooser();

        save = new Button("Save");
        save.setPrefWidth(100);
        neu = new Button("New");
        neu.setPrefWidth(100);
        settings = new Button("Settings");
        settings.setPrefWidth(100);
        table = new Button("Table");
        table.setPrefWidth(100);
        row = new Button("Row");
        row.setPrefWidth(100);
        column = new Button("Column");
        column.setPrefWidth(100);
        date = new Button("Date");
        date.setPrefWidth(100);
    }

    private Node fileChooserFile() {

        FileChooser.ExtensionFilter extFilter = new ExtensionFilter("Text File (*.txt)", "*.txt");
        fileChooser.getExtensionFilters().add(extFilter);

        final Button open = new Button("Open");
        open.setPrefWidth(100);
        open.setOnAction(ae -> openFileAction());

        return open;
    }

    private void openFileAction() {

        File input = fileChooser.showOpenDialog(mainStage);

        if (input != null) {

            try {

                String path = input.getParent();
                String fileName = input.getName();
                fileChooser.setInitialDirectory(new File(path));

                clearData();

            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void clearData() {

        tableView.getItems().clear();
        tableView.getColumns().clear();
        tableView.getStylesheets().clear();
        tableView.getProperties().clear();
        tableView.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
        tableView.getSortOrder().clear();
    }
}

谢谢你的帮助


共 (0) 个答案