Search in sources :

Example 1 with StringToDoubleConverter

use of com.vaadin.data.converter.StringToDoubleConverter in project charts by vaadin.

the class ChartWithExternalDataProviderWithChangingData method getChart.

@Override
protected Component getChart() {
    HorizontalLayout lo = new HorizontalLayout();
    VerticalLayout vlo = new VerticalLayout();
    DataProviderSeries<Data> ds = createChartDS();
    Component grid = createGrid();
    TextField field = new TextField("New data value");
    Binder<Data> binder = new Binder<>();
    binder.forField(field).withValidator((String v) -> v != null && !v.isEmpty(), "The field cannot be empty").withConverter(new StringToDoubleConverter("Not a double")).bind(Data::getValue, Data::setValue);
    binder.readBean(new Data(0.0));
    Button button = new Button("Add data", e -> {
        Data v = new Data(0.0);
        try {
            binder.writeBean(v);
            data.add(v);
            v = new Data(1.0);
            binder.readBean(v);
        } catch (ValidationException ve) {
        }
    });
    Component chart = createChart(ds);
    vlo.addComponents(field, button, grid);
    vlo.setSpacing(true);
    lo.addComponents(vlo, chart);
    grid.setSizeFull();
    chart.setSizeFull();
    lo.setSizeFull();
    lo.setExpandRatio(vlo, 1);
    lo.setExpandRatio(chart, 3);
    return lo;
}
Also used : Binder(com.vaadin.data.Binder) ValidationException(com.vaadin.data.ValidationException) Button(com.vaadin.ui.Button) StringToDoubleConverter(com.vaadin.data.converter.StringToDoubleConverter) VerticalLayout(com.vaadin.ui.VerticalLayout) TextField(com.vaadin.ui.TextField) Component(com.vaadin.ui.Component) HorizontalLayout(com.vaadin.ui.HorizontalLayout)

Aggregations

Binder (com.vaadin.data.Binder)1 ValidationException (com.vaadin.data.ValidationException)1 StringToDoubleConverter (com.vaadin.data.converter.StringToDoubleConverter)1 Button (com.vaadin.ui.Button)1 Component (com.vaadin.ui.Component)1 HorizontalLayout (com.vaadin.ui.HorizontalLayout)1 TextField (com.vaadin.ui.TextField)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1