具有来自多个 类 的数据的 Javafx tableview

Javafx tableview with data from multiple classes

我可以毫无问题地使用来自 1 class 的不同数据填充我的 table 视图。但它不适用于多个 classes。知道如何解决吗? 我在 Whosebug 上查看了类似的问题。但其中 none 可以帮助我。如果您对 "Callback" class 有任何建议,请向我提供完整的导入,因为那里有几个回调 classes。

public class MainViewController implements Initializable {

    @FXML
    private TableColumn<TaskControl, Boolean> colErledigt;

    @FXML
    private TableColumn<TaskControl, Character> colPrioritaet;

    @FXML
    private TableColumn<TaskControl, String> colBeschreibung;


    @FXML
    private TableColumn<ProjectControl, String> colProjekt;


    @FXML
    private TableView<TaskControl> tblView;





    public final void initialize(final URL location,
            final ResourceBundle resources) {

        initializeTableElements();

    }


    public final void initializeTableElements() {
        colBeschreibung
                .setCellValueFactory(new PropertyValueFactory<>("description"));

        colPrioritaet
                .setCellValueFactory(new PropertyValueFactory<>("priority"));


        colProjekt.setCellValueFactory(new PropertyValueFactory<>("name"));


        colErledigt.setMaxWidth(50);
        colErledigt.setCellValueFactory(
                new PropertyValueFactory<TaskControl, Boolean>("isDone"));
        colErledigt
                .setCellFactory(CheckBoxTableCell.forTableColumn(colErledigt));
        colErledigt.setEditable(true);

        try {
            tblView.setItems(getObsTasks());
        } catch (IDNotValidException | StringNotValidException e1) {
            System.out.print("FEHLER beim getObsTasks");
        }

        tblView.setEditable(true);
    }


    public ObservableList<TaskControl> getObsTasks()
            throws IDNotValidException, StringNotValidException {

        ObservableList<TaskControl> obsTasks = FXCollections
                .observableArrayList();

        Map<Context, Set<Task>> test = TasksContextUtility.INSTANCE
                .getAllContextsAndTasks();

        test.values().forEach(v -> {
            v.forEach(b -> obsTasks.add((TaskControl) b));
        });

        return obsTasks;
    }

进一步的问题:如何在 TableCell 的 HashSet 中显示实例的特定属性。所以我在我的 TaskControl class 中有一个 HashSet。在那个 HashSet 中有 class "ProjectControl" 的实例。 ProjectControl 的每个实例都具有 "name" 或 "id" 等属性。

如果可能,我想在 1 个 table 单元格中表示项目实例的所有名称。可能是用逗号分隔的字符串 (project1,project2,project3...).

Taskclass(缩短了很多)我的TaskControlClass继承自这个class

public abstract class Task
  implements Serializable, IDValidatable
{
  private int id;
  private char priority = ' ';
  private final Set<Project> projects = new HashSet();

  public Task(int oid)
    throws IDNotValidException
  {
    if (isIDValid(oid)) {
      this.id = oid;
    } else {
      throw new IDNotValidException("The ID you have specified is not valid!")
      {
        private static final long serialVersionUID = 99044660889990790L;
      };
    }
  }

  public final void setId(int oid)
    throws IDNotValidException
  {
    if (isIDValid(oid)) {
      this.id = oid;
    } else {
      throw new IDNotValidException("The ID you have specified is not valid!")
      {
        private static final long serialVersionUID = 99044660889990790L;
      };
    }
  }

  public final int getId()
  {
    return this.id;
  }

  public final Collection<Context> getContexts()
  {
    return this.contexts;
  }

  public final void addContext(Context context)
    throws ContextNotValidException
  {
    this.contexts.add(context);
  }

  public final void removeContext(Context context)
    throws ContextNotValidException
  {
    this.contexts.remove(context);
  }

  public final Collection<Project> getProjects()
  {
    return this.projects;
  }

  public final void addProject(Project project)
    throws ProjectNotValidException
  {
    this.projects.add(project);
  }

  public final void removeProject(Project project)
    throws ProjectNotValidException
  {
    this.projects.remove(project);
  }

  public final Map<String, String> getAddons()
  {
    return this.addons;
  }
}

在我看来,你只有一个很好的解决方案。

您需要一个额外的 Class 来容纳您的 TaskControl、ContextControl 和 ProjectControl。

您的代码可能看起来像那样。

class Wrapper{
 private TaskControl taskControl;
 private ContextControl contextControl;
 private ProjectControl projectControl;
 ...
 public Boolean isDone(){
   return taskControl != null ? taskControl.isDone() : null;
  }
}


@FXML
private TableView<Wrapper> tblView;

@FXML
private TableColumn<Wrapper, Boolean> colErledigt;

colErledigt.setCellValueFactory(
            new PropertyValueFactory<Wrapper, Boolean>("isDone"));

通过向我的 TaskControl 添加一个额外的字符串来解决它,该字符串包含它包含的所有项目的名称。它通过我在为 Table 列创建 ObservableList 之前调用的函数获取名称。

private String projectsAsString;
    ...
public final void convertProjectsToString() {

    String projects = "";

    for (Project p : this.getProjects()) {
        ProjectControl pp = (ProjectControl) p;
        projects += pp.getName() + ", ";
    }
    if (projects != null && projects != "" && projects.length() > 4) {
        projects = projects.substring(0, projects.length() - 2);
    }
    this.projectsAsString = projects;
}

谢谢大家的帮助。