difference between string and property String in tableview javafx









up vote
1
down vote

favorite












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










share|improve this question



















  • 1




    I'm not entirely sure I understand what you're asking about. Are you asking what's the difference between String and StringProperty? How is TableView involved? Please edit your question to make it more clear.
    – Slaw
    Nov 10 at 15:38










  • so i edited my question ;) i hope to get an answer
    – ياسين زهيد
    Nov 10 at 15:59














up vote
1
down vote

favorite












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










share|improve this question



















  • 1




    I'm not entirely sure I understand what you're asking about. Are you asking what's the difference between String and StringProperty? How is TableView involved? Please edit your question to make it more clear.
    – Slaw
    Nov 10 at 15:38










  • so i edited my question ;) i hope to get an answer
    – ياسين زهيد
    Nov 10 at 15:59












up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question















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







java javafx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 15:57

























asked Nov 10 at 15:22









ياسين زهيد

84




84







  • 1




    I'm not entirely sure I understand what you're asking about. Are you asking what's the difference between String and StringProperty? How is TableView involved? Please edit your question to make it more clear.
    – Slaw
    Nov 10 at 15:38










  • so i edited my question ;) i hope to get an answer
    – ياسين زهيد
    Nov 10 at 15:59












  • 1




    I'm not entirely sure I understand what you're asking about. Are you asking what's the difference between String and StringProperty? How is TableView involved? Please edit your question to make it more clear.
    – Slaw
    Nov 10 at 15:38










  • so i edited my question ;) i hope to get an answer
    – ياسين زهيد
    Nov 10 at 15:59







1




1




I'm not entirely sure I understand what you're asking about. Are you asking what's the difference between String and StringProperty? How is TableView involved? Please edit your question to make it more clear.
– Slaw
Nov 10 at 15:38




I'm not entirely sure I understand what you're asking about. Are you asking what's the difference between String and StringProperty? How is TableView involved? Please edit your question to make it more clear.
– Slaw
Nov 10 at 15:38












so i edited my question ;) i hope to get an answer
– ياسين زهيد
Nov 10 at 15:59




so i edited my question ;) i hope to get an answer
– ياسين زهيد
Nov 10 at 15:59












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Property String is different in Java. Basically you you 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() + "nnMessage: Unable to get the columns from the databasennDetails: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() + "nnMessage: Unable to get the records from the databasennDetails: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






share|improve this answer




















  • thank you bro you help me
    – ياسين زهيد
    Nov 10 at 16:46










  • Anytime bro, my pleasure @ياسين زهيد
    – Zeyad
    Nov 10 at 20:27











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240376%2fdifference-between-string-and-property-string-in-tableview-javafx%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Property String is different in Java. Basically you you 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() + "nnMessage: Unable to get the columns from the databasennDetails: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() + "nnMessage: Unable to get the records from the databasennDetails: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






share|improve this answer




















  • thank you bro you help me
    – ياسين زهيد
    Nov 10 at 16:46










  • Anytime bro, my pleasure @ياسين زهيد
    – Zeyad
    Nov 10 at 20:27















up vote
0
down vote



accepted










Property String is different in Java. Basically you you 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() + "nnMessage: Unable to get the columns from the databasennDetails: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() + "nnMessage: Unable to get the records from the databasennDetails: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






share|improve this answer




















  • thank you bro you help me
    – ياسين زهيد
    Nov 10 at 16:46










  • Anytime bro, my pleasure @ياسين زهيد
    – Zeyad
    Nov 10 at 20:27













up vote
0
down vote



accepted







up vote
0
down vote



accepted






Property String is different in Java. Basically you you 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() + "nnMessage: Unable to get the columns from the databasennDetails: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() + "nnMessage: Unable to get the records from the databasennDetails: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






share|improve this answer












Property String is different in Java. Basically you you 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() + "nnMessage: Unable to get the columns from the databasennDetails: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() + "nnMessage: Unable to get the records from the databasennDetails: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







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 16:24









Zeyad

446




446











  • thank you bro you help me
    – ياسين زهيد
    Nov 10 at 16:46










  • Anytime bro, my pleasure @ياسين زهيد
    – Zeyad
    Nov 10 at 20:27

















  • thank you bro you help me
    – ياسين زهيد
    Nov 10 at 16:46










  • Anytime bro, my pleasure @ياسين زهيد
    – Zeyad
    Nov 10 at 20:27
















thank you bro you help me
– ياسين زهيد
Nov 10 at 16:46




thank you bro you help me
– ياسين زهيد
Nov 10 at 16:46












Anytime bro, my pleasure @ياسين زهيد
– Zeyad
Nov 10 at 20:27





Anytime bro, my pleasure @ياسين زهيد
– Zeyad
Nov 10 at 20:27


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240376%2fdifference-between-string-and-property-string-in-tableview-javafx%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3