0

I basically am trying to populate a table with a class that contains a string field and a map with arraylist as value.

EDIT: So what I want in the table is is something like this:

Student name Science
Bro Man Passs
--- Excellent

The class

public class ItemList {

    private String studentNameList;
    private HashMap<String, ArrayList<SimpleStringProperty>> remarksList;

    public String getStudentNameList() {
        return studentNameList;
    }

    public void setStudentNameList(String studentNameList) {
        this.studentNameList = studentNameList;
    }

    public HashMap<String, ArrayList<SimpleStringProperty>> getRemarksList() {
        return remarksList;
    }

    public void setRemarksList(HashMap<String, ArrayList<SimpleStringProperty>> remarksList) {
        this.remarksList = remarksList;
    }
}

The controller class has a tableview that will hold the class entries I have a loadTable method that loads the class into the tableview

The controller class

public class HelloController implements Initializable {

    @FXML
    private TableView tvTable;
    
    // Observable object to hold the classes
    private ObservableList<Item> tablelist = FXCollections.observableArrayList();

     @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        

        // Item with arraylist as value
        ItemList itemlist = new ItemList();
        itemlist.setStudentNameList("Bro Man");

        HashMap <String, ArrayList<SimpleStringProperty>> remarks = new HashMap<>();
        ArrayList<SimpleStringProperty> remarksString = new ArrayList<>();

        remarksString.add(new SimpleStringProperty("Pass"));
        remarksString.add(new SimpleStringProperty("Excellent"));

        remarks.put("Science",remarksString);


        itemlist.setRemarksList(remarks);
        

        tablelist.add(itemlist);
        
        Method to load table call
        loadTableList(tablelist);


    }

The method

private void loadTableList(ObservableList<ItemList> remarks) {
        tvTable.setItems(remarks);
        tvTable.setVisible(true);

        TableColumn<ItemList, String> column = new TableColumn<ItemList, String>("student name");
        column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getStudentNameList()));
        tvTable.getColumns().add(column);



        // The looping through the hashmap containing arraylist value
        for (Map.Entry<String, ArrayList<SimpleStringProperty>> entry : remarks.get(0).getRemarksList().entrySet()) {
          // Using the key of the map as column name
          TableColumn<ItemList, Number> col = new TableColumn<ItemList, Number>(entry.getKey());

    // What should the code here contains to loop through the arraylist of the values and use as the rows

        }
    }

How do I loop through the hashmap arraylist

Maybe the beginning of the loop itself NOT right I might need a better solution

  • 1
    Does this answer your question? [Populate TableView with Map> JavaFX](https://stackoverflow.com/questions/48889618/populate-tableview-with-mapstring-mapstring-string-javafx) – Giovanni Contreras May 29 '22 at 00:42
  • 1
    how is this different from your parallel question https://stackoverflow.com/questions/72420070/javafx-hashmap-with-arraylist-as-value-into-tableview? why did you create a new account https://stackoverflow.com/questions/72412234/the-method-entryset-is-undefined-for-the-type-mappingchange-mapstring-propert? anyway: [mcve] please, demonstrating what _exactly_ you want to achieve and how it doesn't work – kleopatra May 29 '22 at 05:13
  • 1
    Use a `TreeTableView`, not a `TableView`. – jewelsea May 29 '22 at 05:21

0 Answers0