JavaFX15 TableView<S>中TableColumn<S, T>

JavaFX15中TableView有泛型限制而JavaFX8版本没有。
关于这个问题国内解答较少所以我有了写本篇的想法,我参考了JavaFX15的文档,成功地解决了这个问题。

ProductColumnItem.java

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

public class ProductColumnItem {
    private StringProperty name;
    private LongProperty price;

    public ProductColumnItem(String name, long price) {
        setName(name);
        setPrice(price);
    }

    public StringProperty nameProperty() {
        if (this.name == null) this.name = new SimpleStringProperty(this, "name");
        return this.name;
    }

    public LongProperty priceProperty() {
        if (this.price == null) this.price = new SimpleLongProperty(this, "price");
        return this.price;
    }

    public void setName(String name) {
        nameProperty().set(name);
    }
    
    public void setPrice(Long price) {
        priceProperty().set(price);
    }

    public String getName() {
        return nameProperty().get();
    }

    public Long getPrice() {
        return priceProperty().get();
    }
}

MainApp.java

import java.util.List;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MainApp extends Application {
    /*
     * The main() method is ignored in correctly deployed JavaFX application. main()
     * serves only as fallback in case the application can not be launched through
     * deployment artifacts, e.g., in IDEs with limited FX support. NetBeans ignores
     * main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        List<ProductColumnItem> labelData = List.of(
            new ProductColumnItem("pen", 2),
            new ProductColumnItem("knife", 10)
        );
        ObservableList<ProductColumnItem> labelDataMembers = FXCollections.observableArrayList(labelData);
        TableView<ProductColumnItem> tableView = new TableView<>();
        tableView.setItems(labelDataMembers);
        TableColumn<ProductColumnItem, String> nameColumn = new TableColumn<>("name");
        nameColumn.setCellValueFactory(new PropertyValueFactory<>(labelData.get(0).nameProperty().getName()));
        TableColumn<ProductColumnItem, Long> priceColumn = new TableColumn<>("price");
        priceColumn.setCellValueFactory(new PropertyValueFactory<>(labelData.get(0).priceProperty().getName()));
        tableView.getColumns().setAll(nameColumn, priceColumn);
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 400);
        root.getChildren().add(tableView);
        stage.setScene(scene);
        stage.setTitle("hello");
        stage.show();
    }
}

效果图:
screen

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALoppd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值