JavaFX8 TableView未使用预定服务

时间:2017-07-23 20:56:11

标签: java spring javafx resttemplate scheduledexecutorservice

我是Java和JavaFX的新手,我正在开发一个JavaFX应用程序,并在javafx.concurrent.ScheduledService任务中使用REST api来更新TableView。我能够从SpringBoot REST服务获得响应,并且可以看到数据是在Model对象,ObservableList以及TableView本身上设置的,但Table仍然是空的。

SpringBoot Api端点:

 @RequestMapping(value = "/getAllActive", method = RequestMethod.GET)
public List<IssuedTicket> getAllActive () {
    List<IssuedTicket> issuedTicketList = issuedTicketService.findAll();
    return issuedTicketList;
}

来自EndPoint的JSON响应:

[{"id":2,"ticketId":1230717013545,"dateArrived":"23-07-17","timeArrived":"01:35:45","deviceId":1},{"id":3,"ticketId":1230717013552,"dateArrived":"23-07-17","timeArrived":"01:35:52","deviceId":1},{"id":4,"ticketId":1230717013556,"dateArrived":"23-07-17","timeArrived":"01:35:56","deviceId":1}]

在我的JavaFX应用程序中:

HomeSceneController.java

package com.ronintech.bayTrans.ui.main;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import com.ronintech.bayTrans.model.ActiveTickets;
import com.ronintech.bayTrans.utils.RestErrorHandler;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class HomeSceneController implements Initializable{

@FXML
private AnchorPane homeAnchorPane;

@FXML
private JFXTextField ticketTxt;

@FXML
private JFXButton scanTicketBtn;

@FXML
private TableView<ActiveTickets> activeTicketsTable;

@FXML
private TableColumn<ActiveTickets, Long > ticketIdCol;

@FXML
private TableColumn<ActiveTickets, String > dateArrivedCol;

@FXML
private TableColumn<ActiveTickets, String> timeArrivedCol;

private ObservableList<ActiveTickets> activeTicketsList;

private static final Logger LOGGER = LoggerFactory.getLogger(HomeSceneController.class);
private static final String ACTIVE_TICKETS_URL = "http://localhost:9090/api/ticket/getAllActive";

@Override
public void initialize(URL location, ResourceBundle resources) {
    getActiveTickets.reset();
    getActiveTickets.setPeriod(Duration.seconds(20));
    getActiveTickets.start();

    getActiveTickets.setOnSucceeded(event -> {
        LOGGER.info("onSuccess");
        List<ActiveTickets> activeList = getActiveTickets.getValue();
        activeTicketsList = FXCollections.observableArrayList(activeList);

        ticketIdCol.setCellValueFactory(new PropertyValueFactory<ActiveTickets, Long>("ticketId"));
        dateArrivedCol.setCellValueFactory(new PropertyValueFactory<ActiveTickets, String>("dateArrived"));
        timeArrivedCol.setCellValueFactory(new PropertyValueFactory<ActiveTickets, String>("timeArrived"));
        activeTicketsTable.setItems(activeTicketsList);

        LOGGER.info("Items in Table");
        LOGGER.info(activeTicketsTable.getItems().toString());
    });

    getActiveTickets.setOnFailed(event -> {
        LOGGER.error("service task FAILED");
    });
}

@FXML
void openTicketModal(ActionEvent event) {
}

private ScheduledService<List<ActiveTickets>> getActiveTickets = new ScheduledService<List<ActiveTickets>>() {
    @Override
    protected Task<List<ActiveTickets>> createTask() {
        return new Task<List<ActiveTickets>>() {
            @Override
            protected List<ActiveTickets> call() throws Exception {
                LOGGER.info("Scheduled Service getActiveTickets STARTED");
                RestTemplate restTemplate = new RestTemplate();
                restTemplate.setErrorHandler(new RestErrorHandler());

                List<ActiveTickets> response = restTemplate.getForObject(ACTIVE_TICKETS_URL, List.class);

                LOGGER.info(response.toString);
                return response;
            }
        };
    }
};

}

我的模型对象: ActiveTickets.java

