tableview javafx中的字符串和属性字符串之间有什么区别? 如果我将数据类型用作字符串或属性字符串,它将如何更改tableview? 谁能举例说明这种差异
答案 0 :(得分:0)
属性字符串在Java中是不同的。基本上,当您要在TableView中观察变量时,可以将属性设置为String。 Java之所以这样做是因为Java使用了MVC模式(模型-视图-控制器)。模型是您存储的数据,视图是您所看到的,就像GUI,控制器是您应用程序中所有内容的大脑和逻辑。 Java中的Model是作为包含属性而不是仅包含字段的类完成的。因为当您要将数据存储到JavaFX中的TableView中时,正确的方法是从一个类实例化对象,并且该类中定义的Properties成为该对象的属性,因此您可以将该对象存储在TableView中并放入一些逻辑为了使tableView能够找到此对象的属性并将其填充在表中,如果它们是String而不是属性,则JavaFX将无法获取它们并使它们在表中可观察。我在下面编写了一些逻辑,以使您了解如何完成此操作。首先,这是一个充当模型的类:
public class Contact extends SQL_Objects {
private SimpleStringProperty id;
private SimpleStringProperty firstName;
private SimpleStringProperty lastName;
private SimpleStringProperty phone;
private SimpleStringProperty email;
private SimpleStringProperty unitNo;
private SimpleStringProperty street;
private SimpleStringProperty city;
private SimpleStringProperty province;
private SimpleStringProperty zipCode;
private SimpleStringProperty country;
private SimpleStringProperty gender;
private SimpleStringProperty notes;
private SimpleStringProperty relationship;
private final static String[] FIELD_NAMES = { "id", "firstName", "lastName", "phone", "email", "unitNo", "street", "city", "province", "zipCode", "country", "gender", "notes", "relationship" };
public Contact(String id, String firstName, String lastName, String phone, String email, String unitNo, String street, String city, String province, String zipCode, String country, String gender, String notes, String relationship) {
this.id = new SimpleStringProperty(id);
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.phone = new SimpleStringProperty(phone);
this.email = new SimpleStringProperty(email);
this.unitNo = new SimpleStringProperty(street);
this.street = new SimpleStringProperty(street);
this.city = new SimpleStringProperty(city);
this.province = new SimpleStringProperty(province);
this.zipCode = new SimpleStringProperty(zipCode);
this.country = new SimpleStringProperty(country);
this.gender = new SimpleStringProperty(gender);
this.notes = new SimpleStringProperty(notes);
this.relationship = new SimpleStringProperty(relationship);
}
public String getId() {
return id.get();
}
public String getFirstName() {
return firstName.get();
}
public String getLastName() {
return lastName.get();
}
public String getPhone() {
return phone.get();
}
public String getEmail() {
return email.get();
}
public String getUnitNo() {
return unitNo.get();
}
public String getStreet() {
return street.get();
}
public String getCity() {
return city.get();
}
public String getProvince() {
return province.get();
}
public String getZipCode() {
return zipCode.get();
}
public String getCountry() {
return country.get();
}
public String getGender() {
return gender.get();
}
public String getNotes() {
return notes.get();
}
public String getRelationship() {
return relationship.get();
}
public static String[] getFieldNames() {
return FIELD_NAMES;
}
这些getter和setter应该遵循Java中的命名约定标准,以便在将对象插入表中时(如我将在下面显示的那样),该表将使用字段名称并为每个字段获取getter来获取它的值并使其在表中可见,因此以下是填充表的列和行的控制器示例:
private void fillColumns() {
try { // starting from 2 so that the id column is not included
for (int i = 2; i <= resultSet.getMetaData().getColumnCount(); i++ ) {
TableColumn column = new TableColumn(resultSet.getMetaData().getColumnName(i));
column.setCellValueFactory(new PropertyValueFactory<Contact, String>(Contact.getFieldNames()[i - 1]));
selectedTable.getColumns().add(column);
}
} catch (SQLException ex) {
Alert alert = new Alert(Alert.AlertType.ERROR, "Type:\n" + ex.getClass().getName() + "\n\nMessage: Unable to get the columns from the database\n\nDetails:\n" + ex.getMessage(), ButtonType.OK);
}
}
下一步是填充行,假设我从数据库中选择了数据并将它们存储在结果集中,我将使用next方法逐行遍历resultSet的记录,当没有更多的行
private void fillRows() {
shownRecords = 0;
try {
while(resultSet.next()) {
Contact cont = new Contact(Integer.toString(resultSet.getInt(1)), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getString(6), resultSet.getString(7), resultSet.getString(8), resultSet.getString(9), resultSet.getString(10), resultSet.getString(11), resultSet.getString(12), resultSet.getString(13), resultSet.getString(14));
tableView.getItems().add(cont);
}
resultSet.beforeFirst();
} catch (SQLException ex) {
Alert alert = new Alert(Alert.AlertType.ERROR, "Type:\n" + ex.getClass().getName() + "\n\nMessage: Unable to get the records from the database\n\nDetails:\n" + ex.getMessage(), ButtonType.OK);
alert.show();
}
}
如您所见,我使用了SimpleStringProperty而不是Strings,如果您在这里使用Strings,JavaFx将无法显示结果,因为它们不被认为是插入表中对象的属性