2015-07-31 2 views
0

Я пытаюсь показать некоторую информацию в TableView, но у меня возникли некоторые проблемы, которые я не могу исправить.Почему StringProperty [значение: ""] отображается в моей таблице

Вот что я пытаюсь сделать:

У меня есть Room объект:

public class Room { 

    private SimpleStringProperty id; 
    private SimpleStringProperty description; 
    private SimpleStringProperty isForRepair; 
    private SimpleStringProperty comment; 

    public Room(String id, String description, String isForRepair, String comment) { 
     this.id = new SimpleStringProperty(id); 
     this.description = new SimpleStringProperty(description); 
     this.isForRepair = new SimpleStringProperty(isForRepair); 
     this.comment = new SimpleStringProperty(comment); 
    } 

    public SimpleStringProperty getId() { 
     return id; 
    } 

    public void setId(SimpleStringProperty id) { 
     this.id = id; 
    } 

    public SimpleStringProperty getDescription() { 
     return description; 
    } 

    public void setDescription(SimpleStringProperty description) { 
     this.description = description; 
    } 

    public SimpleStringProperty getIsForRepair() { 
     return isForRepair; 
    } 

    public void setIsForRepair(SimpleStringProperty isForRepair) { 
     this.isForRepair = isForRepair; 
    } 

    public SimpleStringProperty getComment() { 
     return comment; 
    } 

    public void setComment(SimpleStringProperty comment) { 
     this.comment = comment; 
    } 

} 

И я пытаюсь добавить несколько строк в моей таблице. Вот весь мой код:

public class RoomsManager { 

    private TableView<Room> table = new TableView<>(); 

    public RoomsManager() { 
    } 

    public void show() { 
     Stage roomsStage = new Stage(); 

     final ObservableList<Room> data = FXCollections.observableArrayList(
       new Room("1", "The small one", "no", "Good condition") 
     ); 

     Scene scene = new Scene(new Group()); 
     roomsStage.setTitle("Table View Sample"); 
     roomsStage.setWidth(355); 
     roomsStage.setHeight(500); 

     final Label label = new Label("Rooms"); 
     label.setFont(new Font("Arial", 20)); 

     table.setEditable(true); 

     TableColumn idCol = new TableColumn("ID"); 
     idCol.setMinWidth(100); 
     idCol.setCellValueFactory(
       new PropertyValueFactory<Room, String>("id") 
     ); 
     TableColumn descriptionCol = new TableColumn("Description"); 
     descriptionCol.setMinWidth(100); 
     descriptionCol.setCellValueFactory(
       new PropertyValueFactory<Room, String>("description") 
     ); 
     TableColumn isForRepairCol = new TableColumn("Is for repair"); 
     isForRepairCol.setMinWidth(100); 
     isForRepairCol.setCellValueFactory(
       new PropertyValueFactory<Room, String>("isForRepair") 
     ); 
     TableColumn commentCol = new TableColumn("Comment"); 
     commentCol.setMinWidth(100); 
     commentCol.setCellValueFactory(
       new PropertyValueFactory<Room, String>("comment") 
     ); 
     table.setItems(data); 
     table.getColumns().addAll(idCol, descriptionCol, isForRepairCol, commentCol); 

     final VBox vbox = new VBox(); 
     vbox.setSpacing(5); 
     vbox.setPadding(new Insets(10, 0, 0, 10)); 
     vbox.getChildren().addAll(label, table); 

     ((Group) scene.getRoot()).getChildren().addAll(vbox); 

     roomsStage.setScene(scene); 

     roomsStage.show(); 

    } 

} 

Но моя таблица выглядит следующим образом:

enter image description here

Что я здесь отсутствует? Я знаю, что это что-то маленькое, но, как новый, я не могу это заметить. Можете ли вы дать мне толчок?

+0

Не могли бы вы показать, как выглядит 'SimpleStringProperty'? Вероятно, это потому, что 'toString()' метод – lukaslew

+0

@lukaslew Вы можете проверить это здесь: https://docs.oracle.com/javase/8/javafx/api/javafx/beans/property/SimpleStringProperty.html – Slim

+1

Я didn ' t используйте JavaFX, но в соответствии с [this] (http://stackoverflow.com/a/16387259/4165731) ваши методы getter должны возвращать 'String' и использовать' StringProperty.get() '(simillar seters), и вы должны иметь дополнительные методы, подобные 'public final StringProperty commentProperty() {return comment; } ' – lukaslew

ответ

3

Вы не послушались JavaFX конкретного синтаксиса аксессора в вашем Room DataModel:

Если поле ObservableValue как:

private StringProperty val = new SimpleStringProperty(); 

Там должно быть аксессоры как:

public String getVal() { 
    val.get(); 
} 

public void setVal(String newVal) { 
    val.set(newVal); 
} 

public StringProperty valProperty() { 
    return val; 
} 

Примечания метод val Недвижимость. Также см. this Q&A entry.

+0

Я знал, что мне не хватает чего-то маленького, но очень важного! Большое спасибо за помощь! – Slim