2013-03-10 32 views
5

我希望尽可能短而不遗漏有用的信息。 我有以下类:如何为JavaFX设置项目TableView - 对象包含另一个对象?

public class Address{ 
StringProperty city = new SimpleStringProperty(); 
StringProperty street = new SimpleStringProperty(); 

//following the constructor, getters and setters 
... 
} 

我有另一个类的客户端,即一个有一个地址构件

public class Client { 

StringProperty name = new SimpleStringProperty(); 
StringProperty id = new SimpleStringProperty(); 
ObjectProperty<Address> address = new SimpleObjectProperty<>(); 

//following the constructor, getters and setters 
... 
} 

和与控制器一个JavaFX接口包含TableView中对象应该在3输出列客户类的成员和给定对象的地址类的城市成员。我的TableView和TableColumn的定义是以下代码

public class SettingsController { 
TableColumn<Client, String> clientNameCol; 
TableColumn<Client, String> clientEmailCol; 
TableColumn<Client, String> clientCityCol; 
private TableView<Client> clientSettingsTableView; 
... 
... 
    clientNameCol = new TableColumn<>("Name"); 
    clientNameCol.setCellValueFactory(new PropertyValueFactory<Client, String>("name")); 

    clientEmailCol = new TableColumn<>("email"); 
    clientEmailCol.setCellValueFactory(new PropertyValueFactory<Client, String>("email")); 

    clientCityCol = new TableColumn<>("City"); 
    clientCityCol.setCellValueFactory(new PropertyValueFactory<Client, String>("city")); 

    clientSettingsTableView.setItems(clientData); 
    clientSettingsTableView.getColumns().clear(); 
    clientSettingsTableView.getColumns().addAll(clientNameCol, clientEmailCol, clientCityCol); 

当然并且有一个ObservableList clientData包含客户对象的阵列。 除了应为每个客户端输出城市的列以外,一切正常。 我应该如何定义Client对象的城市(由地址成员包含)的列?

+0

可能会有所帮助http://fxapps.blogspot.com/2012/09/showing-object-properties-in-tableview.html – invariant 2013-03-10 23:05:48

回答

6

您的帮助@invariant感谢,我GOOGLE了一点点,我结束了以下解决方案:

clientCityCol = new TableColumn<>("City"); 
clientCityCol.setCellValueFactory(new PropertyValueFactory<Client, Address>("address")); 
// ======== setting the cell factory for the city column 
clientCityCol.setCellFactory(new Callback<TableColumn<Client, Address>, TableCell<Client, Address>>(){ 

     @Override 
     public TableCell<Client, Address> call(TableColumn<Client, Address> param) { 

      TableCell<Client, Address> cityCell = new TableCell<Client, Address>(){ 

       @Override 
       protected void updateItem(Address item, boolean empty) { 
        if (item != null) { 
         Label cityLabel = new Label(item.getCity()); 
         setGraphic(cityLabel); 
        } 
       }      
      };    

      return cityCell;     
     } 

    }); 

地址类有一个getter getCity()返回城里成员作为一个String( )。

+0

真的帮了我@markus谢谢+1。这必须是一种常见的设计情况,但我发现网络上的信息稀少。对于所有未来的访问者来说,官方文档中没有这方面的内容吗? – demongolem 2014-09-25 22:00:51

+0

正是我所看到的,你实际上可以在updateItem上绘制任何你想要的东西 – 2017-08-09 03:47:26

0

这适用于fxml?

考虑下面的代码:

<TableColumn prefWidth="8" text="City"> <cellValueFactory > <PropertyValueFactory property="adress.city" /> </cellValueFactory> </TableColumn>

工作这样,我有一个空白单元格。

+7

这是个问题还是答案? – jewelsea 2014-04-17 01:29:06

+0

感谢您的回答。我想我只是发现了javafx fxml的一个[其他方面](https://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm#CACHDCCF) – smac89 2017-03-24 22:47:29

0

您忘了提及您必须将clientCityCol TableColumn<Client, String> clientCityCol;的定义更改为TableColumn<Client, Address> clientCityCol;,否则它将无法工作。

相关问题