tableview javafx中的字符串和属性字符串有什么区别? 如果我使用数据类型为字符串或属性字符串,它如何更改 TableView ? 谁能举例说明这种差异
最佳答案
Java 中的属性字符串有所不同。基本上,当您想在 TableView 中观察变量时,可以使用属性 String。 Java 这样做的原因是 Java 使用 MVC 模式(模型- View - Controller )。模型是您存储的数据, View 是您所看到的 GUI, Controller 是应用程序中所有内容的大脑和逻辑。 Java 中的模型是作为包含属性而不仅仅是字段的类来完成的。因为当你想将数据存储到JavaFX中的TableView中时,正确的方法是实例化类中的对象,并且该类中定义的属性成为该对象的属性,然后你可以将对象存储在TableView中并放置一些逻辑以允许tableView查找该对象的属性并将它们填充到表中,如果它们是字符串而不是属性,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 来获取其值并使其在表中可观察到,因此下面是填充表的列和行的 Controller 示例:
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 方法逐行遍历结果集的记录,当没有更多行时,此方法将返回 false
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 而不是字符串,如果您在这里使用字符串,JavaFx 将无法显示结果,因为它们在某种程度上不被视为插入表中的对象的属性
关于java - javafx TableView 中字符串和属性字符串的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240376/