【问题标题】:the changes are not visible in window for vaadin 7这些更改在 vaadin 7 的窗口中不可见
【发布时间】:2022-01-04 06:04:19
【问题描述】:

我在 vaadin 7 中有一个窗口,我想显示所做的更改,因为我有更长的任务来处理它的想法。我偶然发现了this site,但我必须移动窗口才能看到更改。在我拥有的窗口中,我调用下一节课:

new PushyUI();

类调用:

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();
                }
            }
        }
   }

如何在不移动窗口的情况下看到更改?谢谢

【问题讨论】:

  • 我认为您需要展示如何启用服务器推送,您上面的代码没有显示注释或部署描述符。
  • 查看此示例应用程序以获得最佳实践 Push 使用 Vaadin 7 github.com/TatuLund/bookstore-v7
  • 我没有启用服务器推送。我有没有办法?

标签: multithreading java-8 vaadin7


【解决方案1】:

我找到了解决方案 here 。我设法进行了修改,所以它对我有用。

            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);
            });
        }

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
猜你喜欢
  • 2011-05-28
  • 1970-01-01
  • 2014-09-06
  • 2017-06-29
  • 1970-01-01
  • 2021-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多