Hallo Leute.
Ich habe hier eine Tableview, in dieser soll die Klasse Auto Angezeigt werden . Zum späteren eindeutigen Identifizieren des Objektes lasse ich ein Zähler intern hochzählen. Auch dieser Zähler soll in der Tableview Angezeigt werden (Wird es dann aber nicht ). Bei der Ausgabe über Sysout klappt es nur nicht beim anzeigen im Tableview.
Schon mal vielen Dank für eure Hilfe.
Hier mal der Code dazu
Controllerklasse:
Autoklasse:
Exception:
Ich habe hier eine Tableview, in dieser soll die Klasse Auto Angezeigt werden . Zum späteren eindeutigen Identifizieren des Objektes lasse ich ein Zähler intern hochzählen. Auch dieser Zähler soll in der Tableview Angezeigt werden (Wird es dann aber nicht ). Bei der Ausgabe über Sysout klappt es nur nicht beim anzeigen im Tableview.
Schon mal vielen Dank für eure Hilfe.
Hier mal der Code dazu
Controllerklasse:
Java:
package application;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class DemoControll implements Initializable {
// Initzialisiere Objekt vom Typ Auto
Auto A1;
// Arraylist erstellen vom Typ Auto
ArrayList<Auto> autoListe = new ArrayList<Auto>();
// Observablelist
ObservableList<Auto> obAuto = FXCollections.observableArrayList();
// Variablen in der GUI
@FXML
private Button btnAdd;
@FXML
private TextField tf_hersteller;
@FXML
private TextField tf_typ;
@FXML
private TextField tf_menge;
@FXML
private TextField tf_preis;
@FXML
private TableView<Auto> tvAuto;
@FXML
private TableColumn<Auto, String> col_hersteller;
@FXML
private TableColumn<Auto, String> col_typ;
@FXML
private TableColumn<Auto, Double> col_preis;
@FXML
private TableColumn<Auto, Integer> col_menge;
@FXML
private TableColumn<Auto, Integer> col_id;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// Daten in Col einfügen
col_hersteller.setCellValueFactory(new PropertyValueFactory<Auto, String>("Hersteller"));
col_typ.setCellValueFactory(new PropertyValueFactory<Auto, String>("Typ"));
col_menge.setCellValueFactory(new PropertyValueFactory<Auto, Integer>("Menge"));
col_preis.setCellValueFactory(new PropertyValueFactory<Auto, Double>("Preis"));
col_id.setCellValueFactory(new PropertyValueFactory<Auto, Integer>("ID"));
// Observablelist in Tableview übergeben
tvAuto.setItems(obAuto);
}
int autoid = 0;
// Button ADD Erzeuge Objekte, fülle Arraliste
@FXML
void btnAddObject(ActionEvent event) {
autoid++;
A1 = new Auto();
A1.setHersteller(tf_hersteller.getText());
A1.setTyp(tf_typ.getText());
A1.setMenge(Integer.parseInt(tf_menge.getText()));
A1.setPreis(Double.parseDouble(tf_preis.getText()));
A1.setId(0);
autoid = autoid + A1.getId();
A1.setId(autoid);
autoListe.add(A1);
obAuto.setAll(autoListe);
System.out.println(A1.getHersteller());
System.out.println(A1.getTyp());
System.out.println(A1.getMenge());
System.out.println(A1.getPreis());
System.out.println(A1.getId());
}
}
Autoklasse:
Java:
package application;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
public class DemoControll implements Initializable {
// Initzialisiere Objekt vom Typ Auto
Auto A1;
// Arraylist erstellen vom Typ Auto
ArrayList<Auto> autoListe = new ArrayList<Auto>();
// Observablelist
ObservableList<Auto> obAuto = FXCollections.observableArrayList();
// Variablen in der GUI
@FXML
private Button btnAdd;
@FXML
private TextField tf_hersteller;
@FXML
private TextField tf_typ;
@FXML
private TextField tf_menge;
@FXML
private TextField tf_preis;
@FXML
private TableView<Auto> tvAuto;
@FXML
private TableColumn<Auto, String> col_hersteller;
@FXML
private TableColumn<Auto, String> col_typ;
@FXML
private TableColumn<Auto, Double> col_preis;
@FXML
private TableColumn<Auto, Integer> col_menge;
@FXML
private TableColumn<Auto, Integer> col_id;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// Daten in Col einfügen
col_hersteller.setCellValueFactory(new PropertyValueFactory<Auto, String>("Hersteller"));
col_typ.setCellValueFactory(new PropertyValueFactory<Auto, String>("Typ"));
col_menge.setCellValueFactory(new PropertyValueFactory<Auto, Integer>("Menge"));
col_preis.setCellValueFactory(new PropertyValueFactory<Auto, Double>("Preis"));
col_id.setCellValueFactory(new PropertyValueFactory<Auto, Integer>("ID"));
// Observablelist in Tableview übergeben
tvAuto.setItems(obAuto);
}
int autoid = 0;
// Button ADD Erzeuge Objekte, fülle Arraliste
@FXML
void btnAddObject(ActionEvent event) {
autoid++;
A1 = new Auto();
A1.setHersteller(tf_hersteller.getText());
A1.setTyp(tf_typ.getText());
A1.setMenge(Integer.parseInt(tf_menge.getText()));
A1.setPreis(Double.parseDouble(tf_preis.getText()));
A1.setId(0);
autoid = autoid + A1.getId();
A1.setId(autoid);
autoListe.add(A1);
obAuto.setAll(autoListe);
System.out.println(A1.getHersteller());
System.out.println(A1.getTyp());
System.out.println(A1.getMenge());
System.out.println(A1.getPreis());
System.out.println(A1.getId());
}
}
Exception:
Java:
Nov. 05, 2018 6:10:51 NACHM. javafx.scene.control.cell.PropertyValueFactory getCellDataReflectively
WARNING: Can not retrieve property 'ID' in PropertyValueFactory: javafx.scene.control.cell.PropertyValueFactory@1d65ec52 with provided class type: class application.Auto
java.lang.IllegalStateException: Cannot read from unreadable property ID
at javafx.base/com.sun.javafx.property.PropertyReference.get(Unknown Source)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.getCellDataReflectively(Unknown Source)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(Unknown Source)
at javafx.controls/javafx.scene.control.cell.PropertyValueFactory.call(Unknown Source)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(Unknown Source)
at javafx.controls/javafx.scene.control.TableColumn.getCellObservableValue(Unknown Source)
at javafx.controls/javafx.scene.control.TableCell.updateItem(Unknown Source)
at javafx.controls/javafx.scene.control.TableCell.indexChanged(Unknown Source)
at javafx.controls/javafx.scene.control.IndexedCell.updateIndex(Unknown Source)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.updateCells(Unknown Source)
at javafx.controls/javafx.scene.control.skin.TableRowSkinBase.<init>(Unknown Source)
at javafx.controls/javafx.scene.control.skin.TableRowSkin.<init>(Unknown Source)
at javafx.controls/javafx.scene.control.TableRow.createDefaultSkin(Unknown Source)
at javafx.controls/javafx.scene.control.Control.doProcessCSS(Unknown Source)
at javafx.controls/javafx.scene.control.Control.access$000(Unknown Source)
at javafx.controls/javafx.scene.control.Control$1.doProcessCSS(Unknown Source)
at javafx.controls/com.sun.javafx.scene.control.ControlHelper.processCSSImpl(Unknown Source)
at javafx.graphics/com.sun.javafx.scene.NodeHelper.processCSS(Unknown Source)
at javafx.graphics/javafx.scene.Node.processCSS(Unknown Source)
at javafx.graphics/javafx.scene.Node.applyCss(Unknown Source)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.setCellIndex(Unknown Source)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCell(Unknown Source)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.getCellLength(Unknown Source)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.computeViewportOffset(Unknown Source)
at javafx.controls/javafx.scene.control.skin.VirtualFlow.layoutChildren(Unknown Source)
at javafx.graphics/javafx.scene.Parent.layout(Unknown Source)
at javafx.graphics/javafx.scene.Parent.layout(Unknown Source)
at javafx.graphics/javafx.scene.Parent.layout(Unknown Source)
at javafx.graphics/javafx.scene.Scene.doLayoutPass(Unknown Source)
at javafx.graphics/javafx.scene.Scene$ScenePulseListener.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.Toolkit.lambda$runPulse$2(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.Toolkit.runPulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.Toolkit.firePulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)