Width exceeds browser width

I have a vertical layout and a grid within it.

@Slf4j
public class AdminView extends VerticalLayout {

    private final Grid<User> grid;
    private final UserService userService;
    private final RoleService roleService;

    public AdminView(UserService userService, RoleService roleService) {
        this.userService = userService;
        this.roleService = roleService;
        setSizeFull();
        grid = generateGrid();
        add(grid);
    }

    private Grid<User> generateGrid() {
        Grid<User> userGrid = new Grid<>(User.class, false);
        userGrid.addColumn(User::getUsername).setHeader("Username").setAutoWidth(true).setFlexGrow(0);
        userGrid.addComponentColumn(user -> {
            TextField textField = new TextField();
            textField.setValue(user.getName());
            textField.addKeyDownListener(Key.ENTER, event -> {
                user.setName(textField.getValue());
                userService.updateUser(user);
            });
            return textField;
        }).setHeader("Name").setResizable(true).setAutoWidth(true).setFlexGrow(0);
        userGrid.addComponentColumn(user -> {
            MultiSelectComboBox<Role> comboBox = new MultiSelectComboBox<>();
            comboBox.setItemLabelGenerator(Role::getRole);
            comboBox.setItems(roleService.findAll());
            comboBox.select(user.getRoles());
            comboBox.addValueChangeListener(event -> {
                user.setRoles(event.getValue());
                userService.updateUser(user);
            });
            comboBox.setSelectedItemsOnTop(true);
            return comboBox;
        }).setHeader("Roles").setResizable(true).setAutoWidth(true).setFlexGrow(0);
        userGrid.addComponentColumn(user -> {
            Button deleteButton = new Button(VaadinIcon.TRASH.create(), event -> {
                userService.deleteUser(user);
                userGrid.setItems(userService.findAll());
            });
            deleteButton.addThemeVariants(ButtonVariant.LUMO_ERROR);
            return deleteButton;
        }).setHeader("Delete").setWidth("5rem").setFlexGrow(0);
        userGrid.setItems(userService.findAll());
        userGrid.setAllRowsVisible(true);
        userGrid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
        userGrid.addClassName(LumoUtility.Width.AUTO);
        userGrid.addClassName(LumoUtility.Flex.SHRINK);
        userGrid.setMaxWidth("100%");

        userGrid.addClassName(LumoUtility.Overflow.AUTO);
        return userGrid;
    }

}

This displays correctly on PC with big width, but on my phone its width exceeds phone width:


So my question is: how to make it never exceed browser width?

The grid is not a responsive component.

Check out this How do I make a responsive Grid that has a different set of columns depending on the browser width. - Vaadin Cookbook

1 Like

But if I want to make other columns smaller, there is no way of doing that?

Sure check the documentation Configuring columns of the Vaadin Grid component

1 Like

You’re using setAutoWidth(true) for your columns, which means they will take their width from the contents of the column. So either set that to false, or make the column contents narrower to fit more in.