1

我用 1 类的不同数据填充我的 tableview 没有问题。但它不适用于我的多个课程。知道如何解决吗?我已经在 stackoverflow 上查看了类似的问题。但他们都无法帮助我。如果您对“回调”类提出任何建议,请向我提供完整的导入,因为那里有几个回调类。

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 类中有一个HashSet。在那个 HashSet 中有“ProjectControl”类的实例。ProjectControl 的每个实例都具有“name”或“id”等属性。

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

任务类(缩短了很多)我的TaskControl类继承自这个类

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;
  }
}
4

2 回答 2

0

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

您需要一个额外的类来保存您的 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"));
于 2016-01-04T08:22:30.867 回答
0

通过向我的 TaskControl 添加一个额外的字符串来解决它,其中包含它包含的所有项目的名称。它通过我在为表列创建 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;
}

无论如何,谢谢你们帮助我。

于 2016-01-09T01:47:53.430 回答