对象包含另一个对象。如何在 TableView 中显示?
Object contains another object. How to show it in TableView?
我正在使用 SceneBuilder
构建 GUI。现在我确实有问题。如果我想在 TableView
中显示单个对象,一切正常,但如果我使用另一个包含另一个对象的构造函数,第二个对象就不会出现在 TableView
.[=16= 中]
代码:
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 class;
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;
}
Table class;
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);
}
感谢您的帮助!
我认为您正在寻找 Cell Customization。
这个link可能对你有帮助:http://code.makery.ch/blog/javafx-8-tableview-cell-renderer/
编辑:
此代码会给您带来一些错误。例如,您设置 TableView<>
将包含 Table
类型的对象,因此您不能设置 Table1
.
类型的 TableColumn<>
此外,您首先需要初始化 TableView
和 TableColumn
。
我找到的解决方案:可以在Table
class中写getters
,return分别是name
和surname
来自 Table1
。
这是您需要的代码(只是更改):
At class MainController
:
TableColumn<Table, String> surname; //Instead of Table1
TableColumn<Table, String> name; //Instead of Table1
//DONT FORGET TO INITIALIZE THE TableColumns and TableView
At class 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
}
我正在使用 SceneBuilder
构建 GUI。现在我确实有问题。如果我想在 TableView
中显示单个对象,一切正常,但如果我使用另一个包含另一个对象的构造函数,第二个对象就不会出现在 TableView
.[=16= 中]
代码:
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 class;
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;
}
Table class;
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);
}
感谢您的帮助!
我认为您正在寻找 Cell Customization。 这个link可能对你有帮助:http://code.makery.ch/blog/javafx-8-tableview-cell-renderer/
编辑:
此代码会给您带来一些错误。例如,您设置 TableView<>
将包含 Table
类型的对象,因此您不能设置 Table1
.
TableColumn<>
此外,您首先需要初始化 TableView
和 TableColumn
。
我找到的解决方案:可以在Table
class中写getters
,return分别是name
和surname
来自 Table1
。
这是您需要的代码(只是更改):
At class
MainController
:
TableColumn<Table, String> surname; //Instead of Table1
TableColumn<Table, String> name; //Instead of Table1
//DONT FORGET TO INITIALIZE THE TableColumns and TableView
At class
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
}