Kamrul Hasan
Kamrul Hasan

Reputation: 69

How to fix object set in grid?

In my application i have a class like:

public class Team {
    private Country teamId;
    private Set<Player> playerSet;
    private Set<Player> substitutes;
    private Set<Coach> coachSet; 
}

When i instantiate a grid like:

Grid<Team> grid = new Grid<>(Team.class);

and set allTeam() from database it shows object for playerSet and coachSet.

My question is i just want to show players name and coach name concate by ,or \n.

Any idea how can i do that?As a beginner it is complicated for me

Upvotes: 2

Views: 283

Answers (2)

kscherrer
kscherrer

Reputation: 5766

I see three options.

The first option is the one you already found yourself: concatenate their names in a single String. This can be done like this:

grid.addColumn(team -> {
    Set<String> coachNames = new HashSet<>();
    for (Coach coach : team.getCoaches()){
        coachNames.add(coach.getName());
    }
    return String.join(", ", coachNames);
});

The second one would be to make use of the Grid item Detail - you could show a coaches grid in the item details. Since you want to display both coaches and players, this option is probably not the best but I wanted to mention the possibility. (Placing two grids inside the item details is possible, but quite strange. Not optimal user experience.)

grid.setItemDetailsRenderer(new ComponentRenderer<>(team -> {
    Grid<Coach> coachGrid = new Grid<>(Coach.class);
    coachGrid.setItems(team.getCoaches());
    return coachGrid;
}));

A third option would be to have the team grid on one side of the view, and on the other you show some relevant stuff of the selected item of the team grid. You can have a separate Grid for the coaches, one for the players, one for the substitutes. You could implement this team detail layout also as a separate view if you wish. If your Team object will get more complicated with more sets, collections and other relative properties, the more will this option become appealing, as this is quite scalable/expandable.

grid.addSelectionListener(event -> {
    if(event.getFirstSelectedItem().isPresent()){
        buildTeamDetails(event.getFirstSelectedItem().get())
    }
})

private void buildTeamDetails(Team team){
    // build your team detail layouts here
}

Upvotes: 2

codinghaus
codinghaus

Reputation: 2358

You can configure which columns are shown in the grid by using grid.removeAllColumns() and then adding all columns you want to have in the grid with grid.addColumn(). Within addColumn() you can create a renderer that defines how the fields (coachName and playerSet) are displayed in the grid.

Let's have a class Team like

public class Team {
    private String coachName;
    private Set<Player> playerSet;
    private Set<Object> objects;
    //getters and setters
}

and a class Player like

public class Player {
    private String firstName;
    private String lastName;
    // getters and setters
}

Now you want to only have coach and player names in the grid. So (in my example) for coachName we can just use the field's getter and we can create a comma separated String for the playerSet with java streams easily.

Configure the grid like:

   grid.setItems(team);
   grid.removeAllColumns();
   grid.addColumn(new TextRenderer<>((ItemLabelGenerator<Team>) Team::getCoachName))
           .setHeader("Coach");
   grid.addColumn(new TextRenderer<>((ItemLabelGenerator<Team>) team1 -> team1.getPlayerSet().stream()
           .map(player1 -> player1.getFirstName() + " " + player1.getLastName())
           .collect(Collectors.joining(", "))))
           .setHeader("Players")
           .setFlexGrow(1);

Then the result looks like:

enter image description here

Upvotes: 1

Related Questions