JavaFX 添加数据到 table
JavaFX add data to a table
我正在尝试将数据添加到 JavaFX table。
我有一个 class GameOfThronesCharacter。我做了几个它的实例,并试图将它的数据插入到 JavaFX table.
table 是在 FX scenebuilder 中制作的,fxml 文件是在 main 中链接的。
这是实践,我计划将它链接到 mysql table,而不是在我完成这部分后对 GoTC 进行硬编码。
table 的表现很奇怪,就像创建第二个 table 一样。
https://youtu.be/1JVraOXVPIk
我不知道我搞砸了什么,有人愿意帮助我吗?这是代码:
Main.java
public class Main extends Application {
static Stage window;
static Scene FrontEnd;
@Override
public void start(Stage primaryStage)throws IOException {
window = primaryStage;
initialize();
window.setTitle("Database Interface v1.0");
window.setResizable(true);
window.setScene(FrontEnd);
window.show();
}
private void initialize() throws IOException {
FrontEnd = new Scene(FXMLLoader.load(getClass().getResource("Frontend.fxml")));
FrontEnd.getStylesheets().add("application.css");
}
public static void main(String[] args) {
launch(args);
}
}
FrontEnd.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<Pane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FrontEndController">
<children>
<TableView fx:id="table">
<columns>
<TableColumn fx:id="deletionColumn" editable="false" prefWidth="75.0" text="Delete" />
<TableColumn fx:id="nameColumn" editable="false" prefWidth="75.0" text="Name" />
<TableColumn fx:id="allegianceColumn" editable="false" prefWidth="75.0" text="Allegiance" />
<TableColumn fx:id="positionColumn" editable="false" prefWidth="75.0" text="Position" />
<TableColumn fx:id="salaryColumn" editable="false" prefWidth="75.0" text="Salary" />
</columns>
</TableView>
</children>
</Pane>
FrontEndController.java
public class FrontEndController implements Initializable {
public TableView<GameOfThronesCharacter> table;
public TableColumn<GameOfThronesCharacter,String> nameColumn;
public TableColumn<GameOfThronesCharacter,String> allegianceColumn;
public TableColumn<GameOfThronesCharacter,String> positionColumn;
public TableColumn<GameOfThronesCharacter,Double> salaryColumn;
private ObservableList<GameOfThronesCharacter> getCharacters(){
ObservableList<GameOfThronesCharacter> characters = FXCollections.observableArrayList();
characters.add(new GameOfThronesCharacter("Cersei","Lannister","Queen Regent",100000));
characters.add(new GameOfThronesCharacter("Jaime","Lannister","King Slayer",80000));
characters.add(new GameOfThronesCharacter("Tyrion","Lannister","Queen's Hand",60000));
return characters;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//nameColumn
nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//allegianceColumn
allegianceColumn = new TableColumn<>("Allegiance");
allegianceColumn.setCellValueFactory(new PropertyValueFactory<>("allegiance"));
//positionColumn
positionColumn = new TableColumn<>("Position");
positionColumn.setCellValueFactory(new PropertyValueFactory<>("position"));
//salaryColumn
salaryColumn = new TableColumn<>("Salary");
salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
//table = new TableView<>();
table.setItems(getCharacters());
table.getColumns().addAll(nameColumn,allegianceColumn,positionColumn,salaryColumn);
}
}
GameOfThronesCharacter.java
public class GameOfThronesCharacter {
private String name;
private String allegiance;
private String position;
private double salary;
public GameOfThronesCharacter(){
}
public GameOfThronesCharacter(String name, String allegiance, String position, double salary){
setName(name);
setAllegiance(allegiance);
setPosition(position);
setSalary(salary);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAllegiance() {
return allegiance;
}
public void setAllegiance (String allegiance){
this.allegiance = allegiance;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
在 FXML 文件中,您创建 table 列并将它们添加到 table 的 columns
列表中:
<TableView fx:id="table">
<columns>
<TableColumn fx:id="deletionColumn" editable="false" prefWidth="75.0" text="Delete" />
<TableColumn fx:id="nameColumn" editable="false" prefWidth="75.0" text="Name" />
<TableColumn fx:id="allegianceColumn" editable="false" prefWidth="75.0" text="Allegiance" />
<TableColumn fx:id="positionColumn" editable="false" prefWidth="75.0" text="Position" />
<TableColumn fx:id="salaryColumn" editable="false" prefWidth="75.0" text="Salary" />
</columns>
</TableView>
然后,在控制器中,您创建一组全新的列,并将所有列添加到 table 的 columns
列表中:
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//nameColumn
nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//allegianceColumn
allegianceColumn = new TableColumn<>("Allegiance");
allegianceColumn.setCellValueFactory(new PropertyValueFactory<>("allegiance"));
//positionColumn
positionColumn = new TableColumn<>("Position");
positionColumn.setCellValueFactory(new PropertyValueFactory<>("position"));
//salaryColumn
salaryColumn = new TableColumn<>("Salary");
salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
//table = new TableView<>();
table.setItems(getCharacters());
table.getColumns().addAll(nameColumn,allegianceColumn,positionColumn,salaryColumn);
}
因此,毫不奇怪,您最终会得到一组重复的列。
只需删除创建第二组列的代码。 (顺便说一句,最好使字段 public,并通过注释 @FXML
使它们可供 FXMLLoader
访问:
public class FrontEndController implements Initializable {
@FXML private TableView<GameOfThronesCharacter> table;
@FXML private TableColumn<GameOfThronesCharacter,String> nameColumn;
@FXML private TableColumn<GameOfThronesCharacter,String> allegianceColumn;
@FXML private TableColumn<GameOfThronesCharacter,String> positionColumn;
@FXML private TableColumn<GameOfThronesCharacter,Double> salaryColumn;
private ObservableList<GameOfThronesCharacter> getCharacters(){
ObservableList<GameOfThronesCharacter> characters = FXCollections.observableArrayList();
characters.add(new GameOfThronesCharacter("Cersei","Lannister","Queen Regent",100000));
characters.add(new GameOfThronesCharacter("Jaime","Lannister","King Slayer",80000));
characters.add(new GameOfThronesCharacter("Tyrion","Lannister","Queen's Hand",60000));
return characters;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//nameColumn
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//allegianceColumn
allegianceColumn.setCellValueFactory(new PropertyValueFactory<>("allegiance"));
//positionColumn
positionColumn.setCellValueFactory(new PropertyValueFactory<>("position"));
//salaryColumn
salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
//table = new TableView<>();
table.setItems(getCharacters());
}
}
我正在尝试将数据添加到 JavaFX table。
我有一个 class GameOfThronesCharacter。我做了几个它的实例,并试图将它的数据插入到 JavaFX table.
table 是在 FX scenebuilder 中制作的,fxml 文件是在 main 中链接的。 这是实践,我计划将它链接到 mysql table,而不是在我完成这部分后对 GoTC 进行硬编码。
table 的表现很奇怪,就像创建第二个 table 一样。 https://youtu.be/1JVraOXVPIk
我不知道我搞砸了什么,有人愿意帮助我吗?这是代码:
Main.java
public class Main extends Application {
static Stage window;
static Scene FrontEnd;
@Override
public void start(Stage primaryStage)throws IOException {
window = primaryStage;
initialize();
window.setTitle("Database Interface v1.0");
window.setResizable(true);
window.setScene(FrontEnd);
window.show();
}
private void initialize() throws IOException {
FrontEnd = new Scene(FXMLLoader.load(getClass().getResource("Frontend.fxml")));
FrontEnd.getStylesheets().add("application.css");
}
public static void main(String[] args) {
launch(args);
}
}
FrontEnd.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<Pane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FrontEndController">
<children>
<TableView fx:id="table">
<columns>
<TableColumn fx:id="deletionColumn" editable="false" prefWidth="75.0" text="Delete" />
<TableColumn fx:id="nameColumn" editable="false" prefWidth="75.0" text="Name" />
<TableColumn fx:id="allegianceColumn" editable="false" prefWidth="75.0" text="Allegiance" />
<TableColumn fx:id="positionColumn" editable="false" prefWidth="75.0" text="Position" />
<TableColumn fx:id="salaryColumn" editable="false" prefWidth="75.0" text="Salary" />
</columns>
</TableView>
</children>
</Pane>
FrontEndController.java
public class FrontEndController implements Initializable {
public TableView<GameOfThronesCharacter> table;
public TableColumn<GameOfThronesCharacter,String> nameColumn;
public TableColumn<GameOfThronesCharacter,String> allegianceColumn;
public TableColumn<GameOfThronesCharacter,String> positionColumn;
public TableColumn<GameOfThronesCharacter,Double> salaryColumn;
private ObservableList<GameOfThronesCharacter> getCharacters(){
ObservableList<GameOfThronesCharacter> characters = FXCollections.observableArrayList();
characters.add(new GameOfThronesCharacter("Cersei","Lannister","Queen Regent",100000));
characters.add(new GameOfThronesCharacter("Jaime","Lannister","King Slayer",80000));
characters.add(new GameOfThronesCharacter("Tyrion","Lannister","Queen's Hand",60000));
return characters;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//nameColumn
nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//allegianceColumn
allegianceColumn = new TableColumn<>("Allegiance");
allegianceColumn.setCellValueFactory(new PropertyValueFactory<>("allegiance"));
//positionColumn
positionColumn = new TableColumn<>("Position");
positionColumn.setCellValueFactory(new PropertyValueFactory<>("position"));
//salaryColumn
salaryColumn = new TableColumn<>("Salary");
salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
//table = new TableView<>();
table.setItems(getCharacters());
table.getColumns().addAll(nameColumn,allegianceColumn,positionColumn,salaryColumn);
}
}
GameOfThronesCharacter.java
public class GameOfThronesCharacter {
private String name;
private String allegiance;
private String position;
private double salary;
public GameOfThronesCharacter(){
}
public GameOfThronesCharacter(String name, String allegiance, String position, double salary){
setName(name);
setAllegiance(allegiance);
setPosition(position);
setSalary(salary);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAllegiance() {
return allegiance;
}
public void setAllegiance (String allegiance){
this.allegiance = allegiance;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
在 FXML 文件中,您创建 table 列并将它们添加到 table 的 columns
列表中:
<TableView fx:id="table">
<columns>
<TableColumn fx:id="deletionColumn" editable="false" prefWidth="75.0" text="Delete" />
<TableColumn fx:id="nameColumn" editable="false" prefWidth="75.0" text="Name" />
<TableColumn fx:id="allegianceColumn" editable="false" prefWidth="75.0" text="Allegiance" />
<TableColumn fx:id="positionColumn" editable="false" prefWidth="75.0" text="Position" />
<TableColumn fx:id="salaryColumn" editable="false" prefWidth="75.0" text="Salary" />
</columns>
</TableView>
然后,在控制器中,您创建一组全新的列,并将所有列添加到 table 的 columns
列表中:
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//nameColumn
nameColumn = new TableColumn<>("Name");
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//allegianceColumn
allegianceColumn = new TableColumn<>("Allegiance");
allegianceColumn.setCellValueFactory(new PropertyValueFactory<>("allegiance"));
//positionColumn
positionColumn = new TableColumn<>("Position");
positionColumn.setCellValueFactory(new PropertyValueFactory<>("position"));
//salaryColumn
salaryColumn = new TableColumn<>("Salary");
salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
//table = new TableView<>();
table.setItems(getCharacters());
table.getColumns().addAll(nameColumn,allegianceColumn,positionColumn,salaryColumn);
}
因此,毫不奇怪,您最终会得到一组重复的列。
只需删除创建第二组列的代码。 (顺便说一句,最好使字段 public,并通过注释 @FXML
使它们可供 FXMLLoader
访问:
public class FrontEndController implements Initializable {
@FXML private TableView<GameOfThronesCharacter> table;
@FXML private TableColumn<GameOfThronesCharacter,String> nameColumn;
@FXML private TableColumn<GameOfThronesCharacter,String> allegianceColumn;
@FXML private TableColumn<GameOfThronesCharacter,String> positionColumn;
@FXML private TableColumn<GameOfThronesCharacter,Double> salaryColumn;
private ObservableList<GameOfThronesCharacter> getCharacters(){
ObservableList<GameOfThronesCharacter> characters = FXCollections.observableArrayList();
characters.add(new GameOfThronesCharacter("Cersei","Lannister","Queen Regent",100000));
characters.add(new GameOfThronesCharacter("Jaime","Lannister","King Slayer",80000));
characters.add(new GameOfThronesCharacter("Tyrion","Lannister","Queen's Hand",60000));
return characters;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
//nameColumn
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//allegianceColumn
allegianceColumn.setCellValueFactory(new PropertyValueFactory<>("allegiance"));
//positionColumn
positionColumn.setCellValueFactory(new PropertyValueFactory<>("position"));
//salaryColumn
salaryColumn.setCellValueFactory(new PropertyValueFactory<>("salary"));
//table = new TableView<>();
table.setItems(getCharacters());
}
}