来自URL json的JavaFX Dynamic TableView [英] JavaFX Dynamic TableView from URL json

查看:208
本文介绍了来自URL json的JavaFX Dynamic TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个动态的tableview,它从网页提供的json中获取数据。问题是,只要来自json的数据超出范围(例如范围应该是0.8-0.9,但它读取1.1),该表会自动使用RED SQUARE图像更新observablelist。如果数据在范围内,则显示BLUE SQUARE图像。它就像一个状态指示器,以便用户知道数据是否正确。我有这个代码:

I want to create a dynamic tableview that gets its data from a json provided by a webpage. The thing is that whenever the data from the json is out of range (for example the range should be 0.8 - 0.9, but it reads 1.1), the table automatically updates the observablelist with a "RED SQUARE" image. If the data is within range, it shows a "BLUE SQUARE" image. It's like a status indicator so that the user knows if the data is correct or not. I have this code:

public ObservableList<PumpSites> list = FXCollections.observableArrayList(
        new PumpSites (blue or red square image, "Canduman"),
        new PumpSites (blue or red square image, "Cubacub"),
        new PumpSites (blue or red square image, "Liloan"),
        new PumpSites (blue or red square image, "Talamban"),
        new PumpSites (blue or red square image, "Tisa")
        );

status.setCellValueFactory(new PropertyValueFactory<PumpSites, String>("status"));
ps.setCellValueFactory(new PropertyValueFactory<PumpSites, String>("ps"));
table.setItems(list);

public class PumpSites {
private final SimpleStringProperty status;
private final SimpleStringProperty ps;

public PumpSites(String status, String ps){
    super();
    this.status = new SimpleStringProperty(status);
    this.ps = new SimpleStringProperty(ps);
}

public String getStatus() {
    return status.get();
}

public String getPs() {
    return ps.get();
}

}

我从中获取数据没有问题杰森。我打算在platform.runlater中动态读取状态指示器的数据,以便它始终更新。如何动态地在表格中的泵站点旁边显示蓝色或红色方块?

I have no problem getting data from the json. I am planning to put the dynamic reading of data for the status indicator inside a platform.runlater so that it updates always. How can I show a blue or red square beside the pump site in the table dynamically?

推荐答案

以下是一些想法和代码示例:

Here are some ideas and code samples:

(1)为表格视图的列单元格渲染状态(是或否) - 示例代码:

(1) Rendering a state (yes or no) for a table view's column cell - sample code:

DataClass 可以像这样定义 boolean 属性:

The DataClass can have a boolean property defined like this:

private SimpleBooleanProperty status;

可以使用状态的布尔属性值使用复选框呈现表格视图的单元格。以下是代码如何定义表视图的列:

The table view's cell can be rendered with the a check box using the status's boolean property values. Here is code how the table view's column may be defined:

TableColumn<DataClass, Boolean> statusCol = new TableColumn<>("Status");
statusCol.setCellValueFactory(new PropertyValueFactory<DataClass, Boolean>("priority"));
statusCol.setCellFactory(column -> {
    CheckBoxTableCell<DataClass, Boolean> cell = new CheckBoxTableCell<>();
    cell.setAlignment(Pos.CENTER);
    return cell;
});

因为我已经建议了一个彩色的盒子(使用像这样的形状类矩形来自 javafx.scene.shape 包)可以呈现而不是复选框。

As I have already suggested a colored box (using a shape class like Rectangle from javafx.scene.shape package) can be rendered instead of a check box.

(2)可以使用 java.util.Timer TimerTask 类定期安排和运行任务。在构建gui之后的主应用程序中,计时器可以按如下方式初始化:

(2) A java.util.Timer and TimerTask classes can be used to schedule and run a task periodically. In the main application after the gui is built the timer can be initialized as follows:

public class BuildMyAppGui {
    //...
    private void initiateTimer() {

            Timer timer = new Timer();
            long zeroDelay = 0L;
            long period = 60000L; // 60 * 1000 = 1 min

            // The timer runs once the first time
            // and subsequently the scheduled task every one minute
            timer.schedule(new DataChangedTask(), zeroDelay, period);
    }
    //...
}

计时器的任务类示例代码:

The timer's task class sample code:

public class DataChangedTask extends TimerTask {

    @Override
    public void run() {
        // Code checks if there is a data change and refreshes the table data.
        // This also refreshes the table column with check box -
        // as checked or un-checked depending on the true/false value
        // in the boolean property in DataClass.
    }
}

注意 DataChangedTask 类可以让构造函数接受数据引用并访问应用程序所需的其他引用。

Note the DataChangedTask class can have constructors take references to data and access other references as required by the app.

这篇关于来自URL json的JavaFX Dynamic TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