Hi,
lets say I have a HorizontalLayout as a parent and inside that is a Button. Am I able to remove the HorizontalLayout including all the content that it has just by calling the remove() function or will there be problems? I always find examples on how to remove a child like the button in my example but not the other way around.
What I basically want to do is when the button is pressed a ConfirmDialog will open. If you confirm that Dialog the HorizontalLayout including all the content inside should be removed.
Here is a code example maybe you can understand my problem. I use Vaadin 14 btw.
public class FriendsView extends VerticalLayout {
private VerticalLayout friendsContainer;
public FriendsView(){
addClassName("friends-view-vertical-layout");
friendsContainer = new VerticalLayout();
friendsContainer.setSizeFull();
HorizontalLayout addFriendButtonContainer = new HorizontalLayout();
Button btnAddFriend = new Button("Add New Friend!");
btnAddFriend.setIcon(new Icon(VaadinIcon.PLUS));
addFriendButtonContainer.add(btnAddFriend);
//Dialog for Add Button
VerticalLayout dialogLayout = new VerticalLayout();
Dialog addFriendDialog = new Dialog();
addFriendDialog.setCloseOnEsc(true);
Label title = new Label("Add a Friend by Username");
Input userNameInput = new Input();
Button confirmButton = new Button("Add");
Button cancelButton = new Button("Cancel");
cancelButton.addClickListener(click -> {
addFriendDialog.close();
});
confirmButton.addClickListener(click -> {
String friendName = userNameInput.getValue();
createFriendAndAdd(friendName);
userNameInput.clear();
addFriendDialog.close();
});
dialogLayout.add(title, userNameInput, confirmButton, cancelButton);
addFriendDialog.add(dialogLayout);
btnAddFriend.addClickListener(click -> {
addFriendDialog.open();
});
add(addFriendButtonContainer, friendsContainer);
}
public void createFriendAndAdd(String name){
HorizontalLayout friendLayout = new HorizontalLayout();
friendLayout.setClassName("friend-layout");
friendLayout.setWidthFull();
friendLayout.setAlignItems(Alignment.CENTER);
Button btnFriend = new Button(name);
btnFriend.setWidth("70%");
Button btnDeleteFriend = new Button();
btnDeleteFriend.setWidth("20%");
btnDeleteFriend.setIcon(new Icon(VaadinIcon.TRASH));
//Dialog for deleting a Friend
Dialog deleteFriendDialog = new Dialog();
VerticalLayout deleteFriendDialogVerticalLayout = new VerticalLayout();
HorizontalLayout deleteFriendDialogHorizontalLayout = new HorizontalLayout();
deleteFriendDialog.setCloseOnEsc(true);
Label title = new Label("Are you sure you want to delete this friend?");
Button confirmButton = new Button("Delete");
Button cancelButton = new Button("Cancel");
deleteFriendDialogVerticalLayout.add(title);
deleteFriendDialogHorizontalLayout.add(cancelButton, confirmButton);
deleteFriendDialog.add(deleteFriendDialogVerticalLayout, deleteFriendDialogHorizontalLayout);
confirmButton.addClickListener(click -> {
//I need to delete the HorizontalLayout that contains the btnFriend and the deleteButton
deleteFriendDialog.close();
});
cancelButton.addClickListener(click -> {
deleteFriendDialog.close();
});
//Opens Dialog when pressing the DeleteButton
btnDeleteFriend.addClickListener(click -> {
deleteFriendDialog.open();
});
friendLayout.add(btnFriend, btnDeleteFriend);
friendsContainer.addComponentAtIndex(0, friendLayout);
}
Thanks if you can help.