2016-05-14 66 views
1

我使用SceneBuilder来构建一个GUI。现在我确实有问题。如果我想在TableView中显示单个对象,那么一切正常,但如果我使用另一个包含另一个对象的构造函数,则第二个对象不会出现在TableView中。对象包含另一个对象。如何在TableView中显示它?

代码:

public class MainController implements Initializable { 

@FXML 
TableView<Table> tableID; 
@FXML 
TableColumn<Table, Integer> iID; 
@FXML 
TableColumn<Table, String> iName; 
@FXML 
TableColumn<Table, String> iDate; 
@FXML 
TableColumn<Table, Integer> iPrice; 
@FXML 
TableColumn<Table1, String> surname; 
@FXML 
TableColumn<Table1, String> name; 

Table1 t1 = new Table1("just", "testing"); 

final ObservableList<Table> data = FXCollections.observableArrayList(
     new Table(t1, 12, "Name 1", "01/01/20012", 50), 
     new Table(t1, 1, "Name 1", "01/01/20012", 50), 
     new Table(t1, 3, "Name 1", "01/01/20012", 50)); 

@Override 
public void initialize(URL location, ResourceBundle resources) { 

    iID.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rID")); 
    iName.setCellValueFactory(new PropertyValueFactory<Table, String>("rName"));  
    iDate.setCellValueFactory(new PropertyValueFactory<Table, String>("rDate"));  
    iPrice.setCellValueFactory(new PropertyValueFactory<Table, Integer>("rPrice")); 
    surname.setCellValueFactory(new PropertyValueFactory<Table1, String>("surname")); 
    name.setCellValueFactory(new PropertyValueFactory<Table1, String>("name")); 

    tableID.setItems(data); 

} 

表1类;

private String surname; 
private String name; 

public Table1(){ 

} 

public Table1(String surname, String name){ 
    this.name = name; 
    this.surname = surname; 
} 

public String getVorname() { 
    return surname; 
} 

public void setVorname(String surname) { 
    this.surname = surname; 
} 

public String getName() { 
    return name; 
} 

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

表类;

private final SimpleIntegerProperty rID; 
private final SimpleStringProperty rName;; 
private final SimpleStringProperty rDate; 
private final SimpleIntegerProperty rPrice; 

Table1 t1 = new Table1("just", "testing"); 

public Table(Table1 t1, int sID, String sName, String sDate, Integer sPrice) { 
    this.rID = new SimpleIntegerProperty(sID); 
    this.rName = new SimpleStringProperty(sName); 
    this.rDate = new SimpleStringProperty(sDate); 
    this.rPrice = new SimpleIntegerProperty(sPrice); 
} 

public Integer getRID() { 
    return rID.get(); 
} 

public void setRID(Integer v) { 
    rID.set(v); 
} 

public String getRName() { 
    return rName.get(); 

} 

public void setRName(String v) { 
    rName.set(v); 

} 
public String getRData(){ 
    return rDate.get(); 

} 

public void setRDate(String v){ 
    rDate.set(v); 

} 
public Integer getRPrice(){ 
    return rPrice.get(); 

} 

public void setRNPrice(Integer v){ 
    rPrice.set(v); 

} 

感谢您的帮助!

回答

0

我认为您正在寻找细胞定制。 此链接可以帮助你:http://code.makery.ch/blog/javafx-8-tableview-cell-renderer/

编辑:

此代码会给你一些错误。例如,您设置TableView<>将包含Table类型的对象,因此您无法设置类型为Table1TableColumn<>

同样,您首先需要初始化TableViewTableColumn s。

,我找到了解决办法:你可以在Table类返回从Table1分别namesurnamegetters

下面是你需要的代码(只是改变):

MainController类:

TableColumn<Table, String> surname; //Instead of Table1 
    TableColumn<Table, String> name; //Instead of Table1 
    //DONT FORGET TO INITIALIZE THE TableColumns and TableView 

Table类:

private Table1 t1; 

public Table(Table1 t1, int sID, String sName, String sDate, Integer sPrice) { 
    this.rID = new SimpleIntegerProperty(sID); 
    this.rName = new SimpleStringProperty(sName); 
    this.rDate = new SimpleStringProperty(sDate); 
    this.rPrice = new SimpleIntegerProperty(sPrice); 
    this.t1 = t1; 
} 
//Add this 
public String getName(){ 
    return t1.getName(); 
} 
//Add this 
public String getSurname(){ 
    return t1.getVorname(); 
    //Bist Du Deutscher? :D 
} 
+0

嗯...感谢您的回答,但我无法从此链接获取所需的信息。我的问题是第二个对象在tableview中完全没有出现... – DonJohn

+0

你可以显示其余的代码吗? –

+0

Thx,在我的文章中添加了代码! – DonJohn

相关问题