如何使用通过以太网在另一个线程中接收的数据更新javaFX表

如何使用通过以太网在另一个线程中接收的数据更新javaFX表,java,javafx,Java,Javafx,我想知道如何每1秒用socketDataReceiver线程中通过网络接收的数据更新JavaFX表,并将该数据放入HashMap中。当数据放入HashMap(定期更新)时,我希望javaFX表使用城市Id、纬度和经度信息进行更新。下面是示例代码 GPSLocation.java public class GPSLocation { public Short cityId; public Double lat; public Double lon; public

我想知道如何每1秒用socketDataReceiver线程中通过网络接收的数据更新JavaFX表,并将该数据放入HashMap中。当数据放入HashMap(定期更新)时,我希望javaFX表使用城市Id、纬度和经度信息进行更新。下面是示例代码

GPSLocation.java

public class GPSLocation {

    public Short cityId;
    public Double lat;
    public Double lon;

    public GPSLocation(short cityId, double lat, double lon) {
        this.cityId = cityId;
        this.lat = lat;
        this.lon = lon;
    }
}
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CityCentreLocation extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        TableView tableView = new TableView();


        TableColumn<Short, GPSLocation> column1 = new TableColumn<>("City_Id");
        column1.setCellValueFactory(new PropertyValueFactory<>("cityId"));

        TableColumn<Double, GPSLocation> column2 = new TableColumn<>("Latitude");
        column2.setCellValueFactory(new PropertyValueFactory<>("lat"));


        TableColumn<Double, GPSLocation> column3 = new TableColumn<>("Longitude");
        column3.setCellValueFactory(new PropertyValueFactory<>("lon"));


        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);
        tableView.getColumns().add(column3);

        VBox vbox = new VBox(tableView);

        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);

        primaryStage.show();
    }

}
Map<Short, GPSLocation> cityLocations = new HashMap<Short, GPSLocation>();

...
//Received socket data is present in buff of length 18

objGPSLocation.cityId = ByteBuffer.wrap(buff, 0, 2).order(ByteOrder.BIG_ENDIAN).getShort();    
objGPSLocation.lat = ByteBuffer.wrap(buff, 2, 8).order(ByteOrder.BIG_ENDIAN).getDouble();
objGPSLocation.lon = ByteBuffer.wrap(buff, 10, 8).order(ByteOrder.BIG_ENDIAN).getDouble();

cityLocations .put(objGPSLocation.cityId, objGPSLocation); 

...
CityCentreLocation.java

public class GPSLocation {

    public Short cityId;
    public Double lat;
    public Double lon;

    public GPSLocation(short cityId, double lat, double lon) {
        this.cityId = cityId;
        this.lat = lat;
        this.lon = lon;
    }
}
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CityCentreLocation extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        TableView tableView = new TableView();


        TableColumn<Short, GPSLocation> column1 = new TableColumn<>("City_Id");
        column1.setCellValueFactory(new PropertyValueFactory<>("cityId"));

        TableColumn<Double, GPSLocation> column2 = new TableColumn<>("Latitude");
        column2.setCellValueFactory(new PropertyValueFactory<>("lat"));


        TableColumn<Double, GPSLocation> column3 = new TableColumn<>("Longitude");
        column3.setCellValueFactory(new PropertyValueFactory<>("lon"));


        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);
        tableView.getColumns().add(column3);

        VBox vbox = new VBox(tableView);

        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);

        primaryStage.show();
    }

}
Map<Short, GPSLocation> cityLocations = new HashMap<Short, GPSLocation>();

...
//Received socket data is present in buff of length 18

objGPSLocation.cityId = ByteBuffer.wrap(buff, 0, 2).order(ByteOrder.BIG_ENDIAN).getShort();    
objGPSLocation.lat = ByteBuffer.wrap(buff, 2, 8).order(ByteOrder.BIG_ENDIAN).getDouble();
objGPSLocation.lon = ByteBuffer.wrap(buff, 10, 8).order(ByteOrder.BIG_ENDIAN).getDouble();

cityLocations .put(objGPSLocation.cityId, objGPSLocation); 

