Table und Images

Hallo Miteinander

Ich habe ein minimales Problem, in einer Liste zwei Images ein String und ein Textfeld zum Anzeigen in einer Table

Leider bekomme ich vom Image nur die Objekt Referenz und nicht das Bild in die Table…

Sieht da irgendjemand den Fehler?

Die Liste:


import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;

public class KontaktListe {

	private int id;

	private SimpleObjectProperty<Image> auswahl;

	private SimpleStringProperty s1;

	private String s1_1;

	private SimpleObjectProperty<Image> löschen;

	private SimpleObjectProperty<TextField> s2;

	/**
	 * Default Konstruktor
	 */
	public KontaktListe() {
		auswahl = new SimpleObjectProperty<Image>();
		s1 = new SimpleStringProperty();
		löschen = new SimpleObjectProperty<Image>();
		s2 = new SimpleObjectProperty<TextField>();
	}

	/**
	 * Erweitert Konstruktor
	 */
	public KontaktListe(int id, Image auswahl, String s1, Image löschen) {
		setId(id);
		setAuswahl(auswahl);
		setS1(s1);
		setLöschen(löschen);
	}

	/**
	 * 
	 * @return id
	 */
	public int getId() {
		return id;
	}

	/**
	 * 
	 * @param id
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 
	 * @return icon auswahl
	 */
	public Image getAuswahl() {
		return auswahl.get();
	}

	/**
	 * 
	 * @param auswahl
	 */
	public void setAuswahl(Image auswahl) {
		this.auswahl.set(auswahl);
	}

	/**
	 * 
	 * @return String 1
	 */
	public String getS1() {
		return s1.get();
	}

	/**
	 * 
	 * @param s1
	 */
	public void setS1(String s1) {
		this.s1.set(s1);
	}

	/**
	 * 
	 * @param s1_1
	 */
	public void setS1_1(String s1_1) {
		this.s1_1 = s1_1;

		setS1(getS1() + "
" + this.s1_1);
	}

	/**
	 * 
	 * @return Icon löschen
	 */
	public Image getLöschen() {
		return löschen.get();
	}

	/**
	 * 
	 * @param löschen
	 */
	public void setLöschen(Image löschen) {
		this.löschen.set(löschen);
	}

	/**
	 * 
	 * @return String 2
	 */
	public TextField getS2() {
		return s2.get();
	}

	/**
	 * 
	 * @param s2
	 */
	public void setS2(TextField s2) {
		this.s2.set(s2);
	}
}

die Table:

	@FXML
	private TableView<KontaktListe> kontaktTable;

	@FXML
	private TableColumn<KontaktListe, Object> colAusw;

	@FXML
	private TableColumn<KontaktListe, ComboBox<String>> colBezeichnung;

	@FXML
	private TableColumn<KontaktListe, Object> colDelete;

	@FXML
	private TableColumn<KontaktListe, TextField> colEintrag;

	/**
	 * Wird aufgerufen sobald die view geladen ist
	 */
	@FXML
	private void initialize() {
		System.out.println("Kontaktverwaltung geladen");

		aenderungenTable();

		colAusw = new TableColumn<KontaktListe, Object>(null);
		colAusw.setCellValueFactory(new PropertyValueFactory<KontaktListe, Object>("auswahl"));
		colAusw.setMaxWidth(50);
		
		colBezeichnung = new TableColumn<KontaktListe, ComboBox<String>>();
		colBezeichnung.setCellValueFactory(new PropertyValueFactory<KontaktListe, ComboBox<String>>("s1"));
		colBezeichnung.setMinWidth(200);
		
		colDelete = new TableColumn<KontaktListe, Object>(null);
		colDelete.setCellValueFactory(new PropertyValueFactory<KontaktListe, Object>("löschen"));
		colDelete.setMaxWidth(50);
		
		colEintrag = new TableColumn<KontaktListe, TextField>();
		colEintrag.setCellValueFactory(new PropertyValueFactory<KontaktListe, TextField>("s2"));
		colEintrag.setMinWidth(200);
		
		kontaktTable.getColumns().addAll(colAusw, colBezeichnung, colDelete, colEintrag);
	}```

und das hinzufügen der Werte:

```	public KontaktModel() {
		auswMenu = new ArrayList<String>();
		
		neuerKontakt();
		
		clients.set(kontaktDaten);
		
	}

	/**
	 * Erstellt einen Default eintrag in der Liste
	 */
	public void neuerKontakt() {
		kontaktDaten.add(addNameVorn(0));

		kontaktDaten.add(addFirma(1));

		kontaktDaten.add(addFunktion(2));

		kontaktDaten.add(addEmail(3));

		kontaktDaten.add(addTel(4));

		//kontakt.add(addZusFelder(5, 0));
	}
	
