javafx-实现滚动条动态显示内容两种方法

方法一:scrollpanel+vbox+label

private Service<String> service;
//vinsp 是vbox,sp是scrollpanel
service = new Service<String>() {
    @Override
    protected Task<String> createTask() {
        return new Task<String>() {
            @Override
            protected String call() throws Exception {

                while (goout) {
                    logstr=logstr+"\n"+unCompressUtils.logMessage + "\n";
                    Label label=new Label("11111");
                    label.setPrefWidth(200);
                    label.setPrefHeight(10);
                    Platform.runLater(() -> {
                        vinsp.getChildren().addAll(label);
                    });

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                return "success";
            }
        };
    }
};
service.start();
sp.setFitToWidth(true);
sp.setContent(vinsp);

在这里插入图片描述
方法二:使用javafx的listview+Task

        Task taskfenxi=new Task() {
            @Override
            protected Object call() throws Exception {

                for (int i = 0; i < re.size(); i++) {
                    countNow[0]++;
                    int finalCountNow = countNow[0];

                    fileList.add(re.get(i));

                    int finalI = i;

                    try {
                        FileInputStream is = new FileInputStream(re.get(finalI));

                        String contentLine = null;
                        int countLineNumber = 1;
                        final Boolean[] iadd = {false};

                        BufferedReader br = new BufferedReader(new InputStreamReader(is));
                        while (true) {
                            try {
                                if (!((contentLine = br.readLine()) != null)) break;
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            CausedbyCheck causedbyCheck = new CausedbyCheck(contentLine);
                            if (causedbyCheck.hasStr) {
                                causedbyCount.getAndIncrement();
                            }
                            ErrorCheck errorCheck = new ErrorCheck(contentLine);
                            if (errorCheck.hasStr) {
                                erroCount.getAndIncrement();
                            }
                            ExceptionCheck exceptionCheck = new ExceptionCheck(contentLine);
                            if (exceptionCheck.hasStr) {
                                exceptionCount.getAndIncrement();
                            }
                            FailCheck failCheck = new FailCheck(contentLine);
                            if (failCheck.hasStr) {
                                failCount.getAndIncrement();
                            }
                            SlowCheck slowCheck = new SlowCheck(contentLine);
                            if (slowCheck.hasStr) {
                                slowCount.getAndIncrement();
                            }

                            if (causedbyCheck.hasStr || errorCheck.hasStr || exceptionCheck.hasStr || failCheck.hasStr || slowCheck.hasStr) {
                                String finalContentLine = contentLine;

                                if (iadd[0] == false) {
                                    iadd[0] = true;
                                    updateValue(finalI + "---- " + "文件:" + re.get(finalI) + "\n");
                                }else{
                                    updateValue(finalI + "----" + finalContentLine);
                                }
                                updateMessage(countAll+"----"+new File(re.get(finalI)).getName()+"----"+finalCountNow+"----"+erroCount+"----"+exceptionCount+"----"+slowCount+"----"+failCount+"----"+causedbyCount);
                            }
                            countLineNumber++;
                        }
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                updateProgress(10,10);

                return null;
            }
        };
        new Thread(taskfenxi).start();

        processor.progressProperty().bind(taskfenxi.progressProperty());
        taskfenxi.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent event) {
                fenxi.setText("分析");
            }
        });
        taskfenxi.setOnRunning(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent event) {
                fenxi.setText("分析中");
            }
        });
        taskfenxi.valueProperty().addListener(new ChangeListener() {
            @Override
            public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                mainlv.getItems().add(newValue);
                mainlv.scrollTo(mainlv.getItems().size());
            }
        });
        taskfenxi.messageProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                String[] arratNewList = newValue.split("----");
                filename.setText("共计文件数:" + arratNewList[0]);
                filepath.setText("当前文件名:" + arratNewList[1]);
                filesize.setText("发现异常数:" + arratNewList[2]);
                resultLabel.setText("本次扫描共计:发现error:" + arratNewList[3] + ";发现exception:" + arratNewList[4] + ";发现slow:" + arratNewList[5] + ";发现fail:" + arratNewList[6] + ";发现causedbyCount:" + arratNewList[7]);
            }
        });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值