Java + Vaadin 创建按钮时出现问题
这是使用 Vaadin 的经典示例
@Route("hello")
public class Dupa extends VerticalLayout {
public renderButtons() throws IOException, I2CFactory.UnsupportedBusNumberException {
Button kitchen_button = new Button("Kitchen");
Button bathrooom_button = new Button("bathroom");
kitchen_button .addClickListener(click -> {
//some actions...
});
add(kitchen_button );
add(bathrooom_button );
}
}
我的问题是从类外部访问 kitchen_button。我尝试将其添加为静态字段,作为类中的字段。甚至尝试在其他类中创建此按钮并作为参数返回此处。
当有人打开页面时,此函数始终运行。因此,如果您在 Firefox 或 Chrome 上打开页面(转到特定端点),它将运行该方法两次。这也造成了问题。我尝试了疯狂的解决方案,例如创建此按钮后,将其添加到列表中,然后对列表中的每个元素运行我的操作。这是非常愚蠢的解决方案,因为每次打开新页面后都会创建此按钮。 没有成功..
我想从另一个类使用该方法,
public void changeColourButtonToRedOff(Button ob) {
ob.addThemeVariants(ButtonVariant.LUMO_PRIMARY, ButtonVariant.LUMO_ERROR);
ob.removeThemeVariants(ButtonVariant.LUMO_SUCCESS);
}
我尝试了很多事情,如上所述,但它不起作用
我尝试将其添加为静态,作为类中的字段。甚至尝试在其他类中创建此按钮并作为参数返回此处。
This is classic example of using Vaadin
@Route("hello")
public class Dupa extends VerticalLayout {
public renderButtons() throws IOException, I2CFactory.UnsupportedBusNumberException {
Button kitchen_button = new Button("Kitchen");
Button bathrooom_button = new Button("bathroom");
kitchen_button .addClickListener(click -> {
//some actions...
});
add(kitchen_button );
add(bathrooom_button );
}
}
My problem is to access kitchen_button from outside the class. I tried adding this as static, as field in class. Even tried to create this buttons in other class and returns here as parameters.
This function runs always when somebody open a page. So if you open page ( go to the specific endpoint) on firefox or chrome it will run that method twice. This makes problem too. I tried crazy solution like, after creating this button, adding it on the list, and then running my actions on every element on the list. It is very stupid solution because after each opening new page this buttons are creating.
No success..
I would like from another class use that method
public void changeColourButtonToRedOff(Button ob) {
ob.addThemeVariants(ButtonVariant.LUMO_PRIMARY, ButtonVariant.LUMO_ERROR);
ob.removeThemeVariants(ButtonVariant.LUMO_SUCCESS);
}
I tried many things as I said above but it does not work
I tried adding this as static, as field in class. Even tried to create this buttons in other class and returns here as parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现通常最好将 Vaadin 小部件声明为布局上的成员字段,而不是本地的成员字段。
要使该按钮在任何地方都可用,请将其标记为
public
。我并不是建议让您的 Vaadin 小部件随处可用。你说:
从多个类访问您的 Vaadin 小部件可能表明设计很差。通常最好让布局管理自己的小部件。其他类应该向布局发送消息,而不是直接操作布局的内容。
你说:
不,绝对不要这样做。每个用户都必须有自己的按钮实例。使用
static
,整个 JVM 中只有一个实例,而 Vaadin不允许 允许共享状态/组件。当尝试附加到另一个会话时,您将收到错误消息。I find it is usually best to declare the Vaadin widgets as member fields on the layout rather than locally.
To make that button available everywhere, mark it as
public
. Not that I recommend making your Vaadin widgets available everywhere.You said:
Accessing your Vaadin widgets from across multiple classes may indicate a poor design. Generally best to have the layout manage its own widgets. Other classes should send messages to the layout rather than directly manipulate the content of the layout.
You said:
No, definitely do not do that. Each of your users must have their own instance of the button. With
static
you would have only one instance throughout the entire JVM, and Vaadin does not allow shared state/components. When attempting to attach to another session, you will get an error.