有 Java 编程相关的问题?

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

java将计算列添加到可观察列表中

我对java和JavaFX场景生成器相当陌生。从Scene builder和Java中很难找到关于如何链接/编码表单的教程。所以我最后有了一个问题

我有一个包含两列的可观察列表。列A给我整数作为字符串列B另一个整数作为字符串-我现在如何计算列C(A-B)

到目前为止,我设置了以下代码:

主控制器类

public class MainController implements Initializable {

    // Define table

    @FXML
    TableView<Table> tableID;
    @FXML
    TableColumn<Table, Integer> iA;
    @FXML
    TableColumn<Table, Integer> iB;
    @FXML
    TableColumn<Table, Integer> iC;

    // Define Form
    @FXML
    TextField AInput;
    @FXML
    TextField BInput;
    @FXML
    Button submit;


    // Define variables


    // create table data
    final ObservableList<Table> data = FXCollections.observableArrayList(
    );


    @Override
    public void initialize(URL location, ResourceBundle resources) {

        iC.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rC"));
        iA.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rA"));
        iB.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rB"));

        tableID.setItems(data); 
    }

    public void onAddItem(ActionEvent event) {
        Table entry = new Table(Integer.parseInt
                (AInput.getText()), Integer.parseInt
                (BInput.getText()));

        // insert data in table
        data.add(entry);
    }

}
}

表格类

public class Table {

    private final SimpleIntegerProperty rA;
    private final SimpleIntegerProperty rB;

    public Table(Integer sA, Integer sB) {

        this.rA = new SimpleIntegerProperty(sA);
        this.rB = new SimpleIntegerProperty(sB);
    }

    public Integer getRA() {
        return rA.get();
    }

    public void setRA(Integer v) {
        rA.set(v);
    }
    public Integer getRB() {
        return rB.get();
    }

    public void setRB(Integer v) {
        rB.set(v);
    }
}

我是否需要为c设置另一个设置模式,在这里我计算rA-rB


共 (1) 个答案

  1. # 1 楼答案

    只需向Table类添加一个getter方法,以返回用于其他列的值之间的差异

    public Integer getRC() { return getRA() - getRB(); }
    

    subtraction table

    可执行示例

    import javafx.application.Application;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.collections.F*;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.control.cell.PropertyValueFactory;
    import javafx.stage.Stage;
    
    public class SubtractingTable extends Application {
        public static class Table {
            private final SimpleIntegerProperty rA;
            private final SimpleIntegerProperty rB;
    
            public Table(Integer sA, Integer sB) {
                this.rA = new SimpleIntegerProperty(sA);
                this.rB = new SimpleIntegerProperty(sB);
            }
    
            public Integer getRA() {
                return rA.get();
            }
    
            public void setRA(Integer v) {
                rA.set(v);
            }
            public Integer getRB() {
                return rB.get();
            }
    
            public void setRB(Integer v) {
                rB.set(v);
            }
    
            public Integer getRC() { return getRA() - getRB(); }
        }
    
        @Override
        public void start(Stage stage) {
            TableView<Table> tableID = new TableView<>();
    
            TableColumn<Table, Integer> iC = new TableColumn<>("A - B");
            iC.setCellValueFactory(new PropertyValueFactory<>("rC"));
            iC.setStyle("-fx-alignment: baseline_right;");
    
            TableColumn<Table, Integer> iA = new TableColumn<>("A");
            iA.setCellValueFactory(new PropertyValueFactory<>("rA"));
            iA.setStyle("-fx-alignment: baseline_right;");
    
            TableColumn<Table, Integer> iB = new TableColumn<>("B");
            iB.setCellValueFactory(new PropertyValueFactory<>("rB"));
            iB.setStyle("-fx-alignment: baseline_right;");
    
            tableID.getColumns().addAll(iA, iB, iC);
    
            tableID.setItems(data);
            tableID.setPrefSize(150, 125);
    
            stage.setScene(new Scene(tableID));
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }    
    
        final ObservableList<Table> data = FXCollections.observableArrayList(
                new Table(5, 3),
                new Table(7, 4),
                new Table(9, 3)
        );
    }