multithreadingjava-8vaadin7

the changes are not visible in window for vaadin 7


I have a window in vaadin 7, and I would like to show changes as there are made, because I have longer task working its think. I stumble upon this site but I have to move the window to see the changes. In the window that I have, I call next class with :

new PushyUI();

the class called:

public class PushyUI extends UI {
    Chart chart = new Chart(ChartType.AREASPLINE);
    DataSeries series = new DataSeries();

    PushyUI() {
        chart.setSizeFull();
        setContent(chart);

        // Prepare the data display
        Configuration conf = chart.getConfiguration();
        conf.setTitle("Hot New Data");
        conf.setSeries(series);

        // Start the data feed thread
        new FeederThread().start();
    }

    class FeederThread extends Thread {
        int count = 0;

        @Override
        public void run() {
            try {
                // Update the data for a while
                while (count < 6) {
                    Thread.sleep(1000);

                    getUI().access(new Runnable() {
                        @Override
                        public void run() {
                            double y = Math.random();
                            series.add(new DataSeriesItem(count++, y),
                                    true, count > 10);
                        }
                    });
                }

                // Inform that we have stopped running
                getUI().access(new Runnable() {
                    @Override
                    public void run() {
                        setContent(new Label("Done!"));
                    }
                });
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
   }

How could I see the changes without have to moving the window around ? Thanks


Solution

  • I found the solution here . I manage to modified so it works for me.

                layout = new HorizontalLayout();
                layout.setSpacing(true);
                layout.setSizeFull();
         
                // Add a normal progress bar
                sample = new ProgressBar();
                layout.addComponent(sample);
                layout.setComponentAlignment(sample, Alignment.MIDDLE_CENTER);
         
                startButton = new Button("Start", new Button.ClickListener() {
                    @Override
                    public void buttonClick(final ClickEvent event) {
                        sample.setValue(0f);
                        sample.setVisible(true);
                        startButton.setEnabled(false);
                        UI.getCurrent().setPollInterval(500);
         
                        launchProgressUpdater(UI.getCurrent(), 10);
                    }
                });
                startButton.setStyleName(ValoTheme.BUTTON_SMALL);
                layout.addComponent(startButton);
                layout.setComponentAlignment(startButton, Alignment.MIDDLE_CENTER);
         
        ...
         
            private void launchProgressUpdater(UI ui, int maxProgress) {
                new Thread(() -> {
                    for (int progress = 1; progress <= maxProgress; progress++) {
                        try {
                            Thread.sleep(1000);
                        } catch (final InterruptedException e) {
                            throw new RuntimeException("Unexpected interruption", e);
                        }
                        updateProgressBar(ui, progress, maxProgress);
                    }
                }).start();
            }
         
            private void updateProgressBar(UI ui, int progress, int maxProgress) {
                ui.access(() -> {
                    final float newValue;
                    if (progress == maxProgress) {
                        ui.setPollInterval(-1);
                        startButton.setEnabled(true);
                        newValue = 0f;
                        sample.setVisible(!sample.isIndeterminate());
                    } else {
                        newValue = (float) progress / maxProgress;
                    }
                    sample.setValue(newValue);
                    Notification.show("Value changed:", Float.toString(newValue), Notification.Type.TRAY_NOTIFICATION);
                });
            }