You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX FXML开发:点击按钮无法将用户输入添加到TableView问题求助

JavaFX TableView数据无法添加问题解决方案

1 修复TableView注入错误

你在Controller中手动实例化了TableView,覆盖了FXMLLoader从FXML注入的界面实例,导致你操作的TableView和界面展示的不是同一个对象:

// 错误写法
@FXML
private TableView<Person> table = new TableView<Person>();

// 修正后写法,不要手动new,由FXMLLoader自动注入
@FXML
private TableView<Person> table;

2 修正Toggle选中值的获取逻辑

你直接把Toggle对象传入了Person构造方法,应该获取选中ToggleButton的文本值,同时补充基础空校验避免空指针:

public void save1(ActionEvent e){
    // 非空校验,避免未输入或未选择时触发空指针
    if (fname.getText().trim().isEmpty() || lname.getText().trim().isEmpty()
        || address.getText().trim().isEmpty() || speed.getSelectedToggle() == null
        || bandw.getSelectedToggle() == null || duration.getSelectedToggle() == null) {
        // 可自行添加弹窗提示逻辑
        return;
    }
    // 提取选中Toggle的文本值
    String speedVal = ((ToggleButton)speed.getSelectedToggle()).getText();
    String bandwVal = ((ToggleButton)bandw.getSelectedToggle()).getText();
    String durationVal = ((ToggleButton)duration.getSelectedToggle()).getText();
    // 直接向TableView的items列表添加数据即可,不需要重复调用setItems
    table.getItems().add(new Person(
        fname.getText().trim(),
        lname.getText().trim(),
        address.getText().trim(),
        speedVal,
        bandwVal,
        durationVal
    ));
    // 可选:添加成功后清空输入项,方便下次输入
    fname.clear();
    lname.clear();
    address.clear();
    speed.selectToggle(null);
    bandw.selectToggle(null);
    duration.selectToggle(null);
}

3 给TableColumn绑定Person类的属性

你的TableColumn没有配置数据映射规则,就算数据成功添加到TableView,列也不知道要展示Person的哪个字段,两种实现方式二选一即可:

方式1:在FXML中直接配置cellValueFactory

给每个<TableColumn>标签添加cellValueFactory属性,属性值对应Person类的属性名:

<TableColumn fx:id="firstn" prefWidth="75.0" text="First Name" cellValueFactory="${javafx.scene.control.cell.PropertyValueFactory('firstName')}" />
<TableColumn fx:id="lastn" prefWidth="75.0" text="Last Name" cellValueFactory="${javafx.scene.control.cell.PropertyValueFactory('lastName')}" />
<TableColumn fx:id="addresss" prefWidth="75.0" text="Address" cellValueFactory="${javafx.scene.control.cell.PropertyValueFactory('address')}" />
<TableColumn fx:id="speeed" prefWidth="84.0" text="Speed(mb/s)" cellValueFactory="${javafx.scene.control.cell.PropertyValueFactory('speed')}" />
<TableColumn fx:id="bwidth" prefWidth="75.0" text="Bandwidth" cellValueFactory="${javafx.scene.control.cell.PropertyValueFactory('bandwidth')}" />
<TableColumn fx:id="durationn" prefWidth="75.0" text="Duration" cellValueFactory="${javafx.scene.control.cell.PropertyValueFactory('duration')}" />

方式2:在Controller的initialize方法中绑定

@FXML
private TableColumn<Person, String> firstn;
@FXML
private TableColumn<Person, String> lastn;
@FXML
private TableColumn<Person, String> addresss;
@FXML
private TableColumn<Person, String> speeed;
@FXML
private TableColumn<Person, String> bwidth;
@FXML
private TableColumn<Person, String> durationn;

@FXML
public void initialize() {
    firstn.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    lastn.setCellValueFactory(new PropertyValueFactory<>("lastName"));
    addresss.setCellValueFactory(new PropertyValueFactory<>("address"));
    speeed.setCellValueFactory(new PropertyValueFactory<>("speed"));
    bwidth.setCellValueFactory(new PropertyValueFactory<>("bandwidth"));
    durationn.setCellValueFactory(new PropertyValueFactory<>("duration"));
}

4 确保Person类符合JavaFX数据模型要求

Person类需要提供对应属性的getter方法,属性名要和绑定的字符串完全匹配,推荐使用JavaFX属性模型实现,示例定义如下:

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Person {
    private final StringProperty firstName;
    private final StringProperty lastName;
    private final StringProperty address;
    private final StringProperty speed;
    private final StringProperty bandwidth;
    private final StringProperty duration;

    public Person(String firstName, String lastName, String address, String speed, String bandwidth, String duration) {
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
        this.address = new SimpleStringProperty(address);
        this.speed = new SimpleStringProperty(speed);
        this.bandwidth = new SimpleStringProperty(bandwidth);
        this.duration = new SimpleStringProperty(duration);
    }

    public String getFirstName() {return firstName.get();}
    public void setFirstName(String firstName) {this.firstName.set(firstName);}
    public StringProperty firstNameProperty() {return firstName;}

    // lastName、address、speed、bandwidth、duration均按照上面的格式生成对应的getter、setter和属性方法即可
}

内容的提问来源于stack exchange,提问作者Cookie Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 13:45:03