官术网_书友最值得收藏!

Time for action – using an InlineDateField component

Let's change the DateField example a little bit:

  1. Open or import the datefield example in your IDE.
  2. Replace DateField with InlineDateField in DateFieldUI.java.
  3. Deploy and run the example.

What just happened?

This is the result you get:

You cannot type the year now, you have to select it from the component.

Uploading files

Study this simple example that displays (in the console) the content of a text file uploaded by the user:

public class UploadUI extends UI implements Receiver {
  @Override
  protected void init(VaadinRequest request) {
 Upload upload = new Upload(
 "Select a text file and look at the console",
 this);

    VerticalLayout layout = new VerticalLayout();
    layout.addComponent(upload);
    setContent(layout);
  }

  @Override
  public OutputStream receiveUpload(String filename,
      String mimeType) {
 return new OutputStream() {
 @Override
 public void write(int b) throws IOException {
 System.out.print((char) b);
 }
 };
  }


}

The Upload class displays a native (browser-dependent) button to select a file and a Vaadin button to upload the file to the server:

We are passing a Receiver interface (implemented in our UploadUI class) to process the upload byte-by-byte. The receiveUpload method must return an OutputStream class that writes the content of the file being uploaded. In this example, we are taking each byte and printing it on the console, but you can do with the bytes whatever you want to. For example, you could write them to a file in the hard disk or in a BLOB in an SQL database.

No, really, how can we actually write the file to the server? OK, OK, here is a possible implementation of recieveUpload that does the job:

@Override
public OutputStream receiveUpload(String filename,
    String mimeType) {
  FileOutputStream output = null;

  try {
    output = new FileOutputStream("C:\\Users\\Alejandro\\"
        + filename);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
  return output;
}

Pop quiz – thinking in Vaadin

Check your knowledge:

Q1. Which method allows you to add options in a ComboBox?

  1. addOption(String option).
  2. addItem(String itemId).
  3. addItem(Object itemId).

Q2. If you want to execute some code when the user changes the value in a Field component you must add a:

  1. ValueChangeListener.
  2. ClickListener.
  3. ValueChangeEvent.

Q3. To add a tooltip, you use the method:

  1. setTooltip(String text).
  2. addTooltip(String text).
  3. setDescription(String text).

Q4. You can add an error message to a Component by calling:

  1. setErrorMessage(String error).
  2. setComponentError(ErrorMessage errorMessage).

Q5. All input components implement (directly or indirectly):

  1. Field.
  2. Property.
  3. All of the above.

Q6. A Property is:

  1. A value of a certain type.
  2. A collection of items.
主站蜘蛛池模板: 金华市| 思南县| 昭平县| 武义县| 延津县| 松滋市| 民县| 南雄市| 乐山市| 蓬安县| 岑溪市| 平阳县| 徐州市| 鹤岗市| 滨海县| 苏尼特右旗| 盱眙县| 寿阳县| 乌什县| 大埔县| 洪洞县| 获嘉县| 翁牛特旗| 新绛县| 墨竹工卡县| 天镇县| 庆安县| 乌鲁木齐县| 黑水县| 涡阳县| 鹤庆县| 土默特左旗| 华宁县| 阳曲县| 济南市| 和静县| 双城市| 屯昌县| 湄潭县| 彭阳县| 秦皇岛市|