package com.ronintech.bayTrans.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ActiveTickets {

private SimpleLongProperty id;
private SimpleLongProperty ticketId;
private SimpleStringProperty dateArrived;
private SimpleStringProperty timeArrived;
private SimpleIntegerProperty deviceId;

public ActiveTickets(long id,
                     long ticketId,
                     String dateArrived,
                     String timeArrived,
                     int deviceId) {
    this.id = new SimpleLongProperty(id);
    this.ticketId = new SimpleLongProperty(ticketId);
    this.dateArrived = new SimpleStringProperty(dateArrived);
    this.timeArrived = new SimpleStringProperty(timeArrived);
    this.deviceId = new SimpleIntegerProperty(deviceId);
}

public long getId() {
    return id.get();
}

public SimpleLongProperty idProperty() {
    return id;
}

public long getTicketId() {
    return ticketId.get();
}

public SimpleLongProperty ticketIdProperty() {
    return ticketId;
}

public String getDateArrived() {
    return dateArrived.get();
}

public SimpleStringProperty dateArrivedProperty() {
    return dateArrived;
}

public String getTimeArrived() {
    return timeArrived.get();
}

public SimpleStringProperty timeArrivedProperty() {
    return timeArrived;
}

public int getDeviceId() {
    return deviceId.get();
}

public SimpleIntegerProperty deviceIdProperty() {
    return deviceId;
}

public void setId(long id) {
    this.id.set(id);
}

public void setTicketId(long ticketId) {
    this.ticketId.set(ticketId);
}

public void setDateArrived(String dateArrived) {
    this.dateArrived.set(dateArrived);
}

public void setTimeArrived(String timeArrived) {
    this.timeArrived.set(timeArrived);
}

public void setDeviceId(int deviceId) {
    this.deviceId.set(deviceId);
}

@Override
public String toString() {
    return "ActiveTickets{" +
            "ticketId=" + ticketId +
            ", dateArrived=" + dateArrived +
            ", timeArrived=" + timeArrived +
            '}';
}
}

RestTemplate调用的响应是:

00:43:06.901 [Thread-9] INFO com.ronintech.bayTrans.ui.main.HomeSceneController - [{id=2, ticketId=1230717013545, dateArrived=23-07-17, timeArrived=01:35:45, deviceId=1}, {id=3, ticketId=1230717013552, dateArrived=23-07-17, timeArrived=01:35:52, deviceId=1}, {id=4, ticketId=1230717013556, dateArrived=23-07-17, timeArrived=01:35:56, deviceId=1}]

将ObservableList设置为TableView后的日志:

activeTicketsTable.setItems(activeTicketsList);

00:43:07.159 [JavaFX Application Thread] INFO com.ronintech.bayTrans.ui.main.HomeSceneController - [{id=2, ticketId=1230717013545, dateArrived=23-07-17, timeArrived=01:35:45, deviceId=1}, {id=3, ticketId=1230717013552, dateArrived=23-07-17, timeArrived=01:35:52, deviceId=1}, {id=4, ticketId=1230717013556, dateArrived=23-07-17, timeArrived=01:35:56, deviceId=1}]

日志显示正在主UI线程上设置TableView数据,但表仍为空

TableView

我不确定我是否以及我做错了什么。如果有人知道如何解决这个问题,请帮助。

提前致谢。

修改

FXML中的我的TableView结构:

<TableView fx:id="activeTicketsTable" prefHeight="800.0" prefWidth="800.0" tableMenuButtonVisible="true">
    <columns>
        <TableColumn fx:id="ticketIdCol" prefWidth="75.0" text="Ticket ID"/>
        <TableColumn fx:id="dateArrivedCol" prefWidth="75.0" text="Date Arrived"/>
        <TableColumn fx:id="timeArrivedCol" prefWidth="75.0" text="Time Arrived"/>
    </columns>
    <columnResizePolicy>
        <TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
    </columnResizePolicy>
</TableView>

更新

似乎编组没有正确发生。

这是ObservableList activeTicketsList的调试跟踪:

activeTicketsList

List已被设置为LinkedHashMaps'

的ArrayList

而当我手动创建一个ObservableList:

private final ObservableList<ActiveTickets> data = FXCollections.observableArrayList(
        new ActiveTickets(1,1234,"date1","time1",1),
        new ActiveTickets(2,5678,"date2","time2",2)
);

“data”被正确设置为ActiveTickets类的对象的ArrayList

data

如何确保JSON - &gt;在RestTemplate中正确完成了对象封送?

1 个答案:

答案 0 :(得分:0)

我不确定这是否真的是问题,但通常我的REST客户端项目不会直接编组到列表中(这可能是杰克逊的问题)。这就是我要做的事情:

ActiveTickets[] response = restTemplate.getForObject(ACTIVE_TICKETS_URL, ActiveTickets[].class);

return Arrays.asList(response);

请注意,您仍会在端点返回一个列表;你不需要改变它。

更新

阅读this了解其他方法。