...
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类CityCenter扩展了应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
TableView TableView=新建TableView();
TableColumn column1=新的TableColumn(“城市Id”);
列1.setCellValueFactory(新属性ValueFactory(“cityId”);
TableColumn column2=新的TableColumn(“纬度”);
第2列:setCellValueFactory(新属性ValueFactory(“lat”);
TableColumn column3=新的TableColumn(“经度”);
第3列:setCellValueFactory(新属性ValueFactory(“lon”);
tableView.getColumns().add(column1);
tableView.getColumns().add(column2);
tableView.getColumns().add(column3);
VBox VBox=新的VBox(tableView);
场景=新场景(vbox);
初级阶段。场景(场景);
primaryStage.show();
}
}
ReceiverThread.java

public class GPSLocation {

    public Short cityId;
    public Double lat;
    public Double lon;

    public GPSLocation(short cityId, double lat, double lon) {
        this.cityId = cityId;
        this.lat = lat;
        this.lon = lon;
    }
}
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CityCentreLocation extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        TableView tableView = new TableView();


        TableColumn<Short, GPSLocation> column1 = new TableColumn<>("City_Id");
        column1.setCellValueFactory(new PropertyValueFactory<>("cityId"));

        TableColumn<Double, GPSLocation> column2 = new TableColumn<>("Latitude");
        column2.setCellValueFactory(new PropertyValueFactory<>("lat"));


        TableColumn<Double, GPSLocation> column3 = new TableColumn<>("Longitude");
        column3.setCellValueFactory(new PropertyValueFactory<>("lon"));


        tableView.getColumns().add(column1);
        tableView.getColumns().add(column2);
        tableView.getColumns().add(column3);

        VBox vbox = new VBox(tableView);

        Scene scene = new Scene(vbox);

        primaryStage.setScene(scene);

        primaryStage.show();
    }

}
Map<Short, GPSLocation> cityLocations = new HashMap<Short, GPSLocation>();

...
//Received socket data is present in buff of length 18

objGPSLocation.cityId = ByteBuffer.wrap(buff, 0, 2).order(ByteOrder.BIG_ENDIAN).getShort();    
objGPSLocation.lat = ByteBuffer.wrap(buff, 2, 8).order(ByteOrder.BIG_ENDIAN).getDouble();
objGPSLocation.lon = ByteBuffer.wrap(buff, 10, 8).order(ByteOrder.BIG_ENDIAN).getDouble();

cityLocations .put(objGPSLocation.cityId, objGPSLocation); 

...
Map cityLocations=newhashmap();
...
//接收到的套接字数据以长度为18的buff显示
objGPSLocation.cityId=ByteBuffer.wrap(buff,0,2).order(ByteOrder.BIG_ENDIAN).getShort();
objGPSLocation.lat=ByteBuffer.wrap(buff,2,8).order(ByteOrder.BIG_ENDIAN).getDouble();
objGPSLocation.lon=ByteBuffer.wrap(buff,10,8).order(ByteOrder.BIG_ENDIAN).getDouble();
cityLocations.put(objGPSLocation.cityId,objGPSLocation);
...

现在我想在javaFX表中填充这个地图数据。

对于您手头的任务,没有一个正确的答案

最简单、快速和肮脏的方法是从表中获取项目,将此
observeList的引用传递给接收方线程。当接收者线程接收到一个项目时,您可以将其添加到列表中。但您必须在JavaFX平台线程中执行此操作

在应用程序类中:

ObservableList<GPSLocation> items = tableView.getItems();
ReceiverThread receiver = new ReceiverThread(items);
receiver.start()
...
这基本上可以解决问题


不过它不是很干净。您的接收者不应该知道与UI相关的东西,比如
observeList
平台
。您的接收者实际上应该将新位置推送到某种接口或队列中。这只是让您开始。

到底是什么问题?@Kleopatra如何在javaFX table中显示HashMap集合数据
TableColumn
s的类型参数应该是相反的。第一个是
TableView
项目类型,第二个是此列单元格中显示的类型。如果您没有将原始类型用于
TableView
,编译器会告诉您的。。。