JavaFX TableView bleibt leer.

DefconDev

Bekanntes Mitglied
Hallo zusammen,

ich habe per SceneBuilder eine Tabelle angelegt, ohne Inhalt.

Jetzt möchte ich diese Tabelle mit Inhalt befüllen, nur die Daten aus meiner Klasse werden nicht übernommen.Lediglich Spalten und Zeilen werden angelegt aber die bleiben leer.

Das Bespiel ist dem Oracle Example nahezu identisch, aber irgendwas übersehe ich.

Java:
        @FXML TableView<Elo_Data> eloTableA;
	
	
	@FXML TableColumn<Elo_Data, String> playersColA;
	@FXML TableColumn<Elo_Data, String> playersEloColA;
	@FXML TableColumn<Elo_Data, String> playersNationEloColA;
	@FXML TableColumn<Elo_Data, String> playersMapEloColA;
	
    
	
	private final ObservableList<Elo_Data> data =
	        FXCollections.observableArrayList(
	            new Elo_Data("Jacob", "Smith", "jacob.smith@example.com","ggg"),
	            new Elo_Data("Isabella", "Johnson", "isabella.johnson@example.com","ggg"),
	            new Elo_Data("Ethan", "Williams", "ethan.williams@example.com","ggg"),
	            new Elo_Data("Emma", "Jones", "emma.jones@example.com","ggg"),
	            new Elo_Data("Michael", "Brown", "michael.brown@example.com","ggg")
	        );
	
	
	
	
	
	
	public void setTable(){
		eloTableA.setItems(data);
		System.out.println(data.size());
	}


Java:
public class Elo_Data {
	
	private SimpleStringProperty playersName;
    private SimpleStringProperty playersElo;
    private SimpleStringProperty playersNationElo;
    private SimpleStringProperty playersMapElo;
    
    public Elo_Data(String playersName, String playersElo, String playersNationElo, String playersMapElo){
    	this.playersName 		= new SimpleStringProperty(playersName);
    	this.playersElo 		= new SimpleStringProperty(playersElo);
    	this.playersNationElo 	= new SimpleStringProperty(playersNationElo);
    	this.playersMapElo 		= new SimpleStringProperty(playersMapElo);
    	
    	
    }

	public SimpleStringProperty getPlayersName() {
		return playersName;
	}

	public SimpleStringProperty getPlayersElo() {
		return playersElo;
	}

	public SimpleStringProperty getPlayersNationElo() {
		return playersNationElo;
	}

	public SimpleStringProperty getPlayersMapElo() {
		return playersMapElo;
	}

	public void setPlayersName(SimpleStringProperty playersName) {
		this.playersName = playersName;
	}

	public void setPlayersElo(SimpleStringProperty playersElo) {
		this.playersElo = playersElo;
	}

	public void setPlayersNationElo(SimpleStringProperty playersNationElo) {
		this.playersNationElo = playersNationElo;
	}

	public void setPlayersMapElo(SimpleStringProperty playersMapElo) {
		this.playersMapElo = playersMapElo;
	}

}
 
Also im Controller würde ich dir mal vorschlagen:
- Entweder du implementierst das Interface Initializeable (was aber aus der dev-Sicht deprecated ist)
- Oder du annotierst eine Methode (egal ob private oder public) mit @FXML und nennt sie initialize()

In dieser Methode setzt du mal deine Daten der TableView und am besten setzt du auch noch die CellValueFactory für jede einzelne Column, dann sollte das auch mit dem Anzeigen klappen.
 
Sorry dass ich mich jetzt erst melde.

Also die zwei Vorschläge hatte ich zuvor schon versucht, habe mir deine Ratschläge aus anderen Threads zu herzen genommen 🙂. Leider aber selbes Ergebnis. Die Spalten und Zeilen werden

Eigentlich soll die Tabelle auch erst befüllt werden wenn ich einen Button drücke.



Die obere Tableview sieht so aus wenn ich den Button klicke oder es per initialize() mache.

Zuvor sieht sie dann unbefüllt aus wie die untere.
 
Das Problem wird sein, dass du noch nicht die CellValueFactory (z.B. PropertyValueFactory) angegeben hast. Optional fehlt dann noch die CellFactory für die Darstellung (brauchst du aber eigentlich nur, wenn du nicht mit Strings arbeitest oder spezielle Tabellen-Zellen mit z.B. Buttons, etc benötigst).
 
Hi, habe das Projekt mal runtergebrochen aufs nötigste.

Java:
package tableview;


import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class MainController {
	
	@FXML TableView<Elo_Data> tableview;
	
	@FXML TableColumn<Elo_Data, String> col1;
	
	
	public final ObservableList<Elo_Data> data =
	        FXCollections.observableArrayList(
	        		new Elo_Data("Jacob", "Smith", "jacob.smith@example.com","ggg"),
		            new Elo_Data("Isabella", "Johnson", "isabella.johnson@example.com","ggg"),
		            new Elo_Data("Ethan", "Williams", "ethan.williams@example.com","ggg"),
		            new Elo_Data("Emma", "Jones", "emma.jones@example.com","ggg"),
		            new Elo_Data("Michael", "Brown", "michael.brown@example.com","ggg")
	            
	        );
	
	
	
	
	public void initialize(){
		System.out.println("dffff");
		
		setTable();
		col1.setCellValueFactory(
			    new PropertyValueFactory<Elo_Data,String>("firstName")
			);
	}
	
	public void setTable(){
		System.out.println(data.size());
		
		tableview.setItems(data);
		col1.setText("Test");
		
		System.out.println(data.size());
		System.out.println(tableview.visibleProperty());
		System.out.println(tableview.isVisible());
	}
	
	

}

