简体   繁体   English

Vaadin 14 Upload - 如何在用户单击选择文件对话框中的“取消”按钮时捕获事件

[英]Vaadin 14 Upload - how to catch event when user clicks Cancel button in choose file dialog

I've tried all existing listeners, but no one can catch this type of event.我已经尝试了所有现有的侦听器,但没有人能够捕捉到这种类型的事件。

Button uploadButton = new Button("Choose a file");
uploadButton.setDisableOnClick(true);
Upload upload = new Upload();
upload.setUploadButton(uploadButton);

User clicks uploadButton, button now disabled.用户点击上传按钮,按钮现在被禁用。 Then in system choose file dialog user clicks Cancel button instead of choosing a file.然后在系统选择文件对话框中,用户单击取消按钮而不是选择文件。 Dialog is closed, no event is fired, uploadButton still disabled.对话框已关闭,未触发任何事件,uploadButton 仍处于禁用状态。 I want to catch event when Cancel button is pressed and enable uploadButton.我想在按下取消按钮时捕获事件并启用上传按钮。

Behavior confirmed行为确认

I can confirm the behavior you saw: No events seem to fire when user cancels the file-picker dialog.我可以确认您看到的行为:当用户取消文件选择器对话框时,似乎没有事件触发。

Here is an example app in Vaadin 14.1.21.这是 Vaadin 14.1.21 中的示例应用程序。 I added listeners for several of the event types.我为几种事件类型添加了侦听器。

package work.basil.example;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.upload.*;
import com.vaadin.flow.component.upload.receivers.MemoryBuffer;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.server.PWA;

import java.time.Instant;

/**
 * The main view contains a button and a click listener.
 */
@Route ( "" )
@PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
@CssImport ( "./styles/shared-styles.css" )
@CssImport ( value = "./styles/vaadin-text-field-styles.css", themeFor = "vaadin-text-field" )
public class MainView extends VerticalLayout
{

    public MainView ( )
    {
        MemoryBuffer buffer = new MemoryBuffer();
        Upload upload = new Upload( buffer );

        upload.addStartedListener( ( StartedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addSucceededListener( ( SucceededEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFinishedListener( ( FinishedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addAllFinishedListener( ( AllFinishedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addProgressListener( ( ProgressUpdateEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFileRejectedListener( ( FileRejectedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFailedListener( ( FailedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );

        upload.addFileRejectedListener( ( FileRejectedEvent event ) -> System.out.println( event.getClass().getCanonicalName() + " happened " + Instant.now() ) );


        this.add( upload );
    }
}

Feature, not a bug功能,而不是错误

I consider this behavior a feature.我认为这种行为是一个特征。 If the user clicks the button to choose files, but cancels before completing that choice, nothing has really happened.如果用户单击按钮选择文件,但在完成该选择之前取消,则实际上什么也没有发生。 No upload has been attempted.未尝试上传。 So no event should fire as nothing eventful has transpired.因此,不应触发任何事件,因为没有发生任何事件。

Perhaps you should edit your Question to explain your interest in detecting the user having changed their mind about choosing files to upload.也许你应该编辑你的问题来解释你有兴趣检测用户改变了他们选择要上传的文件的想法。 Perhaps there is a better solution to your ultimate goal.也许有更好的解决方案来实现您的最终目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户单击后退按钮时,如何显示 Vaadin 23 的确认对话框? - How can I show a confirm dialog with Vaadin 23 when the user clicks on back button? 使用Primefaces fileUpload组件,当用户单击“取消”时会触发事件吗? - With Primefaces fileUpload component, is there an event fired when the user clicks “Cancel”? 带有选项卡的 Vaadin 14 对话框 - Vaadin 14 dialog with tabs JavaFX:当用户单击 X 按钮时显示对话框窗口? - JavaFX: Show a Dialog window when a user clicks the X button? 当用户单击X按钮退出程序时,如何编写一个弹出对话框的编码? - How do I code a pop up dialog box to appear when the user clicks the X button to exit the program? 如何制作一个像这个自定义对话框一样的对话框选择框,右上角像这样一个取消按钮? - How to make a the dialog choose box like this custom dialog with cancel button on the top right corner like this one? 在对话框中的按钮上捕获单击事件 - catch a click event on a button in dialog Vaadin 14 Grid + Dialog:如何在关闭对话框后刷新网格? - Vaadin 14 Grid + Dialog: How to refresh grid after closing dialog? 单击JDialog中的“取消”按钮时发生异常 - Exception when clicks Cancel button in JDialog 用户单击通知时如何显示警报对话框? - How to bring alert dialog when user clicks on notification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM