我使用SceneBuilder
来构建GUI。现在我确实遇到了问题。如果我想在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);
}
Table1类;
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 :(得分:0)
我认为您正在寻找Cell Customization。 此链接可以为您提供帮助:http://code.makery.ch/blog/javafx-8-tableview-cell-renderer/
修改强>
此代码会给您一些错误。例如,您要设置TableView<>
将包含Table
类型的对象,因此您无法设置TableColumn<>
类型的Table1
。
同样,您首先需要初始化TableView
和TableColumn
。
我找到的解决方案:您可以在getters
类中编写Table
,分别从name
返回surname
和Table1
。
以下是您需要的代码(只是更改):
在课程
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
}