Java:
package tableview;


import javafx.beans.property.SimpleStringProperty;

public class Elo_Data {
	
	private SimpleStringProperty playersName;
    private SimpleStringProperty playersElo;
    private SimpleStringProperty playersNationElo;
    private SimpleStringProperty playersMapElo;
    
    public Elo_Data(String playersName, String playersElo, String playersNationElo, String playersMapElo){
    	this.playersName 		= new SimpleStringProperty(playersName);
    	this.playersElo 		= new SimpleStringProperty(playersElo);
    	this.playersNationElo 	= new SimpleStringProperty(playersNationElo);
    	this.playersMapElo 		= new SimpleStringProperty(playersMapElo);
    	
    	
    }

	public SimpleStringProperty getPlayersName() {
		return playersName;
	}

	public SimpleStringProperty getPlayersElo() {
		return playersElo;
	}

	public SimpleStringProperty getPlayersNationElo() {
		return playersNationElo;
	}

	public SimpleStringProperty getPlayersMapElo() {
		return playersMapElo;
	}

	public void setPlayersName(SimpleStringProperty playersName) {
		this.playersName = playersName;
	}

	public void setPlayersElo(SimpleStringProperty playersElo) {
		this.playersElo = playersElo;
	}

	public void setPlayersNationElo(SimpleStringProperty playersNationElo) {
		this.playersNationElo = playersNationElo;
	}

	public void setPlayersMapElo(SimpleStringProperty playersMapElo) {
		this.playersMapElo = playersMapElo;
	}

}

Java:
package tableview;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class Main extends Application{
	
	@Override
	public void start(Stage stage) throws Exception {
		FXMLLoader loader = new FXMLLoader(getClass().getResource("/tableview.fxml"));
		Parent root = loader.load();
		
		Scene scene = new Scene(root);
		 
		stage.setScene(scene);
		stage.show();
	}
	  
	public static void main(String[] args) {
	 launch(args);
	}

}

Java:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tableview.MainController">
   <children>
      <TableView fx:id="tableview" layoutX="6.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columns>
          <TableColumn fx:id="col1" prefWidth="75.0" text="C1" />
          <TableColumn prefWidth="75.0" text="C2" />
            <TableColumn prefWidth="75.0" text="Column X" />
            <TableColumn prefWidth="75.0" text="Column X" />
            <TableColumn prefWidth="75.0" text="Column X" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
 
Du musst die PropertyValueFactory richtig verwenden. Hier gehts um den Namen!

Auszug:

Code:
A convenience implementation of the Callback interface, designed specifically for use within the TableColumn cell value factory. An example of how to use this class is:


 TableColumn<Person,String> firstNameCol = new TableColumn<Person,String>("First Name");
 firstNameCol.setCellValueFactory(new PropertyValueFactory<Person,String>("firstName"));
 

In this example, the "firstName" string is used as a reference to an assumed firstNameProperty() method in the Person class type (which is the class type of the TableView items list). Additionally, this method must return a Property instance. If a method meeting these requirements is found, then the TableCell is populated with this ObservableValue. In addition, the TableView will automatically add an observer to the returned value, such that any changes fired will be observed by the TableView, resulting in the cell immediately updating.

If no method matching this pattern exists, there is fall-through support for attempting to call get<property>() or is<property>() (that is, getFirstName() or isFirstName() in the example above). If a method matching this pattern exists, the value returned from this method is wrapped in a ReadOnlyObjectWrapper and returned to the TableCell. However, in this situation, this means that the TableCell will not be able to observe the ObservableValue for changes (as is the case in the first approach above).

Habs ausprobiert und es funktioniert.

Java:
package tableview;

import javafx.beans.property.SimpleStringProperty;

public class Elo_Data {

  private SimpleStringProperty playersName;
  private SimpleStringProperty playersElo;
  private SimpleStringProperty playersNationElo;
  private SimpleStringProperty playersMapElo;

  public Elo_Data(String playersName, String playersElo, String playersNationElo, String playersMapElo) {
    this.playersName = new SimpleStringProperty(playersName);
    this.playersElo = new SimpleStringProperty(playersElo);
    this.playersNationElo = new SimpleStringProperty(playersNationElo);
    this.playersMapElo = new SimpleStringProperty(playersMapElo);

  }

  public SimpleStringProperty playersNameProperty() {
    return playersName;
  }

  public SimpleStringProperty playersEloProperty() {
    return playersElo;
  }

  public SimpleStringProperty playersNationProperty() {
    return playersNationElo;
  }

  public SimpleStringProperty playersMapEloProperty() {
    return playersMapElo;
  }

  public void setPlayersName(SimpleStringProperty playersName) {
    this.playersName = playersName;
  }

  public void setPlayersElo(SimpleStringProperty playersElo) {
    this.playersElo = playersElo;
  }

  public void setPlayersNationElo(SimpleStringProperty playersNationElo) {
    this.playersNationElo = playersNationElo;
  }

  public void setPlayersMapElo(SimpleStringProperty playersMapElo) {
    this.playersMapElo = playersMapElo;
  }

}

Änderung im Controller:

Java:
col1.setCellValueFactory(new PropertyValueFactory<Elo_Data, String>("playersName"));

Ich persönlich bin ja der Fan von einem Callback und dann kann man das per Hand selektieren, aber jedem das seine.
 
Zuletzt bearbeitet:
Vielen Dank.

Das Detail hatte ich komplett übersehen. Dass der String == der Name der Referenz sein muss.

In erster Linie wollte ich nur das Oracle Beispiel umsetzen. Wie das mit den Callback umzusetzen ist, müsste ich mir erst mal anschauen.
 

Zurück
Oben