JavaFX TableView 使用 显示数据 增加 添加 删除 修改 更新 数据

推荐用法

package fx.com;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class Main4 extends Application {
    public static void main(String[] args) {
        Application.launch(Main4.class,args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Data2 d1= new Data2("A",15,70,true);
        Data2 d2= new Data2("B",15,11,false);
        Data2 d3= new Data2("C",18,100,true);
        Data2 d4= new Data2("D",20,52,true);


        ObservableList<Data2> list = FXCollections.observableArrayList();

        //监听列表被改变
//        ObservableList<Data2> list = FXCollections.observableArrayList(new Callback<Data2, Observable[]>() {
//            @Override
//            public Observable[] call(Data2 param) {
//                System.out.println(param.getAgeProperty());
//                return new Observable[0];
//            }
//        });
        list.addAll(d1,d2,d3,d4);
        TableView<Data2> tableView = new TableView<>(list);
        TableColumn<Data2,String> tc_name = new TableColumn<>("姓名");
        tc_name.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<Data2, String> param) {
                return param.getValue().getNameProperty();
            }
        });

        TableColumn<Data2,Number> tc_age = new TableColumn<>("年龄");
        tc_age.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<Data2, Number> param) {
                return param.getValue().getAgeProperty();
            }
        });

        TableColumn<Data2,Number> tc_score = new TableColumn<>("分数");
        tc_score.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, Number>, ObservableValue<Number>>() {
            @Override
            public ObservableValue<Number> call(TableColumn.CellDataFeatures<Data2, Number> param) {
                return param.getValue().getScoreProperty();
            }
        });
        TableColumn<Data2,Boolean> tc_is = new TableColumn<>("is");
        tc_is.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2, Boolean>, ObservableValue<Boolean>>() {
            @Override
            public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Data2, Boolean> param) {
                return param.getValue().getIsProperty();
            }
        });


        tableView.getColumns().addAll(tc_name,tc_age,tc_score,tc_is);

        Button button = new Button("button");
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setTopAnchor(tableView,100.0);
        anchorPane.setLeftAnchor(tableView,100.0);
        anchorPane.getChildren().addAll(button,tableView);
        Scene scene = new Scene(anchorPane);
        primaryStage.setScene(scene);
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);
        primaryStage.setTitle("网格布局");
        primaryStage.getIcons().add(new Image("http://www.haotuo.net.cn/Resources/cq/qunlogo.png"));
        primaryStage.show();


        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                //增加
                tableView.getItems().add(new Data2("小李",77,55,false));
                //删除
                tableView.getItems().remove(0);
                //改
                tableView.getItems().set(2,new Data2("小的",22,0,true));
                d1.setAge(1);

            }
        });

    }
}

类Data2

package fx.com;

import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class Data2 {
    private SimpleStringProperty name = new SimpleStringProperty();
    private SimpleIntegerProperty age = new SimpleIntegerProperty();
    private SimpleDoubleProperty score = new SimpleDoubleProperty();
    private SimpleBooleanProperty is = new SimpleBooleanProperty(    );

    public Data2(String name, int age, double score, boolean is) {
        this.name.set(name);
        this.age.set(age);
        this.score.set(score);
        this.is.set(is);
    }

    public Data2() {

    }

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

    public SimpleStringProperty nameProperty() {
        return name;
    }

    public void setName(String name) {
        this.name.set(name);
    }

    public int getAge() {
        return age.get();
    }

    public SimpleIntegerProperty ageProperty() {
        return age;
    }

    public void setAge(int age) {
        this.age.set(age);
    }

    public double getScore() {
        return score.get();
    }

    public SimpleDoubleProperty scoreProperty() {
        return score;
    }

    public void setScore(double score) {
        this.score.set(score);
    }

    public boolean isIs() {
        return is.get();
    }

    public SimpleBooleanProperty isProperty() {
        return is;
    }

    public void setIs(boolean is) {
        this.is.set(is);
    }
    public SimpleStringProperty getNameProperty(){
        return  this.name;
    }
    public SimpleIntegerProperty getAgeProperty(){
        return  this.age;
    }
    public SimpleDoubleProperty getScoreProperty(){
        return  this.score;
    }
    public SimpleBooleanProperty getIsProperty(){
        return  this.is;
    }
}

 

 

  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaFXTableView 控件是一个用于展示表格数据的高级控件。它提供了灵活的列和行布局,支持对表格数据进行排序、筛选、编辑和选择等操作。 TableView 的主要特点和功能包括: 1. 列(Columns):TableView 可以包含多个列,每个列由 TableColumn 对象表示。你可以添加、移除和重新排序列,设置列的宽度、标题、对齐方式等属性。 2. 行(Rows):TableView 的每一行表示表格中的一数据记录。可以通过添加TableView数据集合(ObservableList)来动态添加删除行。 3. 单元格(Cells):每个单元格用于显示一个数据项。你可以自定义单元格的样式,包括字体、颜色、背景等。同时,TableView 也支持对单元格进行编辑。 4. 数据源(Items):TableView 使用一个 ObservableList 作为数据源,它可以是任何实现了 javafx.collections.ObservableList 接口的类。 5. 排序和筛选:TableView 支持对表格数据进行排序和筛选。你可以通过设置 TableColumn 的 sortable 属性来启用或禁用某列的排序功能。 6. 编辑(Editing):TableView 可以设置为可编辑模式,允许用户直接在表格中修改数据。你可以通过设置 TableColumn 的 editable 属性来控制某列是否可编辑。 7. 选择(Selection):可以通过 TableView 的 SelectionModel 获取用户选择的行或单元格信息。支持单选和多选模式。 8. 自定义单元格显示:你可以通过 setCellFactory 方法来自定义单元格的显示方式,实现更复杂的表格布局和样式。 使用 TableView 控件,你可以将表格数据以更直观、灵活和可交互的方式展示给用户,满足各种数据展示和操作的需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值