使用ID向TableView添加项目

时间:2014-05-08 22:23:19

标签: java javafx

我有带有id,name,lastname等的TableView。此外,我还添加了将玩家添加到tableview的按钮。如何在添加btn时添加具有默认ID(0,1,2,3 ...)的播放器。我有Player类:

public class CoreAppFXMLController implements Initializable {

/**
 * tekst_fildovi
 */
@FXML
private TextField playerNameFld;
@FXML
private TextField playerLNFld;
@FXML
private TextField playerNumberFld;
@FXML
private TextField playerPointsFld;
@FXML
private TextField coachesNameFld;
@FXML
private TextField coachesLNFld;
@FXML
private TextField coachesAgeFld;
@FXML
private TextField teamNameFld;
@FXML
private TextField teamFromFld;
@FXML
private TextField teamPointsFld;
/*
 * @FXML private ChoiceBox<Coaches> teamWLFld;
 */
@FXML
private ChoiceBox<String> positionCb;
/**
 * colone
 */
@FXML
private TableColumn<Player, Integer> playerIdCol;
@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 TableColumn<Coaches, String> coachesNameCol;
@FXML
private TableColumn<Coaches, String> coachesLNCol;
@FXML
private TableColumn<Coaches, String> coachesAgeCol;
@FXML
private TableColumn<Teams, String> teamsNameCol;
@FXML
private TableColumn<Teams, String> teamsFromCol;
@FXML
private TableColumn<Teams, String> teamsPointsCol;

/**
 * @param tabele
 */
@FXML
private TableView<Player> playerTable;
@FXML
private TableView<Coaches> coachesTable;
@FXML
private TableView<Teams> teamsTable;

private ObservableList<Player> playerData;

private ObservableList<Coaches> coachesData;

private ObservableList<Teams> teamsData;

public class Player {

    private SimpleIntegerProperty playerId = new SimpleIntegerProperty();
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleStringProperty lastName = new SimpleStringProperty();
    private SimpleStringProperty number = new SimpleStringProperty();
    private SimpleStringProperty points = new SimpleStringProperty();

    public Player(int playerId, String name, String lastName, String number,
            String points) {
        this.playerId = new SimpleIntegerProperty(playerId);
        this.name.setValue(name);
        this.lastName.setValue(lastName);
        this.number.setValue(number);
        this.points.setValue(points);

    }

    public void setId(int id) {
        playerId.set(id);
    }

    public int getId() {
        return playerId.get();
    }

    public String getName() {
        if (name != null)
            return "";
        return name.getValueSafe();
    }

    public String getlastName() {
        if (lastName != null)
            return "";
        return lastName.getValueSafe();
    }

    public String getNumber() {
        if (number != null)
            return "";
        return number.getValueSafe();
    }

    public String getPoints() {
        if (points != null)
            return "";
        return points.getValueSafe();
    }

    public SimpleIntegerProperty idProperty() {
        return playerId;
    }

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public SimpleStringProperty lastNameProperty() {
        return lastName;
    }

    public SimpleStringProperty numberProperty() {
        return number;
    }

    public SimpleStringProperty pointsProperty() {
        return points;
    }

}

/**
 * Initializes the controller class.
 */
public void initialize(URL url, ResourceBundle rb) {

    playerIdCol
            .setCellValueFactory(new PropertyValueFactory<Player, Integer>(
                    "playerId"));

    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"));
    coachesNameCol
            .setCellValueFactory(new PropertyValueFactory<Coaches, String>(
                    "name"));
    coachesLNCol
            .setCellValueFactory(new PropertyValueFactory<Coaches, String>(
                    "lastName"));
    coachesAgeCol
            .setCellValueFactory(new PropertyValueFactory<Coaches, String>(
                    "age"));

    playerData = FXCollections.observableArrayList();
    playerTable.setItems(playerData);
    playerTable.setEditable(false);

    coachesData = FXCollections.observableArrayList();
    coachesTable.setItems(coachesData);
    coachesTable.setEditable(false);

    teamsData = FXCollections.observableArrayList();
    teamsTable.setItems(teamsData);
    teamsTable.setEditable(false);

}

public void addPlayersAction(ActionEvent event) {
    playerData.add(new Player(1, playerNameFld.getText(), playerLNFld
            .getText(), playerNumberFld.getText(), playerPointsFld
            .getText()));
    playerNameFld.clear();
    playerLNFld.clear();
    playerNumberFld.clear();
    playerPointsFld.clear();

}

和我的FXML包含tableview和playerIdCol:

<TableView fx:id="playerTable" prefHeight="295.0" prefWidth="593.0" AnchorPane.bottomAnchor="290.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="219.0">
                              <columns>
                                <TableColumn maxWidth="5000.0" minWidth="80.0" prefWidth="80.0" text="Id" fx:id="playerIdCol" />
                                <TableColumn maxWidth="5000.0" minWidth="180.0" prefWidth="180.0" text="Name" fx:id="playerNameCol" />
                                <TableColumn maxWidth="5000.0" minWidth="180.0" prefWidth="180.0" text="Lastname" fx:id="playerLNCol" />
                                <TableColumn maxWidth="5000.0" minWidth="80.0" prefWidth="80.0" text="Number" fx:id="playerNumberCol" />
                                <TableColumn maxWidth="5000.0" minWidth="80.0" prefWidth="80.0" text="Points" fx:id="playerPointsCol" />
                                <TableColumn maxWidth="5000.0" minWidth="180.0" prefWidth="180.0" text="Position" fx:id="playerPositionCol" />
                              </columns>
                            </TableView>

先谢谢你。

1 个答案:

答案 0 :(得分:0)

这不是JavaFX问题,而是一个Java问题。

您可以创建一个计算Player s

的静态字段
private static int playerCount = 0;

然后

new Player(playerCount++, ...);

有很多不同的方法。你只需要稍微研究一下原理。