【发布时间】:2019-08-20 07:33:16
【问题描述】:
以下问题:
我有一个带有TableView 的 GUI 来显示一些数据。
为避免在将新文件加载到表中时出现一些问题,我调用了一个方法来清除表中的所有数据(此事件发生在我从 FileChooser 中选择一个文件之后并且在数据显示之前) .
问题是,加载文件后,布局发生变化。
我发现发生这种情况是因为我正在清除 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();
}
}
感谢您的帮助!
【问题讨论】:
-
删除
tableView.getProperties().clear()方法内的clearData()行可修复错误。 -
@MayurPatel 我知道这会“解决”这个问题。但我需要
tableView.getProperties().clear()行来确保每个文件都像它应该的那样。 -
Node#getProperties()返回的地图是GridPane等布局存储特定于Node的约束的位置。如果您清除该地图,那么约束就会消失。为什么你认为你需要清除那张地图? -
@Slaw 每个文件都是独立的。行、列和宽度等的大小。为了确保文件正确加载(大小),我认为清除数据是最好的方法。
-
您是否在不清除属性映射的情况下测试了您的应用程序?不清地图真的会出问题吗?