我想在javafx中创建一个tableview,它可以实时向我显示添加的行,我的目标是编写一个程序,它可以逐个导入表视图中的行,我尝试下面的代码:
public class Controller implements Initializable {
@FXML
private TableView<Logs> table;
@FXML
private TableColumn<Logs, Integer> numCol;
@FXML
private TableColumn<Logs, Integer> nameCol;
@FXML
private TableColumn<Logs, String> familyCol;
@FXML
private TableColumn<Logs, String> JobCol;
private int num = 1;
private List<Logs> listLogs = new ArrayList();
private ObservableList<Logs> observableListlogs;
public Controller() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
numCol.setCellValueFactory(new PropertyValueFactory<Logs, Integer>("num"));
nameCol.setCellValueFactory(new PropertyValueFactory<Logs, Integer>("name"));
familyCol.setCellValueFactory(new PropertyValueFactory<Logs, String>("family"));
jobCol.setCellValueFactory(new PropertyValueFactory<Logs, String>("job"));
observableListlogs = FXCollections.observableArrayList(listLogs);
table.setItems(observableListlogs);
}
@FXML
private void captureEvents() {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"C:\\Users\\a\\Desktop\\users\"&& user.exe");
builder.redirectErrorStream(true);
try {
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line ;
int lineNumber=1;
while (true ) {
line = r.readLine();
if (line==null)
break;
parseLine(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void parseLine(String line) {
Pattern p = Pattern.compile("<name>(.*?)</name><family>(.*?)</family><Job>(.*?)</Job>
Pattern p1 = Pattern.compile("<exceed>(.*?)</exceed>");
Matcher m = p.matcher(line);
Matcher m1 = p1.matcher(line);
if (m.find()) {
try {
Logs log = new Logs(num++, m.group(1).toString(),
m.group(2).toString(), m.group(3).toString());
observableListlogs.add(log));
} catch (Exception e) {
e.printStackTrace();
}
} else
if (m1.find()) {
try{
Logs log = new Logs(num++, m1.group(1));
observableListlogs.add(log));
}catch (Exception e){
e.printStackTrace();
}
}
}
}
但遗憾的是我没有看到tableView的内容,直到一些异常(我自己创建,我可以看到tableView的内容,应用程序被锁定,直到异常发生)发生(我的意思是它没有当行添加到表中时,一个接一个地向我显示,异常发生后它突然显示了表视图的所有内容,我需要实时向我显示内容) 我该怎么做才能在表格视图中看到即时更改?
答案 0 :(得分:0)
通过执行以下操作来阻止主线程:
while (true ) {
line = r.readLine();
if (line==null)
break;
parseLine(line);
}
主线程(或UI-Thread)用于呈现UI。如果你通过一些繁重的计算或IO阻止它,你会遇到framedrops甚至像你的情况一样冻结。您需要在后台线程上执行IO代码,以便主线程可以自由执行UI更新。
作为一个简单的解决方法,现在可以将代码放入一个新线程并启动它:
@FXML
private void captureEvents() {
Thread thread = new Thread(() -> {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"C:\\Users\\a\\Desktop\\users\"&& user.exe");
builder.redirectErrorStream(true);
try {
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line ;
int lineNumber=1;
while (true ) {
line = r.readLine();
if (line==null)
break;
parseLine(line);
}
} catch (Exception e) {
e.printStackTrace();
}
});
thread.start();
}
这将启动一个新的后台线程,它将执行您的任务。您可能希望将此任务作为某种工作线程执行,但作为一种快速解决方案,这应该可以解决您的问题。