Javafx tableview包含来自多个类的数据 [英] Javafx tableview with data from multiple classes

查看:103
本文介绍了Javafx tableview包含来自多个类的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用来自1个类的不同数据填充我的tableview没有问题。但是对于我来说,多个班级并不适用。知道如何解决这个问题吗?
我在stackoverflow上检查了类似的问题。但他们都不能帮助我。如果您对Callback类提出任何建议,请提供完整的导入,因为那里有几个Callback类。

I have no problem filling my tableview with diffrent data from 1 class. But it does not work for me with multiple classes. Any idea how to solve that? I have checked out similar questions on stackoverflow. But none of them could help me. If you suggest anything with the "Callback" class, please provide me the full import, because there are a couple of Callback classes out there.

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

进一步的问题:如何在HashSet中显示实例的某个属性在TableCell中。所以我在TaskControl类中有一个HashSet。在该HashSet中有ProjectControl类的实例。 ProjectControl的每个实例都有name或id等属性。

Further question: How can I show a certain Attribute of an Instance in a HashSet in a TableCell. So I have in my TaskControl class a HashSet. In that HashSet there are Instances of the class "ProjectControl". Every instance of ProjectControl has attributes like "name" or "id" etc.

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

And I want to represent all the names of the project instances in 1 single table cell if possible. Maybe as a string seperated with commas (project1,project2,project3...).

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

Task class (shortened a lot) my TaskControl Class inherits from this 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;
  }
}


推荐答案

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

Solved it by adding an additional String to my TaskControl, that contains the names of all the projects it contains. It gets the names through a function that I call just before I create the ObservableList for the Table Column.

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

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

Thank you guys anyways for helping me.

这篇关于Javafx tableview包含来自多个类的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