Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - Javafx tableview with data from multiple classes

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

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.

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...).

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;
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my opition you only have one nice solution for this.

You need a extra Class that holds your TaskControl, ContextControl and ProjectControl.

Your Code can look something like that.

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"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...