首页 > 编程语言 >JavaFX TableView 使用 显示数据 增加 添加 删除 修改 更新 数据

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

时间:2023-02-01 12:35:41浏览次数:45  
标签:return TableView javafx JavaFX new import 数据 public Data2


推荐用法

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;
}
}

 

 

标签:return,TableView,javafx,JavaFX,new,import,数据,public,Data2
From: https://blog.51cto.com/u_10780206/6031338

相关文章

  • JavaFX 网格布局 GridPane
    packagefx.com;importjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.image.Image;importjavafx.......
  • SpringBoot整合druid数据源
    SpringBoot默认使用HikariDataSource,但也可以整合外部DataSource。下面介绍SpringBoot_Druid整合整合有两种方式:1、starter整合 2、自定义整合1、starter整合2、自定......
  • Django实现从数据库查询数据展示在界面
    配置路径,编写前台界面应用之前建好的数据库表参考https://www.cnblogs.com/roselearn/p/17075119.html下图为数据库的数据编写视图from.models......
  • 开源大数据分析平台的内容有什么?
    在大数据时代,做好数据管理是非常重要的一个步骤。可以给企业做出正确的经营决策,指引新的发展方向。因此,随着数字化时代的到来,很多企业都倾向于寻找适宜的开源大数据分析平......
  • 开源数据湖仓:Delta vs. Iceberg vs. Hudi vs. Kudu vs. Hologres
    深度对比Delta、Iceberg和Hudi三大开源数据湖方案_开源_胡争(子毅)_InfoQ精选文章https://www.infoq.cn/article/fjebconxd2sz9wloykfo替换Kudu,Hologres助力好未来网校实......
  • 17-项目实战-上传excel并保存到数据库
    1.创建数据表classHome(models.Model):"""首页"""title=models.CharField(verbose_name="标题",max_length=32)def__str__(self):return......
  • redis基本数据类型 set类型
      127.0.0.1:6379>SADDs1abc(integer)3127.0.0.1:6379>SMEMBERSs11)"b"2)"c"3)"a"127.0.0.1:6379>SREMs1a(integer)1127.0.0.1:6379>SCARD......
  • 记一次数据库迁移
    数据库迁移前言数据库迁移有啥好记录的,一句话搞就完了.操蛋在于,项目没有实际开发测试完,但是需要先写发版步骤.场景:平常开发服务器A,要给客户定制化部署一份B......
  • RNA-seq测序方法及数据分析
    RNA高通量测序(RNA-sequencing,缩写为RNA-seq)是目前高通量测序技术中被用得最广的一种技术。RNA-seq可以帮助我们了解:各种比较条件下,所有基因的表达情况的差异。RNA-seq可......
  • SQL Server批量数据插入SQL语句
    1.INSERTINTOSELECT语句语句形式为:InsertintoTable2(field1,field2,…)selectvalue1,value2,…fromTable1要求目标表Table2必须存在,由于目标表Table2已经存在,所以......