简体   繁体   English

Java从另一个类更新表视图单元格值

[英]Java updating Table view cell values from another class

I am trying to change one of my columns and its cell values from another class however i keep getting a null pointer exception when java try's to execute that line OverviewController.getOverviewController.returnStatusColumn.setCellValueFactory( cellData -> cellData.getValue().getStatusProperty()); 我正在尝试从另一类更改我的一列及其单元格值,但是当Java尝试执行该行时,我一直收到空指针异常OverviewController.getOverviewController.returnStatusColumn.setCellValueFactory( cellData -> cellData.getValue().getStatusProperty()); , I have removed all irreverent code ,我已删除所有不正确的代码

TableView Class: TableView类别:

@FXML 
private TableView<Task> taskTable;
@FXML
private TableColumn<Task, String> statusColumn;

private final static OverviewController controller = new OverviewController 
();


public static OverviewController getOverviewController() {
    return controller;
}


public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;
    taskTable.setItems(mainApp.getTaskData());
    taskTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
    taskTable.setPlaceholder(new Label(""));
}

public TableView<taskTable> returnTasks() {
    return taskTable;
}

public TableColumn<taskTable, String> returnStatusColumn() {
    return statusColumn;
}

@Override
public void initialize(URL location, ResourceBundle resources) {
        statusColumn.setCellValueFactory(cellData -> 
cellData.getValue().getStatusProperty());
}

@FXML
public void createTask(ActionEvent event) throws InterruptedException, 
IOException, ParseException {
    thread = new MyThread();
    thread.main(null);

    statusColumn.setCellValueFactory(cellData -> 
cellData.getValue().getStatusRunningProperty());
    statusColumn.setStyle("-fx-text-fill: green; -fx-font-weight: bold;");
    taskTable.refresh();
}

@FXML
public void stopTasks() {
    statusColumn.setCellValueFactory(cellData -> 
cellData.getValue().getStatusProperty());
    statusColumn.setStyle("-fx-text-fill: red; -fx-font-weight: bold;");
    taskTable.refresh();
}

This class works fine when i want to update the table columns, if i click stop tasks method (which is linked to a button) the status column gets updated to the stop label which i want to do, same with start tasks method. 当我想更新表列时,该类工作正常,如果我单击停止任务方法(链接到按钮),则状态列将更新为我想做的停止标签,与开始任务方法相同。

Random class where i want to update the Table view status column: 我要在其中更新表视图状态列的随机类:

public class UpdateTable {
public static void main(String[] args) {
    OverviewController.getOverviewController.returnStatusColumn.setCellValueFactory(
    cellData -> cellData.getValue().getStatusProperty());
    OverviewController.getOverviewController().returnTasks().refresh();
    }

}

TableView Data: TableView数据:

//Status Information
private final SimpleStringProperty status;
private final SimpleStringProperty statusRunning;


public Task() {
    this(null, null);
}

    public Task() {
        this.statusRunning = new SimpleStringProperty("Running");
        this.status= new SimpleStringProperty("Stop");
    }

    public StringProperty getStatusProperty( ) {
        return status;
    }

    public StringProperty getStatusRunningProperty( ) {
        return statusRunning;
    }

}

If i ran the random class it will lead to a null pointer exception in particular this line: OverviewController.getOverviewController().returnStatusColumn().setCellValueFactory(cellData -> cellData.getValue().getStatusProperty()); 如果我运行随机类,则会导致出现空指针异常,尤其是以下行: OverviewController.getOverviewController().returnStatusColumn().setCellValueFactory(cellData -> cellData.getValue().getStatusProperty());

Have i done this completely the wrong way? 我做错了吗? I just want to be able to update the Table view column cells from a different class. 我只希望能够从其他类更新Table View列单元格。

Yes, you're doing this the wrong way. 是的,您这样做的方式是错误的。

You don't use a Application subclass anywhere which needs to be used as entry point of your application (assuming you're not using JFXPanel or Platform.startup ). 您无需在需要用作应用程序入口点的任何地方使用Application子类(假设您未使用JFXPanelPlatform.startup )。 Furthermore you access the column as first statement in your program which means there's no way the statusColumn field is initialized. 此外,您将列作为程序中的第一条语句进行访问,这意味着无法初始化statusColumn字段。

Also usually there shouldn't be a need to involve any class but the controller class for initializing the cellValueFactory . 通常,除了用于初始化cellValueFactory的控制器类之外,通常无需涉及任何类。 Especially using static fields is a bad approach: 特别是使用static字段是一种不好的方法:

Assuming you specify the controller class in the fxml, FXMLLoader creates a new instance of the controller class. 假设您在fxml中指定了控制器类,则FXMLLoader FXMLLoader创建控制器类的新实例。 This instance is different from the instance stored in the controller field so even when using 该实例与存储在controller字段中的实例不同,因此即使使用

OverviewController.getOverviewController.returnStatusColumn.setCellValueFactory(
    cellData -> cellData.getValue().getStatusProperty());

after loading the fxml you wouldn't get the instance you need. 加载fxml之后,您将无法获得所需的实例。

Instead I recommend using the initialize method of the controller class for these kind of initialisations. 相反,我建议对此类initialize使用控制器类的initialize方法。 It's invoked by FXMLLoader after creating and injecting all the objects specified in the fxml. 创建并注入fxml中指定的所有对象后, FXMLLoader调用它。

public class OverviewController {

    ...

    @FXML
    private TableColumn<Task, String> statusColumn;

    ...

    @FXML
    private void initialize() {
        statusColumn.setCellValueFactory(cellData -> cellData.getValue().getStatusProperty());
    }

}

If you do need to pass some info from an class that is not the controller, refer to the answers here Passing Parameters JavaFX FXML . 如果确实需要从不是控制器的类中传递一些信息,请在此处传递答案JavaFX FXML Better approaches than using static are described in the answers. 答案中描述了比使用static方法更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM