方法一: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]);
}
});