为什么我的JavaFX TableView不起作用?

时间:2017-11-27 10:11:08

标签: java javafx tableview

我有一个带有TableView的JavaFX应用程序。但是在折射器之后,它不想工作。我不明白为什么它不起作用。我在TableView中添加了一些来自mapMarkerData的数据。

LatLongMainApp.java

private ObservableList<MapMarker> mapMarkerData = FXCollections.observableArrayList();

private Stage stage;

/**
 * Constructeur de mapMarkerData
 */
public LatLongMainApp() {
}


/**
 * Démarre l'application
 */
@Override
public void start(Stage stage) throws Exception {

    FXMLLoader loader = new FXMLLoader();

    //Charge l'interface
    loader.setLocation(LatLongMainApp.class.getResource("view/Scene.fxml"));
    AnchorPane root = (AnchorPane) loader.load();

    Scene scene = new Scene(root);
    scene.getStylesheets().add("/styles/Styles.css");

    //Donne l'acces au donnés de l'interface au controller
    LatLongFXMLController controller = loader.getController();
    controller.setLatLongMainApp(this);

    stage.setTitle("Application");
    stage.setResizable(false);
    stage.setScene(scene);
    stage.getIcons().add(new Image("file:icone-app-48.png"));
    stage.show();
    this.stage = stage;
}

public ObservableList<MapMarker> getMapMarkerData() {
    return mapMarkerData;
}

/**
 * Returns the main stage.
 * @return
 */
public Stage getStage() {
    return stage;
}

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

控制器:

LatLongFXMLController.java

private TableView<MapMarker> mapMarkerTab;

@FXML
private TableColumn<MapMarker, String> nomMarqueurColumn;
@FXML
private TableColumn<ObjectProperty<Coordonnees>, Double> latitudeColumn;
@FXML
private TableColumn<ObjectProperty<Coordonnees>, Double> longitudeColumn;
@FXML
private TableColumn<MapMarker, String> timeColumn;

//other variables

@Override
public void initialize(URL url, ResourceBundle rb) {

    mapMarkerTab = new TableView<MapMarker>();
    nameMarkerColumn.setCellValueFactory(cellData -> cellData.getValue().getN_Marqueur());
    latitudeColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().getLatitude().asObject());
    longitudeColumn.setCellValueFactory(cellData -> cellData.getValue().getValue().getLongitude().asObject());
    timeColumn.setCellValueFactory(cellData -> cellData.getValue().getTimeNextStringPropertyTime());
    //Remplissages des textFields
    startTimeTextField.setText(TimeUtil.formatTime(MapMarker.getTimeStart()));
    startDateTextField.setText(TimeUtil.formatDate(MapMarker.getTimeStart()));

    logger.log(Level.INFO, "Démarage terminé");

}

//Many Methods

public void setLatLongMainApp(LatLongMainApp latLongMainApp) {
    this.latLongMainApp = latLongMainApp;

    //On ajoute la liste observable dans le tableau
    mapMarkerTab.setItems(latLongMainApp.getMapMarkerData());
}

和Model classe:

mapMarker.java

private StringProperty n_Marker;
private ObjectProperty<Coordonnees> coordonnees;
private Marker marker;
private MarkerOptions markerOptions;
private LocalDateTime timeNext;
private static LocalDateTime timeStart = LocalDateTime.now();
private static LocalDateTime currentTime;
//SecurityCheck est une variable qui est mis à 1 si il y a eu un problème de conversion.
private static int securityCheck = 0;

/**
 * Constructeur avec Paramêtre
 * @param intervalleCheckReset 
 */
MapMarker(String marqueur, Coordonnees coordonnees, Marker marker, int nbrMarker, int valueIntervalle){

    this.n_Marker = new SimpleStringProperty(marqueur);
    this.marker = marker;
    this.coordonnees = new SimpleObjectProperty<Coordonnees>(coordonnees);
    if(nbrMarker - 1 == 0) {
        this.timeNext = timeStart;
        currentTime = timeNext;
    }
    else {
        this.timeNext = currentTime.plusSeconds(valueIntervalle);
        currentTime = this.timeNext;
        }
}

这是我在FXML文件中的TableView:

<TableView fx:id="mapMarkerTab" layoutX="14.0" layoutY="14.0"
prefHeight="625.0" prefWidth="442.0" AnchorPane.bottomAnchor="50.0"
AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<columns>
    <TableColumn fx:id="nomMarqueurColumn" prefWidth="83.0"
        resizable="false" text="N*" />
    <TableColumn fx:id="latitudeColumn" minWidth="-1.0" prefWidth="130.0"
        resizable="false" text="Latitude" />
    <TableColumn fx:id="longitudeColumn" minWidth="-1.0" prefWidth="112.0"
        resizable="false" text="Longitude" />
    <TableColumn fx:id="timeColumn" minWidth="-1.0" prefWidth="116.0"
        resizable="false" text="Time" />
</columns>
<columnResizePolicy>
    <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>

我尝试了一些方法来解决这个问题,但我找到了解决办法。

编辑:我有Coordonnees.java

Coordonnees.java

private DoubleProperty latitude; //I think it's right for this class
private DoubleProperty longitude;

Coordonnees(double latitude, double longitude){
    this.latitude = new SimpleDoubleProperty(latitude);
    this.longitude = new SimpleDoubleProperty(longitude);
}

//Getters and Setters

我在我的TableView中添加我的数据:

Coordonnees coord = new Coordonnees(tempLatitude, tempLongitude);
MapMarker mapMarker = new MapMarker(name, coord, marker, nbrMarker, valueIntervalleInt);
map.addMarker(marker);
latLongMainApp.getMapMarkerData().add(mapMarker);

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,我在我的控制器类(LatLongFXMLController.java)中进行了更改,如下所示:

    @Override
public void initialize(URL url, ResourceBundle rb) {

    search.bind(searchTextField.textProperty());
    nomMarqueurColumn.setCellValueFactory(cellData -> cellData.getValue().getN_Marqueur());
    latitudeColumn.setCellValueFactory(cellData -> cellData.getValue().getCoordonnees().getLatitude().asObject());
    longitudeColumn.setCellValueFactory(cellData -> cellData.getValue().getCoordonnees().getLongitude().asObject());
    timeColumn.setCellValueFactory(cellData -> cellData.getValue().getTimeNextStringPropertyTime());
    //Remplissages des textFields
    startTimeTextField.setText(TimeUtil.formatTime(MapMarker.getTimeStart()));
    startDateTextField.setText(TimeUtil.formatDate(MapMarker.getTimeStart()));
}

在折射之后,我改变了我的变量纬度和经度的位置,这是因为它现在有效。 (但奇怪的是,该计划之前仍在运作。)