Reputation: 247
I am using Vaadin in Java and I am following this tutorial: Vaadin Upload So I have created a new Class name Uploader. But there is some stuff which doesn't work in my code, I put what is not working in ** text **:
import com.vaadin.server.FileResource;
import com.vaadin.ui.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
/**
* Created by mflamant on 15/05/2017.
*/
public class Uploader {
final Embedded image = new Embedded("Uploaded image");
**image.setVisible(false);**
class Image implements Upload.Receiver, Upload.SucceededListener{
public File file;
public OutputStream receiveUpload(String filename, String mimeType){
FileOutputStream fos = null;
try{
file = new File(filename);
fos = new FileOutputStream(file);
} catch (final java.io.FileNotFoundException e){
e.printStackTrace();
return null;
}
return fos;
}
public void uploadSucceeded(Upload.SucceededEvent event){
image.setVisible(true);
image.setSource(new FileResource(file));
}
};
Image receiver = new Image();
Upload upload = new Upload("Upload image here", receiver);
**upload.setButtonCaption("Start Upload");**
**upload.SucceededListener(receiver);**
Panel panel = new Panel("Image storage");
Layout panelContent = new VerticalLayout();
**panelContent.addComponents(upload, image);**
**panel.setContent;**
}
The error I have is "Can not resolve symbol". Can you explain to me why these lines aren't working?
Upvotes: 1
Views: 943
Reputation: 13426
Upload example doesn't list the whole code of the application. It only includes the code snippets specific to the Upload component itself. These code snippets are not expected to work if you just paste them into your class.
This example is a part of Vaadin Documentation and you're expected to understand the basics at the time you reach this part.
Example code is intended to work as a part of a method that builds a Vaadin component. The particular error is that you can only call methods, like image.setVisible(false)
from an executable code block. You can't just paste them in your class declaration, that's not a valid Java code.
Tutorial links to a working code on Github. As you can see it contains all the necessary initialization in place:
public class UploadExample extends CustomComponent implements BookExampleBundle {
private static final long serialVersionUID = -4292553844521293140L;
public void init (String context) {
//... omitted for brevity
basic(layout);
//... omitted for brevity
}
void basic(VerticalLayout layout) {
final Image image = new Image("Uploaded Image");
//the rest of the example code goes here
Please, note that this class alone still doesn't work as a standalone application. This is just one of the components.
So, what you can do now:
Upvotes: 2