Reputation: 1
I wanna make a Table and a Linechart with with the Data from the Table. I already Createt a Table and a Linechart, but i dont know how to combine them. Thats how my table looks, but i need a Linechart with the data from the Observablelist. I need to know how i can add two Datas from the columns from my Table into my Linechart.
package application;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class Tabelle extends Application {
private TableView<Werte> tView = new TableView<Werte>(); //Tabelle wird erstellt mit dem Datentyp "Werte"
@SuppressWarnings("unchecked")
@Override
public void start(Stage stage) {
stage.setTitle("GPS-Maus"); //Titel="GPS-Maus"
stage.setWidth(800); //Breite des Fensters auf 800
stage.setHeight(300); //Höhe des Fensters auf 300
tView.setEditable(true);
//Columns werden erstellt
//Quality Column
TableColumn<Werte, Integer> qualityCol = new TableColumn<Werte, Integer>("quality");
qualityCol.setCellValueFactory((p) -> {
return p.getValue().qualityProperty().asObject();
});
qualityCol.prefWidthProperty().bind(tView.widthProperty().multiply(0.2));
//numberOfSatellites Column
TableColumn<Werte, Integer> numberOfSatellitesCol = new TableColumn<Werte, Integer>("numberOfSatellites");
numberOfSatellitesCol.setCellValueFactory((p) -> {
return p.getValue().numberOfSatellitesProperty().asObject();
});
numberOfSatellitesCol.prefWidthProperty().bind(tView.widthProperty().multiply(0.2));
//Latitude Column
TableColumn<Werte, Double> latitudeCol = new TableColumn<Werte, Double>("latitude");
latitudeCol.setCellValueFactory((p) -> {
return p.getValue().latitudeProperty().asObject();
});
latitudeCol.prefWidthProperty().bind(tView.widthProperty().multiply(0.3));
//Altitude Column
TableColumn<Werte, Double> altitudeCol = new TableColumn<Werte, Double>("altitude");
altitudeCol.setCellValueFactory((p) -> {
return p.getValue().altitudeProperty().asObject();
});
altitudeCol.prefWidthProperty().bind(tView.widthProperty().multiply(0.3));
//Longitude Column
TableColumn<Werte, Double> longitudeCol = new TableColumn<Werte, Double>("longitude");
longitudeCol.setCellValueFactory((p) -> {
return p.getValue().longitudeProperty().asObject();
});
longitudeCol.prefWidthProperty().bind(tView.widthProperty().multiply(0.3));
//a Column
TableColumn<Werte, Double> aCol = new TableColumn<Werte, Double>("a");
aCol.setCellValueFactory((p) -> {
return p.getValue().aProperty().asObject();
});
aCol.prefWidthProperty().bind(tView.widthProperty().multiply(0.3));
//Die Header werden definiert
tView.getColumns().addAll(qualityCol, numberOfSatellitesCol, latitudeCol, altitudeCol, longitudeCol, aCol);
tView.setItems(inhaltListe()); //Den Zellen werden die Werte von der erstellten ObservableList übergeben
Scene scene = new Scene(tView); //Scene ist der Container
stage.setScene(scene); //stage ist der top level JavaFX container
stage.show();
}
private ObservableList<Werte> inhaltListe() { // Methode wird erstellt, welche eine ObservableList zurück erwartet. Name der Methode ist "inhaltListe"
final ObservableList<Werte> werte = FXCollections.observableArrayList( //ObservableList wird erstellt um den Attributen aus dem Object "Werte" Werte zuzuordnen. Name ist "werte". ObservableList die hier verwendet wird, ist observableArrayList, da sich dieser am besten für Objects anbietet.
new Werte(1, 2, 3, 4, 5, 6), //"Werte" wird mit Beispielzahlen gefüllt
new Werte(7, 8, 9, 10, 11, 12),
new Werte(13, 14, 15, 16, 17, 18),
new Werte(19, 20, 21, 22, 23, 24),
new Werte(25, 26, 27, 28, 29, 30));
return werte;
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: -1
Views: 216
Reputation: 198
You need to create two NumberAxis, one for X and other for Y axis, and then an XYChart.Series object.
Take a look at the oracle oficial tutorial on line charts: https://docs.oracle.com/javafx/2/charts/line-chart.htm
Upvotes: 0