我无法将数据加载到表格中。我的课程Player
包含name
,lastName
等。我想在文本字段中将name
,lastname
插入到表播放器中。
public class CoreAppFXMLController implements Initializable {
@FXML
private Button addBtn;
@FXML
private Button deleteBtn;
@FXML
private Tab playersTab;
@FXML
private Tab coachesTab;
@FXML
private TextField playerNameFld;
@FXML
private TextField playerLNFld;
@FXML
private TextField playerNumberFld;
@FXML
private TextField playerPointsFld;
@FXML
private ChoiceBox<String> positionCb;
@FXML
private TableColumn<Player, String> playerNameCol;
@FXML
private TableColumn<Player, String> playerLNCol;
@FXML
private TableColumn<Player, String> playerNumberCol;
@FXML
private TableColumn<Player, String> playerPointsCol;
@FXML
private TableView<Player> playerTable;
ObservableList<Player> data = FXCollections.observableArrayList();
public class Player {
private String name;
private String lastName;
private String number;
private String points;
public Player(String name, String lastName, String number, String points) {
this.name = name;
this.lastName = lastName;
this.number = number;
this.points = points;
}
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 String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getPoints() {
return points;
}
public void setPoints(String points) {
this.points = points;
}
}
/**
* Initializes the controller class.
*/
public void initialize(URL url, ResourceBundle rb) {
positionCb.setItems(FXCollections.observableArrayList("PLAYMAKER",
"SHOOTER", "WING", "CENTER", "CENTER-WING"));
playerNameCol
.setCellValueFactory(new PropertyValueFactory<Player, String>(
"name"));
playerLNCol
.setCellValueFactory(new PropertyValueFactory<Player, String>(
"lastName"));
playerNumberCol
.setCellValueFactory(new PropertyValueFactory<Player, String>(
"number"));
playerPointsCol
.setCellValueFactory(new PropertyValueFactory<Player, String>(
"points"));
playerTable.setItems(data);
}
public void addBtnAction(ActionEvent event) {
data.add(new Player(playerNameFld.getText(), playerLNFld.getText(),
playerNumberFld.getText(), playerPointsFld.getText()));
playerNameFld.clear();
playerLNFld.clear();
playerNumberFld.clear();
playerPointsFld.clear();
}
public void deleteBtnAction(ActionEvent event) {
}
}
我做的与此处显示的完全相同:http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm#CJAGAAEE
但我无法让它发挥作用。任何人都可以帮助我吗?
答案 0 :(得分:0)
它与示例不完全相同,如果您使用的是Java 7,那么无论如何它都可能无法正常工作。尝试设置Player
类以使用SimpleStringProperty
作为成员变量,然后为每个返回成员的方法添加*Property()
方法。例如:
public class Player
{
private SimpleStringProperty name;
public Player(String name)
{
this.name.set(name);
}
public SimpleStringProperty nameProperty() { return name; }
public String getName() { return name.get(); }
public void setName(String name) { this.name.set(name); }
}
尝试并以相同的方式添加其余成员。