如何将clicklistener附加到Vaadin标签?

use*_*306 6 label vaadin

如何在不添加水平或垂直布局的情况下将clicklistener添加到vaadin标签?我想在点击标签时显示工具提示,而不是鼠标悬停.

Ole*_*leg 9

那是不可能的.

把它放在一个布局中真的没什么大不了的,以下是你需要做的:

HorizontalLayout labelLayout = new HorizontalLayout();
labelLayout.addComponent(new Label("text"));
labelLayout.addLayoutClickListener( e -> <code that does something>);
Run Code Online (Sandbox Code Playgroud)

如果您不想这样做,您可以使用第三方添加,这完全符合您的要求.https://vaadin.com/directory#!addon/labelbutton

有了它你可以做:

LabelButton label = new LabelButton("text", event -> <do something>);
Run Code Online (Sandbox Code Playgroud)


Dun*_*523 5

我建议您使用按钮并添加无边框样式,如下面的代码所示.它将显示为标签.

    VerticalLayout vertical = new VerticalLayout();
    vertical.addComponent(new Label("Am the Hint..."));

    PopupView popup = new PopupView(null,vertical);

    Button b = new Button("Show Hint");
    b.addStyleName(ValoTheme.BUTTON_BORDERLESS);
    b.addClickListener((Button.ClickEvent event) -> {
        popup.setPopupVisible(true);
    });
    addComponents(b, popup);
Run Code Online (Sandbox Code Playgroud)