Issue
what is the difference between string and Property String in tableview javafx? how it changes the tableview if i use data type as String or Property String ?? Can anyone give example to show this difference
Solution
Property String is different in Java. Basically you use property String when you want to observe your variable in a TableView. The reason Java does this is that Java uses an MVC Pattern (Model-View-Controller). The model is your stored data, the view is what you see like GUI and the controller is the brains and logic for everything in your application. The Model in Java is done as classes that hold properties rather than just fields. Because when you want to store data into a TableView in JavaFX the proper way is to instantiate objects from a class and the Properties defined in this class becomes the properties of this object, by then you can store the object in the TableView and put some logic to allow the tableView to go find the properties of this object and fill them in the table, if they were Strings rather than properties, JavaFX won't be able to get them and make them observable in the table. I wrote some logic below to give you an idea of how this is done. So first this is a class that acts as a Model:
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;
}
Those getters and setters should follow the Standards of naming conventions in Java, so that when you insert and object into the table as i will show below, the table would use the field names and fetch for the getter for each field to get it's value and make it observable in the tabele, so below is an example of the controller to fill the columns and rows of the table:
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);
}
}
The next is to fill the rows, assuming i selected data from the database and stored them in a result set, i will use the next method to go through the records of the resultSet row by row, this method would return false when there is no more rows
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();
}
}
So as you can see, i used SimpleStringProperty rather than Strings and if u used Strings here JavaFx wouldn't be able to display the results as they somehow are not considered properties of the objects inserted into the table
Answered By - Zeyad
Answer Checked By - Senaida (JavaFixing Volunteer)