Javafx tableview包含来自多个类的数据

时间:2016-01-02 23:24:24

标签: java javafx tableview tablecolumn observablelist

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

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

2 个答案:

答案 0 :(得分:0)

在我的选择中,你只有一个很好的解决方案。

您需要一个额外的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"));

答案 1 :(得分:0)

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

    use Regexp::Grammars;
qr{ 
    \A 
    <Answer>
    (*COMMIT)   # <-- Remove this to see the error messages get less accurate
    (?:
        \Z
    |
        <warning: (?{ "Extra junk after expression at index $INDEX: '$CONTEXT'" })>
        <warning: Expected end of input>
        <error:>
    )

    <rule: Answer>  
        <[_Operand=Mult]>+ % <[_Op=(\+|\-)]>
            (?{ $MATCH = shift @{$MATCH{_Operand}};
                for my $term (@{$MATCH{_Operand}}) {
                    my $op = shift @{$MATCH{_Op}};
                    if ($op eq '+') { $MATCH += $term; }
                    else            { $MATCH -= $term; }
                }
            })
      |
        <error: Expected valid arithmetic expression>

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