【问题标题】:javafx tableview tablemodel connectionjavafx tableview tablemodel 连接
【发布时间】:2018-03-02 17:49:58
【问题描述】:

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

我收到一个错误:

incompatible types: WorkoutTableModel cannot be converted to ObservableList

非常感谢任何见解或建议。 我在尝试转换为 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 代码片段:

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();
}

【问题讨论】:

  • 您混淆了 Swing 和 JavaFX。这是两个完全不同的工具包。
  • 这是真的。我只是注意到了。我将如何更新模型以适应 JavaFX UI?
  • 这个问题太笼统了。看看一些教程,如果你不能让它工作,就问一个具体的问题。 makery tutorial 很受欢迎
  • 使用data 作为项目列表并设置cellValueFactorys 以返回适当的属性。假设 getter 是公共的PropertyValueFactory,构造函数参数为"date""route""distance""time"。但请帮自己一个忙,将类型参数添加到TableViewTableColumns。否则编译器的某些类型检查可能是不可能的,并且会在运行时导致异常,而不是更难调试...

标签: java javafx tableview tablemodel


【解决方案1】:

最终 ObservableList 数据 = FXCollections.observableArrayList(model);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-17
    • 2017-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2013-01-20
    • 2013-03-29
    相关资源
    最近更新 更多