JavaFX如何在TabelView单元格中显示Object属性而不是Object值

时间:2019-04-06 14:36:38

标签: java uitableview javafx tableview

我正在尝试在ObservableList的表视图中打印javafx。我试图在表视图中显示的ObservableList中的对象包含对Person类型的对象的引用,我试图显示Person对象的name属性而不是Person对象的值。我怎样才能做到这一点?

FXMLConroller:

public class FXMLControllerAdd implements Initializable{  
    public ObservableList<Event> events = FXCollections.observableArrayList();
    public ObservableList<Person> people = FXCollections.observableArrayList();

    Person chosenPerson = null;

    @FXML private TextField txtName;
    @FXML private ComboBox contactBox;
    @FXML public TableView<Event> eventTable;
    @FXML private TableColumn<Event, String> NameCell, contactCell;


    public void contactBoxUpdate(){
    //Sett chosenPerson to person selected from ComboBox
        for (Person aPerson : people){
            if(contactBox.getValue().equals(aPerson)){
                chosenPerson = aPerson;
            }
        }
    }


    @FXML
    public void registerEvent(ActionEvent event) {
        eventTable.setItems(addEvent());
    }

    public ObservableList<Event> addEvent(){    
        events.add(new Event(txtName.getText(), chosenPerson);                                       
        return events;
    }


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        NameCell.setCellValueFactory(new PropertyValueFactory<Event, String>("name"));
        contactCell.setCellValueFactory(new PropertyValueFactory<Event, String>("contactPerson"));

    }    

事件类:

public class Event {

    public SimpleStringProperty name  //Name of event
    public Person contactPerson;      

    public Arrangement(String navn, Person kontaktperson) {
        this.name = new SimpleStringProperty(name);
        this.contactPerson = contactPerson;
    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name = new SimpleStringProperty(name);
    }


    public Person getContactPerson() {
        return contactPerson;
    }

    public void setKontaktperson(Person contactPerson) {
        this.contactPerson = contactPerson;
    }



}

人员班:

public class Person{


    public SimpleStringProperty name;
    public SimpleIntegerProperty phoneNr;

    public Person(String name, Integer phoneNr) {
        this.name       = new SimpleStringProperty(name);
        this.phoneNr  = new SimpleIntegerProperty(phoneNr);

    }

    public String getName() {
        return name.get();
    }

    public void setName(String name) {
        this.name = new SimpleStringProperty(name);
    }

    public Integer getPhoneNr() {
        return phoneNr.get();
    }

    public void setPhoneNr(Integer phoneNr) {
        this.phoneNr = new SimpleIntegerProperty(phoneNr);
    }
}

所以我希望contactCell显示chosenPerson.getName()而不是所选人员的值,例如:Person @ 3112fc3

0 个答案:

没有答案