JavaFX 表视图表模型连接



我想将我的javafx表视图连接到我的表模型的数据以显示在视图中。

我收到错误:

incompatible types: WorkoutTableModel cannot be converted to ObservableList

任何见解或建议将不胜感激。尝试转换为可观察列表时,我也遇到错误:

incompatible types: inference variable E has incompatible bounds
    equality constraints: Workout
    lower bounds: WorkoutTableModel
  where E is a type-variable:
    E extends Object declared in method <E>observableArrayList(E...)
The type of <E>observableArrayList(E...) is erroneous
  where E is a type-variable:
    E extends Object declared in method <E>observableArrayList(E...)

谢谢!

表型号:

package myworkouts.presentation;
import myworkouts.domain.*;
import java.util.*;
import javax.swing.table.AbstractTableModel;
public class WorkoutTableModel extends AbstractTableModel {
private String[] columnNames = {"Date", "Route", "Distance", "Time"};
private List<Workout> workouts = new LinkedList<Workout>();
public void setWorkouts(List<Workout> workouts) {
    this.workouts = workouts;
}    
@Override
public int getRowCount() {
    return workouts.size();
}
@Override
public int getColumnCount() {
    return columnNames.length;
}
@Override
public Object getValueAt(int row, int column) {
    Workout workout = workouts.get(row);
    switch (column) {
        case 0:
            return workout.getDate();
        case 1:
            return workout.getRoute();
        case 2:
            return workout.getDistance();
        case 3:
            return workout.getTime();
    }
    return "";
}
@Override
public String getColumnName(int column) {
    return columnNames[column];
    }
}

mainUI snippit:

public class MainUI extends Application {
private Account account = null;
private WorkoutTableModel model = new WorkoutTableModel();
final ObservableList<Workout> data = FXCollections.observableArrayList(model);
public void setAccount(Account account) {
    this.account = account;
}

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Workout CRUD form JavaFX Application");
    TableView workoutsTbl = new TableView(); 
    workoutsTbl.setEditable(true);
    workoutsTbl.setItems(model);
    // Add workoutsTbl header
    Text headerLabel = new Text("Workouts");
    headerLabel.getStyleClass().add("header");
    // add workoutsTbl column labels
    TableColumn dateCol = new TableColumn("Date");
    TableColumn routeCol = new TableColumn("Route");
    TableColumn distanceCol = new TableColumn("Distance");
    TableColumn timeCol = new TableColumn("Time");     
    workoutsTbl.getColumns().addAll(dateCol, routeCol, distanceCol, timeCol);      
    workoutsTbl.autosize();
    VBox vbox  = new VBox(10);
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10,5,5,10));
    // Create the registration form grid pane
    GridPane gridPane = createWorkoutsFormPane();
    //gridPane.add(workoutsTbl,1,10,2,5);
    vbox.getChildren().addAll(headerLabel,workoutsTbl,gridPane);
    // Add UI controls to the registration form grid pane
    addUIControls(gridPane);
    // Create a scene with registration form grid pane as the root node
    Scene scene = new Scene(vbox, 520, 500);
    // Set the scene in primary stage   
    primaryStage.setScene(scene);
    scene.getStylesheets().add
        (MainUI.class.getResource("Login.css").toExternalForm());  
    primaryStage.show();
}

final ObservableList data = FXCollections.observableArrayList(model(;

Swing AbstractTableModel 不是列表类型的东西。查看它的声明,看看它实现了什么。它不是列表。

最新更新