简体   繁体   中英

JavaFX/SceneBuilder StringProperty[value: ] text in cell

So I'm working on a project for my Java/SQL lecture about an administration tool for customers and buildings which these customers could rent.

I've created a .fxml for my customers, to search through a sql database and display them and later show them in a TableView. At the moment I'm just testing the GUI and trying to get these things working but I can't figure out how to put text in my TableView properly. I've created a class for my customers (Kunde) and the controller class for my customer.fxml. I've also made an ObservableList for my customers and put a test customer in it. The TableView actually "shows" the row but not just the value but with the addition "[...]Property [value: ]" and then the actual value of the string. But I just want to have the real value in the cells. Here is a little picture of the problem.

So here's my code. I hope anyone could help me (some of the comments or attributes are in german but i still hope you can understand what's meant). CustomerController:

And my customer class:

package project.hausverwaltung.model;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class Kunde {

    private SimpleStringProperty firstName;
    private SimpleStringProperty lastName;
    private SimpleStringProperty street;
    private SimpleStringProperty streetNumber;
    private SimpleIntegerProperty zip;
    private SimpleStringProperty city;
    private SimpleStringProperty birthDate;
    private SimpleIntegerProperty customerID;

    public Kunde(String firstName, String lastName, String street, String streetNumber, int zip, String city,
            int customerID, String birthDate) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
        this.street = new SimpleStringProperty(street);
        this.streetNumber = new SimpleStringProperty(streetNumber);
        this.zip = new SimpleIntegerProperty(zip);
        this.city = new SimpleStringProperty(city);
        this.birthDate = new SimpleStringProperty(birthDate);
        this.customerID = new SimpleIntegerProperty(customerID);
    }

    public SimpleStringProperty getFirstName() {
        return firstName;
    }

    public void setFirstName(SimpleStringProperty firstName) {
        this.firstName = firstName;
    }

    public SimpleStringProperty getLastName() {
        return lastName;
    }

    public void setLastName(SimpleStringProperty lastName) {
        this.lastName = lastName;
    }

    public SimpleStringProperty getStreet() {
        return street;
    }

    public void setStreet(SimpleStringProperty street) {
        this.street = street;
    }

    public SimpleStringProperty getStreetNumber() {
        return streetNumber;
    }

    public void setStreetNumber(SimpleStringProperty streetNumber) {
        this.streetNumber = streetNumber;
    }

    public SimpleIntegerProperty getZip() {
        return zip;
    }

    public void setZip(SimpleIntegerProperty zip) {
        this.zip = zip;
    }

    public SimpleStringProperty getCity() {
        return city;
    }

    public void setCity(SimpleStringProperty city) {
        this.city = city;
    }

    public SimpleStringProperty getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(SimpleStringProperty birthDate) {
        this.birthDate = birthDate;
    }

    public SimpleIntegerProperty getCustomerID() {
        return customerID;
    }

    public void setCustomerID(SimpleIntegerProperty customerID) {
        this.customerID = customerID;
    }

}

I hope anyone of you could give me a little hint, what's wrong with my code and how to fix it. Thanks a lot!

Use the JavaFX Properties pattern (which is a fairly simple extension of the regular Java Bean pattern) for your model class ( Kunde ). In other words, you define a String property called firstName via the usual get/set methods, which is implemented by an observable property, and then define an accessor method for the property itself.

You should also, as always, program to interfaces, and not use implementation classes as part of the public API of your model class.

Specifically, it should look like:

public class Kunde {

    // Properties:

    private final StringProperty firstName = new SimpleStringProperty();

    public StringProperty firstNameProperty() {
        return firstName ;
    }

    public final String getFirstName() {
        return firstNameProperty().get();
    }

    public final void setFirstName(String firstName) {
        firstNameProperty().set(firstName);
    }

    // similarly for other properties

    // Constructor:

    public Kunde(String firstName /* , ... */) {
        // Note there is no problem in calling setFirstName(...)
        // from the constructor as it is a final method:
        setFirstName(firstName);

        // ...
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM