gpt4 book ai didi

java - 如何使用 JavaFX 从实体设置 TableView<> 中的值

转载 作者:行者123 更新时间:2023-11-30 01:59:15 28 4
gpt4 key购买 nike

我正在尝试实现以下JavaFXSpring实现,但无法掌握TableView中如何操作和显示实体形式。 (我的意思是 Gender 类。这只是一个实体,代表表,它只能包含两个值: malefemale - 我想在 TableView 中显示这个值)

人:

@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
}

性别

  @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
}

MainController我是实现类TableView<>用于数据成像和编辑表:

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 文件*部分,执行表显示:

<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>

所以,结果我有来自 IPN 的数据专栏和引用 Gender gender 中的对象栏目:

enter image description here

但我需要得到 String gender这些对象的值(value)。在这种情况下如何从对象获取和放置值?

其他信息

全人类

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;}
}

完整性别类别

@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;
}
}

最佳答案

只需自行实现cellValueFactory:

TableColumn<Person, String> genderColumn = new TableColumn<>("Gender");
// genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue().getGender().getGender()));
<小时/>

或者,您可以使用 cellFactory 自定义 Gender 对象的显示方式:

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());
}
}

您可以使用 updateItem 方法来代替,例如更新用作 TableCell.graphicImageView 以显示性别符号...

TableColumn<Person, Gender> genderColumn = new TableColumn<>("Gender");
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
genderColumn.setCellFactory(col -> new GenderTableCell<>());

关于java - 如何使用 JavaFX 从实体设置 TableView<> 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53445562/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com