我想创建显示不同内容的标签作为tableview。但是,它仅显示每个选项卡中没有列和内容的表。以下是相关代码。
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle(WINDOW_TITLE);
showWindow();
}
public void showWindow() {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(UserInterface.class.getResource(MAIN_WINDOW_LAYOUT));
AnchorPane page = (AnchorPane) loader.load();
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Returns the main stage.
*
* @return primaryStage
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
创建表的表类
/**
* Creates a Task Table to display tasks
*
*/
protected TaskTable(ArrayList<Tasks> taskListToDisplay) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(LOCATION_TASK_TABLE_FXML));
loader.setController(this); // Required due to different package declaration from Main
setTable(taskListToDisplay);
indexCol.getStyleClass().add("align-center");
timeCol.getStyleClass().add("align-right");
indexCol.setCellValueFactory(new PropertyValueFactory<>("index"));
descriptionCol.setCellValueFactory(new PropertyValueFactory<>("description"));
timeCol.setCellValueFactory(new PropertyValueFactory<>("time"));
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
}
/**
* Create task table to display
*/
public ObservableList<TaskModel> getTaskList(ArrayList<Tasks> currentList) {
ObservableList<TaskModel> taskData = FXCollections.observableArrayList();
DateDisplay display = new DateDisplay();
for (int i = 0; i < currentList.size(); i++) {
Tasks currentTask = currentList.get(i);
taskData.add(new TaskModel(i + 1, currentTask.getDescription(),
display.getDurationString(currentTask)));
}
return taskData;
}
public Node getTaskTable(){
return this.taskTable;
}
public TableView getTable(){
return this.table;
}
public void setTable(ArrayList<Tasks> taskList){
table.setItems(getTaskList(taskList));
}
public void setObservableTable(ObservableList taskList) {
table.getItems().setAll(taskList);
}
和TabHandler
// -----------------------------------------
// FXML variables
// -----------------------------------------
@FXML
private TextField userCommand;
@FXML
private Label feedback;
@FXML
private Tab toDoTab;
@FXML
private Tab completedTab;
@FXML
private TabPane tabPane;
@FXML
private TaskTable toDoTaskTable = new TaskTable();
@FXML
private TaskTable completedTaskTable = new TaskTable();
// -----------------------------------------
// Class variables
// -----------------------------------------
private ArrayList<Tasks> currentToDoList = new ArrayList<Tasks>();
private ArrayList<Tasks> currentCompletedList = new ArrayList<Tasks>();
// -------------------------------------------------------
// Message String
// -------------------------------------------------------
private static final String PROMPT_USERCOMMAND_TEXT = "Enter command";
private static final String PROMPT_USERCOMMAND_CLEAR = "";
@FXML
private void initialize() throws Exception {
initGUI();
handleUserInput();
}
public void handleUserInput() {
userCommand.setOnKeyPressed(e -> {
if (e.getCode().equals(KeyCode.ENTER)) {
String userInput = userCommand.getText();
FeedbackMessage output;
try {
output = getOutputFromLogic(userInput);
currentToDoList = output.getIncompleteTaskList();
currentCompletedList = output.getCompleteTaskList();
toDoTaskTable = new TaskTable(currentToDoList);
completedTaskTable = new TaskTable(currentCompletedList);
toDoTab.setContent(toDoTaskTable.getTable());
completedTab.setContent(completedTaskTable.getTable());
feedback.setText(output.getFeedback());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
public void initGUI() {
toDoTaskTable.setTable(currentToDoList);
completedTaskTable.setTable(currentCompletedList);
toDoTab.setContent(toDoTaskTable.getTableView());
completedTab.setContent(completedTaskTable.getTableView());
}
答案 0 :(得分:0)
你不应该这样做:
@FXML
private TaskTable toDoTaskTable = new TaskTable();
有一个原因是您使用@FXML
注释字段,并且在加载FXML时,此字段将与所有其他字段一起实例化。
如果使用关键字new
重新实例化,则原始控件的引用将丢失(控件应该在FXML中存在)。在新创建的实例上调用setTable
时,您将向此对象添加数据,该数据与FXML上显示的对象无关,因此它始终显示为空。
我猜测(从您提出问题的方式,我应该猜对了)TaskTable
是一个自定义组件。如果是,则可以轻松地将自定义组件导入场景构建器。有questions and answers可以帮助您。
从上述代码中删除new TaskTable()
并修复其他问题后,您的代码就会开始运行。