	/**
	 * 
	 * @param id
	 */
	private KontaktListe addNameVorn(int id) {
		KontaktListe kontaktListe = new KontaktListe();

		TextField textfield = new TextField("Nachname");
		
		kontaktListe.setId(id);
		kontaktListe.setS1("Vorname");
		kontaktListe.setS2(textfield);

		return kontaktListe;
	}

	/**
	 * 
	 * @param id
	 */
	private KontaktListe addFirma(int id) {
		KontaktListe kontaktListe = new KontaktListe();

		kontaktListe.setId(id);
		kontaktListe.setS1("Firma");

		return kontaktListe;
	}

	/**
	 * 
	 * @param id
	 */
	private KontaktListe addFunktion(int id) {
		KontaktListe kontaktListe = new KontaktListe();

		kontaktListe.setId(id);
		kontaktListe.setS1("Position");

		return kontaktListe;
	}

	/**
	 * 
	 * @param id
	 */
	private KontaktListe addEmail(int id) {
		KontaktListe kontaktListe = new KontaktListe();

		kontaktListe.setId(id);
		kontaktListe.setAuswahl(Icons.kontaktZusAng.getImage());
		kontaktListe.setS1("E-Mail");
		kontaktListe.setLöschen(Icons.kontaktLoeschen.getImage());

		return kontaktListe;
	}

	/**
	 * 
	 * @param id
	 * @return kontaktliste
	 */
	private KontaktListe addTel(int id) {
		KontaktListe kontaktListe = new KontaktListe();

		kontaktListe.setId(id);
		kontaktListe.setAuswahl(Icons.kontaktZusAng.getImage());
		kontaktListe.setS1("Tel. Direkt");
		kontaktListe.setLöschen(Icons.kontaktLoeschen.getImage());

		return kontaktListe;
	}```

Weis da wirklich niemand rat?

Ich sehe nicht wirklich, wo Du deiner TableView Bilder hinzufügst. Ich hätte mal zumindest so etwas wie

private TableColumn<KontaktListe, ImageView> imageColumn```
erwartet. Wo soll denn das Image herkommen?

Du hast völlig recht, dies war ganz sicher ein Fehler, den ich nun behoben habe. Nur leider bleibt das Ergebnis noch das gleiche:

	@FXML
	private TableView<KontaktListe> kontaktTable;

	@FXML
	private TableColumn<KontaktListe, Image> colAusw;

	@FXML
	private TableColumn<KontaktListe, String> colBezeichnung;

	@FXML
	private TableColumn<KontaktListe, Image> colDelete;

	@FXML
	private TableColumn<KontaktListe, TextField> colEintrag;

	/**
	 * Wird aufgerufen sobald die view geladen ist
	 */
	@FXML
	private void initialize() {
		System.out.println("Kontaktverwaltung geladen");

		aenderungenTable();

		colAusw = new TableColumn<KontaktListe, Image>();
		colAusw.setCellValueFactory(new PropertyValueFactory<KontaktListe, Image>("auswahl"));
		colAusw.setMaxWidth(50);
		
		colBezeichnung = new TableColumn<KontaktListe, String>();
		colBezeichnung.setCellValueFactory(new PropertyValueFactory<KontaktListe, String>("s1"));
		colBezeichnung.setMinWidth(200);
		
		colDelete = new TableColumn<KontaktListe, Image>();
		colDelete.setCellValueFactory(new PropertyValueFactory<KontaktListe, Image>("löschen"));
		colDelete.setMaxWidth(50);
		
		colEintrag = new TableColumn<KontaktListe, TextField>();
		colEintrag.setCellValueFactory(new PropertyValueFactory<KontaktListe, TextField>("s2"));
		colEintrag.setMinWidth(200);
		
		kontaktTable.getColumns().addAll(colAusw, colBezeichnung, colDelete, colEintrag);
	}

Du musst wohl eine eigene TableCell implementieren.

{
     @Override
     protected void updateItem(ImageView arg1, boolean arg2)
     {
          if (arg2)
          {
              setGraphic(arg1);
          }
}```
Beachte die Benutzung von ImageView anstelle von Image. Ein pures Image ist für JavaFX nicht darstellbar, die Klasse Image ist eine "Hilfsklasse" für das UI-Element ImageView.

@Hobelhai

Der Tipp mit dem ImageView hat geholfen. Ich habe im versucht ein Image in der Table darzustellen über den ImageView. Das ging nicht. Jetzt wird das Image direkt über new ImageView(path) erstellt und so klappt es vorzüglich.

Vielen Dank

Aber gerne doch.