我已经弄清楚如何创建 TableView
以及如何向其中添加内容,但我对如何编辑现有内容感到困惑。
在这个应用程序中,我已经对其进行了设置,以便人们可以将玩家姓名和某些统计数据添加到桌面 View 中,但我现在希望能够添加随机滚动并向其添加 dex 和其他修饰符,并用它填充桌面 View 。这将驻留在 rollInitBtnClicked()
模块中。
请帮助我了解我需要做什么。
这是我的Main
类。
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
TextField playerName;
TextField dexMod;
TextField otherMod;
TableView<Players> playersTable;
Button addPlayerBtn;
Button rollInitBtn;
Button delPlayerBtn;
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Initiative");
// TextField so the user can enter a players Name
playerName = new TextField();
playerName.setPrefSize(200, 20);
playerName.setPromptText("Player Name");
//playerName.setFocusTraversable(false);
// TextField so the user can enter the players dex mod
dexMod = new TextField();
dexMod.setPrefSize(50, 20);
dexMod.setPromptText("Dex");
//dexMod.setFocusTraversable(false);
// TextField so the user can enter the players other mods
otherMod = new TextField();
otherMod.setPrefSize(50, 20);
otherMod.setPromptText("Other");
//otherMod.setFocusTraversable(false);
addPlayerBtn = new Button("Add Player");
addPlayerBtn.setOnAction(e -> addPlayerBtnClicked());
delPlayerBtn = new Button("Delete Player");
delPlayerBtn.setOnAction(e -> delPlayerBtnClicked());
rollInitBtn = new Button("Roll Initiative");
rollInitBtn.setOnAction(e -> rollInitBtnClicked());
//TableView Name Columns
TableColumn<Players, String> nameColumn = new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
//TableView dexMod Columns
TableColumn<Players, String> dexColumn = new TableColumn<>("Dex Mod");
dexColumn.setMinWidth(50);
dexColumn.setStyle( "-fx-alignment: CENTER;");
dexColumn.setCellValueFactory(new PropertyValueFactory<>("dexMod"));
//TableView otherMod Columns
TableColumn<Players, String> otherColumn = new TableColumn<>("Other Mods");
otherColumn.setMinWidth(50);
otherColumn.setStyle( "-fx-alignment: CENTER;");
otherColumn.setCellValueFactory(new PropertyValueFactory<>("otherMod"));
//TableView roll Columns
TableColumn<Players, String> rollColumn = new TableColumn<>("Die Roll");
rollColumn.setMinWidth(50);
rollColumn.setStyle( "-fx-alignment: CENTER;");
rollColumn.setCellValueFactory(new PropertyValueFactory<>("roll"));
//TableView total Columns
TableColumn<Players, String> totalColumn = new TableColumn<>("Total");
totalColumn.setMinWidth(50);
totalColumn.setStyle( "-fx-alignment: CENTER;");
totalColumn.setCellValueFactory(new PropertyValueFactory<>("total"));
playersTable = new TableView<>();
playersTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
playersTable.setItems(getPlayers());
playersTable.getColumns().addAll(nameColumn, dexColumn, otherColumn, rollColumn, totalColumn);
HBox hLayout = new HBox();
hLayout.setPadding(new Insets(10, 10, 10, 10)); // sets the padding on the top right left and bottom of the layout
hLayout.setSpacing(10); // sets the spacing between the TextFields and buttons
hLayout.getChildren().addAll(playerName, dexMod, otherMod, addPlayerBtn, rollInitBtn, delPlayerBtn);
VBox vLayout = new VBox();
vLayout.getChildren().addAll(playersTable, hLayout);
Scene mainScene = new Scene(vLayout, 800, 400);
primaryStage.setScene(mainScene);
primaryStage.show();
}
// What happens when the user clicks the Add Player button
public void addPlayerBtnClicked() {
Players players = new Players();
players.setName(playerName.getText());
players.setDexMod(Integer.parseInt(dexMod.getText()));
players.setOtherMod(Integer.parseInt(otherMod.getText()));
playersTable.getItems().add(players);
playerName.clear();
dexMod.clear();
otherMod.clear();
}
// What happens when the user clicks the Delete Player button
public void delPlayerBtnClicked() {
ObservableList<Players> playerSelected, allPlayers;
allPlayers = playersTable.getItems();
playerSelected = playersTable.getSelectionModel().getSelectedItems();
playerSelected.forEach(allPlayers::remove);
}
public void rollInitBtnClicked() {
Players players = new Players();
ObservableList<Players> playerSelected, allPlayers;
allPlayers = playersTable.getItems();
playerSelected = playersTable.getSelectionModel().getSelectedItems();
players.setRoll((int)Math.ceil(Math.random() * 20));
}
public ObservableList<Players> getPlayers(){
ObservableList<Players> players = FXCollections.observableArrayList();
return players;
}
}
以下内容位于名为 Players
的类中。
public class Players {
private String name;
private int dexMod;
private int otherMod;
private int roll;
private int total;
// default method to enter default data
public Players(){
}
// overloaded method to enter data that is handed to it
public Players(String name, int dexMod, int otherMod, int roll, int total){
this.name = name;
this.dexMod = dexMod;
this.otherMod = otherMod;
this.roll = roll;
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDexMod() {
return dexMod;
}
public void setDexMod(int dexMod) {
this.dexMod = dexMod;
}
public int getOtherMod() {
return otherMod;
}
public void setOtherMod(int otherMod) {
this.otherMod = otherMod;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
}
最佳答案
对于TableView
要获得修改通知,您需要创建 ObservableValue
包装收到修改通知的可用属性值。 PropertyValueFactory
使用方法<propertyName>Property()
检索此ObservableValue
。如果不存在这样的方法,则使用 getter;然而在这种情况下TableView
没有通知任何修改。此外total
属性本身不应被修改。
从 JavaFX 8u60 开始,有一个 refresh
TableView
中的方法,这将允许您手动触发刷新:
private final Random random = new Random();
public void rollInitBtnClicked() {
List<Players> targets = playersTable.getItems();
for (Players p : targets) {
p.setRoll(random.nextInt(20));
// set total ect...
}
// update table
playersTable.refresh();
}
但是,“也与旧版本 JavaFX 兼容的标准方法将修改属性:
private final IntegerProperty roll = new SimpleIntegerProperty();
public int getRoll() {
return roll.get();
}
public void setRoll(int roll) {
this.roll.set(roll);
}
public IntegerProperty rollProperty() {
return this.roll;
}
// same modifications for other int properties
...
private final IntegerBinding total = Bindings.createIntegerBinding(() -> getRoll() + getOtherMod() + getDexMod(), roll, otherMod, dexMod);
public int getTotal() {
return this.total.get();
}
public IntegerBinding totalProperty() {
return this.total;
}
这将允许您删除 refresh
调用rollInitBtnClicked
上面发布了方法。
附加说明
- 如果您使用类来存储单个玩家的数据,则不应将其命名为
Players
,但是Player
. 您的
TableColumn
使用了不正确的类型参数s。这在这里并不重要,因为PropertyValueFactory
无论类型参数如何,都可以工作,但如果您使用TableColumn
的其他功能,它可能会给您带来问题。 。第二个类型参数应始终与ObservableValue
的类型参数匹配。由cellValueFactory
返回:TableColumn<Players, String> nameColumn TableColumn<Players, Number> dexColumn TableColumn<Players, Number> otherColumn TableColumn<Players, Number> rollColumn TableColumn<Players, Number> totalColumn
关于JavaFX:如何编辑现有的 TableView 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43816685/