gpt4 book ai didi

java - 如何在Vaadin中实现一键上传组件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:21:23 25 4
gpt4 key购买 nike

我有以下实现单个上传示例的类

@Override
public void init() {
Window mainWindow = new Window("Singleuploadclick Application");
Label label = new Label("Hello Vaadin user");

mainWindow.addComponent(label);

status = new Label("Please select a file to upload");
upload = new Upload(null, receiver);

upload.setImmediate(true);
upload.setButtonCaption("Select file");

upload.addListener(new Upload.StartedListener() {
private static final long serialVersionUID = 1L;

@Override
public void uploadStarted(StartedEvent event) {
upload.setVisible(false);
status.setValue("Uploading file \"" + event.getFilename() + "\"");
}
});

upload.addListener(new Upload.ProgressListener() {
private static final long serialVersionUID = 1L;

@Override
public void updateProgress(long readBytes, long contentLength) {
}

});

upload.addListener(new Upload.SucceededListener() {
private static final long serialVersionUID = 1L;

@Override
public void uploadSucceeded(SucceededEvent event) {
status.setValue("Uploading file \"" + event.getFilename() + "\" succeeded");
}
});

upload.addListener(new Upload.FailedListener() {
private static final long serialVersionUID = 1L;

@Override
public void uploadFailed(FailedEvent event) {
status.setValue("Uploading interrupted");
}
});

upload.addListener(new Upload.FinishedListener() {
private static final long serialVersionUID = 1L;

@Override
public void uploadFinished(FinishedEvent event) {
upload.setVisible(true);
upload.setCaption("Select another file");
}
});

mainWindow.addComponent(status);
mainWindow.addComponent(upload);
setMainWindow(mainWindow);
}

运行应用程序时,上传组件的布局显示很奇怪。

enter image description here

所以我只需要一个上传按钮的上传组件,这就是为什么我使用:upload.setImmediate(true);

最佳答案

Button.setImmediate(true) 用于在文件选择后开始上传(无需点击按钮)。但是您仍然需要使用 CSS 隐藏按钮。

引自 Book of Vaadin 5.25 Upload :

You can also hide the upload button with .v-upload .v-button {display: none} in theme, have custom logic for starting the upload, and call startUpload() to start it. If the upload component has setImmediate(true) enabled, uploading starts immediately after choosing the file.

因此您需要将其添加到您的自定义主题中:

.v-upload .v-button {
display: none
}

关于java - 如何在Vaadin中实现一键上传组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28633929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com