JavaFX将数据添加到表中

时间:2016-12-23 15:40:18

标签: java mysql javafx scenebuilder

我试图将数据添加到JavaFX表中。

我有一个GameOfThronesCharacter类。我做了几个实例,并试图将数据插入到JavaFX表中。

该表是在FX scenebuilder中制作的,fxml文件是在main中链接的。 这是练习,我计划将它链接到一个mysql表,而不是在我把这个部分放下来硬编码GoTC。

表格表现得非常奇怪,就像创建第二张桌子一样。 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;
    }       
}

1 个答案:

答案 0 :(得分:2)

在FXML文件中,您创建表格列并将其添加到表格的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>

然后,在控制器中,您创建一组全新的列,并将所有列添加到表的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);

}

因此,毫不奇怪,您最终会得到一组重复的列。

只需删除创建第二组列的代码即可。 (顺便说一句,最好将字段设为公开,并通过注释FXMLLoader使@FXML访问它们):

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

    }
}