Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Upload component - how to I choose a file before invoking submitUpload()?

David Green
10 years ago Apr 04, 2012 9:44am

Hi,

I'm trying to create a custom upload dialogue positioning a button slightly differently to the default implementation. In my Button.ClickListener I have an upload.submitUpload() call, however a button appears on the for saying 'choose file'. How can avoid displaying this button and invoke the (file open) 'chooser' and/or set the filename I want to upload?

Also if there is an error I get a stack-trace on the UI - I thought I could wrap my button in an exception handler to avoid this but since the stack trace never includes my own classes I don't know how to put in an exception handling hook (/listener). What is the best way to do this?

Thanks

Code:

public class UploadComponent extends Window implements Upload.SucceededListener, Upload.FailedListener, Upload.Receiver, Upload.StartedListener, Upload.ProgressListener {

    private Upload upload = new Upload("", this);

    private Panel panel = null; // element for containing components
    private VerticalLayout v = new VerticalLayout();

    private Button submitButton = new Button("Upload...");

    private static final Logger log = LogUtil.getLogger();

    private Button.ClickListener submitButtonClickListener = new Button.ClickListener() {
        @Override
        public void buttonClick(Button.ClickEvent event) {
            try {
                log.info("Clicked submit");
                upload.submitUpload();
            }
            catch (Exception ex) {
                log.error("Caught exception after submit");
                log.error(ex.toString());
            }
        }
    };
    
    public UploadComponent() {

        upload.setImmediate(true); // Upload immediately

        upload.setDescription("");
        upload.setStyleName("default");

        upload.setButtonCaption(null); // Set the caption to null hides the upload button

        upload.addListener((Upload.StartedListener) this);
        upload.addListener((Upload.ProgressListener) this);
        upload.addListener((Upload.SucceededListener) this);
        upload.addListener((Upload.FailedListener) this);

        v.addComponent(upload);

        submitButton.addListener(submitButtonClickListener);;

        v.addComponent(submitButton);
        addComponent(v);

    }
Last updated on Apr, 4th 2012