How to set value in TableView from Entity, using JavaFX
I am trying to implements the following JavaFX
vs Spring
implementation, but cannot grasp how to operate and display entity in TableView
form. (I means Gender
class. This is just an entity, that represented table, which can include only two value: male
or female
- and I want display this value in TableView
)
Person:
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@One_to_One
private Gender gender;
//getters & setters
}
Gender
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
String gender;
//getters & setters
}
In MainController
class I am implement TableView<>
for data imaging and editing table:
Controller:
public class MainController {
@FXML private TableView<Person> table;
@FXML private TextField ipnTxt; //Long data in Person entity
@FXML private TextField genderTxt; //Other entity (Gender) in Person entity
private ObservableList<Person> data;
@PostConstruct
public void init(){
List<Person> personList = personService.getAllPerson();
data = FXCollections.observableArrayList(personList);
TableColumn<Person, String> ipnColumn = new TableColumn<>("IPN");
ipnColumn.setCellValueFactory(new PropertyValueFactory<>("ipn"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
table.getColumns().setAll(ipnColumn, genderColumn);
table.setItems(data);
}
.fxml file
*Part, where execute table displaying:
<TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="405.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
<children>
<!--<TextField fx:id="id" promptText="ID">-->
<!--<HBox.margin>-->
<!--<Insets right="3.0"/>-->
<!--</HBox.margin>-->
<!--</TextField>-->
<TextField fx:id="ipnTxt" promptText="IPN">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<TextField fx:id="genderTxt" promptText="Gender">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<Button minWidth="-Infinity" mnemonicParsing="false" onAction="#addPerson" text="Add" />
</children>
</HBox>
So, in result I have data from IPN
column and reference on Gender
object in gender
column:
But I need to get String gender
value from those objects.
How to get and put value from object in this case?
Additional info
Full Person class
package com.production.weighlifting.weightliftingviewer.model.domain;
import javax.persistence.*;
import java.sql.Date;
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@Column(name = "passport_number", unique = true)
private String passportNumber;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "born")
private java.sql.Date born;
public String genderValue;
@OneToOne
// @Column(name = "gender_id")
private Gender gender;
public Person(){
}
public Person(long ipn, String passportNumber, String name, String lastName, Date born, Gender gender) {
this.ipn = ipn;
this.passportNumber = passportNumber;
this.name = name;
this.lastName = lastName;
this.born = born;
this.gender = gender;
this.genderValue = gender.getGender();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getIpn() {
return ipn;
}
public void setIpn(long ipn) {
this.ipn = ipn;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public java.sql.Date getBorn() {
return born;
}
public void setBorn(java.sql.Date born) {
this.born = born;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getGenderValue(){return genderValue;}
}
Full Gender class
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
private String gender;
public Gender(){
}
public long getId() {
return id;
}
public Gender(GenderEnum genderEnum) {
this.gender = genderEnum.name();
}
public void setId(long id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
java javafx
add a comment |
I am trying to implements the following JavaFX
vs Spring
implementation, but cannot grasp how to operate and display entity in TableView
form. (I means Gender
class. This is just an entity, that represented table, which can include only two value: male
or female
- and I want display this value in TableView
)
Person:
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@One_to_One
private Gender gender;
//getters & setters
}
Gender
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
String gender;
//getters & setters
}
In MainController
class I am implement TableView<>
for data imaging and editing table:
Controller:
public class MainController {
@FXML private TableView<Person> table;
@FXML private TextField ipnTxt; //Long data in Person entity
@FXML private TextField genderTxt; //Other entity (Gender) in Person entity
private ObservableList<Person> data;
@PostConstruct
public void init(){
List<Person> personList = personService.getAllPerson();
data = FXCollections.observableArrayList(personList);
TableColumn<Person, String> ipnColumn = new TableColumn<>("IPN");
ipnColumn.setCellValueFactory(new PropertyValueFactory<>("ipn"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
table.getColumns().setAll(ipnColumn, genderColumn);
table.setItems(data);
}
.fxml file
*Part, where execute table displaying:
<TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="405.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
<children>
<!--<TextField fx:id="id" promptText="ID">-->
<!--<HBox.margin>-->
<!--<Insets right="3.0"/>-->
<!--</HBox.margin>-->
<!--</TextField>-->
<TextField fx:id="ipnTxt" promptText="IPN">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<TextField fx:id="genderTxt" promptText="Gender">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<Button minWidth="-Infinity" mnemonicParsing="false" onAction="#addPerson" text="Add" />
</children>
</HBox>
So, in result I have data from IPN
column and reference on Gender
object in gender
column:
But I need to get String gender
value from those objects.
How to get and put value from object in this case?
Additional info
Full Person class
package com.production.weighlifting.weightliftingviewer.model.domain;
import javax.persistence.*;
import java.sql.Date;
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@Column(name = "passport_number", unique = true)
private String passportNumber;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "born")
private java.sql.Date born;
public String genderValue;
@OneToOne
// @Column(name = "gender_id")
private Gender gender;
public Person(){
}
public Person(long ipn, String passportNumber, String name, String lastName, Date born, Gender gender) {
this.ipn = ipn;
this.passportNumber = passportNumber;
this.name = name;
this.lastName = lastName;
this.born = born;
this.gender = gender;
this.genderValue = gender.getGender();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getIpn() {
return ipn;
}
public void setIpn(long ipn) {
this.ipn = ipn;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public java.sql.Date getBorn() {
return born;
}
public void setBorn(java.sql.Date born) {
this.born = born;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getGenderValue(){return genderValue;}
}
Full Gender class
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
private String gender;
public Gender(){
}
public long getId() {
return id;
}
public Gender(GenderEnum genderEnum) {
this.gender = genderEnum.name();
}
public void setId(long id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
java javafx
add a comment |
I am trying to implements the following JavaFX
vs Spring
implementation, but cannot grasp how to operate and display entity in TableView
form. (I means Gender
class. This is just an entity, that represented table, which can include only two value: male
or female
- and I want display this value in TableView
)
Person:
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@One_to_One
private Gender gender;
//getters & setters
}
Gender
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
String gender;
//getters & setters
}
In MainController
class I am implement TableView<>
for data imaging and editing table:
Controller:
public class MainController {
@FXML private TableView<Person> table;
@FXML private TextField ipnTxt; //Long data in Person entity
@FXML private TextField genderTxt; //Other entity (Gender) in Person entity
private ObservableList<Person> data;
@PostConstruct
public void init(){
List<Person> personList = personService.getAllPerson();
data = FXCollections.observableArrayList(personList);
TableColumn<Person, String> ipnColumn = new TableColumn<>("IPN");
ipnColumn.setCellValueFactory(new PropertyValueFactory<>("ipn"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
table.getColumns().setAll(ipnColumn, genderColumn);
table.setItems(data);
}
.fxml file
*Part, where execute table displaying:
<TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="405.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
<children>
<!--<TextField fx:id="id" promptText="ID">-->
<!--<HBox.margin>-->
<!--<Insets right="3.0"/>-->
<!--</HBox.margin>-->
<!--</TextField>-->
<TextField fx:id="ipnTxt" promptText="IPN">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<TextField fx:id="genderTxt" promptText="Gender">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<Button minWidth="-Infinity" mnemonicParsing="false" onAction="#addPerson" text="Add" />
</children>
</HBox>
So, in result I have data from IPN
column and reference on Gender
object in gender
column:
But I need to get String gender
value from those objects.
How to get and put value from object in this case?
Additional info
Full Person class
package com.production.weighlifting.weightliftingviewer.model.domain;
import javax.persistence.*;
import java.sql.Date;
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@Column(name = "passport_number", unique = true)
private String passportNumber;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "born")
private java.sql.Date born;
public String genderValue;
@OneToOne
// @Column(name = "gender_id")
private Gender gender;
public Person(){
}
public Person(long ipn, String passportNumber, String name, String lastName, Date born, Gender gender) {
this.ipn = ipn;
this.passportNumber = passportNumber;
this.name = name;
this.lastName = lastName;
this.born = born;
this.gender = gender;
this.genderValue = gender.getGender();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getIpn() {
return ipn;
}
public void setIpn(long ipn) {
this.ipn = ipn;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public java.sql.Date getBorn() {
return born;
}
public void setBorn(java.sql.Date born) {
this.born = born;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getGenderValue(){return genderValue;}
}
Full Gender class
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
private String gender;
public Gender(){
}
public long getId() {
return id;
}
public Gender(GenderEnum genderEnum) {
this.gender = genderEnum.name();
}
public void setId(long id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
java javafx
I am trying to implements the following JavaFX
vs Spring
implementation, but cannot grasp how to operate and display entity in TableView
form. (I means Gender
class. This is just an entity, that represented table, which can include only two value: male
or female
- and I want display this value in TableView
)
Person:
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@One_to_One
private Gender gender;
//getters & setters
}
Gender
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
String gender;
//getters & setters
}
In MainController
class I am implement TableView<>
for data imaging and editing table:
Controller:
public class MainController {
@FXML private TableView<Person> table;
@FXML private TextField ipnTxt; //Long data in Person entity
@FXML private TextField genderTxt; //Other entity (Gender) in Person entity
private ObservableList<Person> data;
@PostConstruct
public void init(){
List<Person> personList = personService.getAllPerson();
data = FXCollections.observableArrayList(personList);
TableColumn<Person, String> ipnColumn = new TableColumn<>("IPN");
ipnColumn.setCellValueFactory(new PropertyValueFactory<>("ipn"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
table.getColumns().setAll(ipnColumn, genderColumn);
table.setItems(data);
}
.fxml file
*Part, where execute table displaying:
<TableView fx:id="table" editable="true" prefHeight="200.0" prefWidth="405.0" tableMenuButtonVisible="true" AnchorPane.bottomAnchor="50.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnResizePolicy><TableView fx:constant="CONSTRAINED_RESIZE_POLICY" /></columnResizePolicy>
</TableView>
<HBox alignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="10.0" AnchorPane.rightAnchor="10.0">
<children>
<!--<TextField fx:id="id" promptText="ID">-->
<!--<HBox.margin>-->
<!--<Insets right="3.0"/>-->
<!--</HBox.margin>-->
<!--</TextField>-->
<TextField fx:id="ipnTxt" promptText="IPN">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<TextField fx:id="genderTxt" promptText="Gender">
<HBox.margin>
<Insets right="3.0"/>
</HBox.margin>
</TextField>
<Button minWidth="-Infinity" mnemonicParsing="false" onAction="#addPerson" text="Add" />
</children>
</HBox>
So, in result I have data from IPN
column and reference on Gender
object in gender
column:
But I need to get String gender
value from those objects.
How to get and put value from object in this case?
Additional info
Full Person class
package com.production.weighlifting.weightliftingviewer.model.domain;
import javax.persistence.*;
import java.sql.Date;
@Entity
@Table(name = "passport_data")
public class Person {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "ipn", unique = true)
private long ipn;
@Column(name = "passport_number", unique = true)
private String passportNumber;
@Column(name = "name")
private String name;
@Column(name = "last_name")
private String lastName;
@Column(name = "born")
private java.sql.Date born;
public String genderValue;
@OneToOne
// @Column(name = "gender_id")
private Gender gender;
public Person(){
}
public Person(long ipn, String passportNumber, String name, String lastName, Date born, Gender gender) {
this.ipn = ipn;
this.passportNumber = passportNumber;
this.name = name;
this.lastName = lastName;
this.born = born;
this.gender = gender;
this.genderValue = gender.getGender();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getIpn() {
return ipn;
}
public void setIpn(long ipn) {
this.ipn = ipn;
}
public String getPassportNumber() {
return passportNumber;
}
public void setPassportNumber(String passportNumber) {
this.passportNumber = passportNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public java.sql.Date getBorn() {
return born;
}
public void setBorn(java.sql.Date born) {
this.born = born;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public String getGenderValue(){return genderValue;}
}
Full Gender class
@Entity
@Table(name = "gender")
public class Gender {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "gender")
private String gender;
public Gender(){
}
public long getId() {
return id;
}
public Gender(GenderEnum genderEnum) {
this.gender = genderEnum.name();
}
public void setId(long id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
java javafx
java javafx
edited Nov 23 '18 at 13:00
asked Nov 23 '18 at 11:08
Valentyn Hruzytskyi
30813
30813
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Simply implement the cellValueFactory
on your own:
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
// genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getGender().getGender()));
Alternatively you could use the cellFactory
to customize how a Gender
object is displayed:
public class GenderTableCell<T> extends TableCell<T, Gender> {
@Override
protected void updateItem(Gender item, boolean empty) {
super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getGender());
}
}
You could use the updateItem
method instead to e.g. update a ImageView
used as TableCell.graphic
to display gender symbols instead...
TableColumn<Person, Gender> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellFactory(col -> new GenderTableCell<>());
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
add a comment |
You are trying to display nested value. JavaFx does not provide explicit nested values handling like "person.gender". You need to add getter method to Person which will act as Gender String provider.
Btw - although it works, using PropertyValueFactory is not the best choice.
public class Gender {
private String value;
public Gender(String value) {
this.value = value;
}
public String getGender() {
return value;
}
}
public class Person {
private String name;
private Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public Gender getGender() {
return gender;
}
public String getGenderValue() {
return gender.getGender();
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class NestedPropertyApp extends Application {
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Peter", new Gender("Male")));
TableView<Person> tableView = new TableView<>(persons);
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("genderValue"));
tableView.getColumns().add(nameColumn);
tableView.getColumns().add(genderColumn);
stage.setScene(new Scene(tableView));
stage.show();
}
}
Thanks, but it is not work for me(. I even create StringgenderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay inGender
column
– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Done! I think, that caus inprivate ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate thePerson
class and to be using only for display data to table.
– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445562%2fhow-to-set-value-in-tableview-from-entity-using-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Simply implement the cellValueFactory
on your own:
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
// genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getGender().getGender()));
Alternatively you could use the cellFactory
to customize how a Gender
object is displayed:
public class GenderTableCell<T> extends TableCell<T, Gender> {
@Override
protected void updateItem(Gender item, boolean empty) {
super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getGender());
}
}
You could use the updateItem
method instead to e.g. update a ImageView
used as TableCell.graphic
to display gender symbols instead...
TableColumn<Person, Gender> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellFactory(col -> new GenderTableCell<>());
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
add a comment |
Simply implement the cellValueFactory
on your own:
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
// genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getGender().getGender()));
Alternatively you could use the cellFactory
to customize how a Gender
object is displayed:
public class GenderTableCell<T> extends TableCell<T, Gender> {
@Override
protected void updateItem(Gender item, boolean empty) {
super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getGender());
}
}
You could use the updateItem
method instead to e.g. update a ImageView
used as TableCell.graphic
to display gender symbols instead...
TableColumn<Person, Gender> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellFactory(col -> new GenderTableCell<>());
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
add a comment |
Simply implement the cellValueFactory
on your own:
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
// genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getGender().getGender()));
Alternatively you could use the cellFactory
to customize how a Gender
object is displayed:
public class GenderTableCell<T> extends TableCell<T, Gender> {
@Override
protected void updateItem(Gender item, boolean empty) {
super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getGender());
}
}
You could use the updateItem
method instead to e.g. update a ImageView
used as TableCell.graphic
to display gender symbols instead...
TableColumn<Person, Gender> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellFactory(col -> new GenderTableCell<>());
Simply implement the cellValueFactory
on your own:
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
// genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getGender().getGender()));
Alternatively you could use the cellFactory
to customize how a Gender
object is displayed:
public class GenderTableCell<T> extends TableCell<T, Gender> {
@Override
protected void updateItem(Gender item, boolean empty) {
super.updateItem(item, empty);
setText(empty || item == null ? "" : item.getGender());
}
}
You could use the updateItem
method instead to e.g. update a ImageView
used as TableCell.graphic
to display gender symbols instead...
TableColumn<Person, Gender> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellFactory(col -> new GenderTableCell<>());
answered Nov 23 '18 at 13:13
fabian
50.5k115272
50.5k115272
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
add a comment |
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
Thank you! Its work!
– Valentyn Hruzytskyi
Nov 23 '18 at 13:17
add a comment |
You are trying to display nested value. JavaFx does not provide explicit nested values handling like "person.gender". You need to add getter method to Person which will act as Gender String provider.
Btw - although it works, using PropertyValueFactory is not the best choice.
public class Gender {
private String value;
public Gender(String value) {
this.value = value;
}
public String getGender() {
return value;
}
}
public class Person {
private String name;
private Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public Gender getGender() {
return gender;
}
public String getGenderValue() {
return gender.getGender();
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class NestedPropertyApp extends Application {
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Peter", new Gender("Male")));
TableView<Person> tableView = new TableView<>(persons);
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("genderValue"));
tableView.getColumns().add(nameColumn);
tableView.getColumns().add(genderColumn);
stage.setScene(new Scene(tableView));
stage.show();
}
}
Thanks, but it is not work for me(. I even create StringgenderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay inGender
column
– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Done! I think, that caus inprivate ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate thePerson
class and to be using only for display data to table.
– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
add a comment |
You are trying to display nested value. JavaFx does not provide explicit nested values handling like "person.gender". You need to add getter method to Person which will act as Gender String provider.
Btw - although it works, using PropertyValueFactory is not the best choice.
public class Gender {
private String value;
public Gender(String value) {
this.value = value;
}
public String getGender() {
return value;
}
}
public class Person {
private String name;
private Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public Gender getGender() {
return gender;
}
public String getGenderValue() {
return gender.getGender();
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class NestedPropertyApp extends Application {
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Peter", new Gender("Male")));
TableView<Person> tableView = new TableView<>(persons);
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("genderValue"));
tableView.getColumns().add(nameColumn);
tableView.getColumns().add(genderColumn);
stage.setScene(new Scene(tableView));
stage.show();
}
}
Thanks, but it is not work for me(. I even create StringgenderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay inGender
column
– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Done! I think, that caus inprivate ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate thePerson
class and to be using only for display data to table.
– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
add a comment |
You are trying to display nested value. JavaFx does not provide explicit nested values handling like "person.gender". You need to add getter method to Person which will act as Gender String provider.
Btw - although it works, using PropertyValueFactory is not the best choice.
public class Gender {
private String value;
public Gender(String value) {
this.value = value;
}
public String getGender() {
return value;
}
}
public class Person {
private String name;
private Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public Gender getGender() {
return gender;
}
public String getGenderValue() {
return gender.getGender();
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class NestedPropertyApp extends Application {
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Peter", new Gender("Male")));
TableView<Person> tableView = new TableView<>(persons);
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("genderValue"));
tableView.getColumns().add(nameColumn);
tableView.getColumns().add(genderColumn);
stage.setScene(new Scene(tableView));
stage.show();
}
}
You are trying to display nested value. JavaFx does not provide explicit nested values handling like "person.gender". You need to add getter method to Person which will act as Gender String provider.
Btw - although it works, using PropertyValueFactory is not the best choice.
public class Gender {
private String value;
public Gender(String value) {
this.value = value;
}
public String getGender() {
return value;
}
}
public class Person {
private String name;
private Gender gender;
public Person(String name, Gender gender) {
this.name = name;
this.gender = gender;
}
public String getName() {
return name;
}
public Gender getGender() {
return gender;
}
public String getGenderValue() {
return gender.getGender();
}
}
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class NestedPropertyApp extends Application {
public static void main(String args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
ObservableList<Person> persons = FXCollections.observableArrayList();
persons.add(new Person("Peter", new Gender("Male")));
TableView<Person> tableView = new TableView<>(persons);
TableColumn<Person, String> nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("genderValue"));
tableView.getColumns().add(nameColumn);
tableView.getColumns().add(genderColumn);
stage.setScene(new Scene(tableView));
stage.show();
}
}
answered Nov 23 '18 at 12:23
Przemek Krysztofiak
1544
1544
Thanks, but it is not work for me(. I even create StringgenderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay inGender
column
– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Done! I think, that caus inprivate ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate thePerson
class and to be using only for display data to table.
– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
add a comment |
Thanks, but it is not work for me(. I even create StringgenderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay inGender
column
– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Done! I think, that caus inprivate ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate thePerson
class and to be using only for display data to table.
– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
Thanks, but it is not work for me(. I even create String
genderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay in Gender
column– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Thanks, but it is not work for me(. I even create String
genderValue
field, that initialize in constructor, and all works well, but in table nothing not disblay in Gender
column– Valentyn Hruzytskyi
Nov 23 '18 at 12:45
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Please paste your getters from Person class. Or maybe even the whole Person class.
– Przemek Krysztofiak
Nov 23 '18 at 12:52
Done! I think, that caus in
private ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate the Person
class and to be using only for display data to table.– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
Done! I think, that caus in
private ObservableList<Person> data;
string. So, I am should be try to create one more class, that will by dublicate the Person
class and to be using only for display data to table.– Valentyn Hruzytskyi
Nov 23 '18 at 13:13
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
That is a really good idea.
– Przemek Krysztofiak
Nov 23 '18 at 13:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445562%2fhow-to-set-value-in-tableview-from-entity-using-javafx%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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