JavaFX表格数据显示简单Demo
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JavaFX表格数据显⽰简单Demo 最近业务需求,需要点击⼀个按钮然后显⽰数值,初涉JavaFX,查看了相关的⽂档,简单的做了⼀个Demo 1、⾸先⽤Java secene builder做出⼀个页⾯,页⾯如下:
设置好各个组件的fx:id。
如下所⽰:
1 @FXML
2private ResourceBundle resources;
3
4 @FXML
5private URL location;
6
7 @FXML
8private TableColumn<MaterialInfo, String> codeCol;
9
10 @FXML
11private TableColumn<MaterialInfo, String> createDateCol;
12
13 @FXML
14private TableView<MaterialInfo> dataTable; //tableView
15
16 @FXML
17private TableColumn<MaterialInfo, String> isActiveCol;
18
19 @FXML
20private TableColumn<MaterialInfo, String> nameCol;
21
22 @FXML
23private Label nowDate;
24
25 @FXML
26private TableColumn<MaterialInfo, String> seqnumCol;
27
28 @FXML
29private TableColumn<MaterialInfo, String> shortCodeCol;
30
31 @FXML
32private Button showData;
33
34 @FXML
35private Button udateDate;
36
37private List<MaterialInfo> materialList = new ArrayList<MaterialInfo>(); //放置数据的集合
38
39
40
41 ObservableList<MaterialInfo> list = FXCollections.observableArrayList(); //javaFX 的数据集合
初始化该Sence,其中定义的MterialInfo是放置每列的,具体如下:
1private SimpleStringProperty num;
2private SimpleStringProperty name;
3private SimpleStringProperty code;
4private SimpleStringProperty shortCode;
5
6private SimpleStringProperty isActive;
7private SimpleStringProperty createDate;
8
9public String getNum(){
10return num.get();
11 }
12
13public String getName(){
14return name.get();
15 }
16
17public String getCode(){
18return code.get();
19 }
20
21public String getShortCode(){
22return shortCode.get();
23 }
24
25public String getIsActive(){
26return isActive.get();
27 }
28
29public String getCreateDate(){
30return createDate.get();
31 }
32
33public ObservableValue<String> numProperty(){
34return num;
35 }
36public ObservableValue<String> nameProperty(){
37return name;
38 }
39public ObservableValue<String> codeProperty(){
40return code;
41 }
42public ObservableValue<String> shortCodeProperty(){
43return shortCode;
44 }
45public ObservableValue<String> isActiveProperty(){
46return isActive;
47 }
48public ObservableValue<String> createDateProperty(){
49return createDate;
50 }
51
52public void setNum(String num){
53this.num = new SimpleStringProperty(num);
54
55 }
56public void setName(String name){
= new SimpleStringProperty(name);
58
59 }
60public void setCode(String code){
61this.code = new SimpleStringProperty(code);
62
63 }
64public void setShortCode(String shortCode){
65this.shortCode = new SimpleStringProperty(shortCode);
66
67 }
68public void setIsActive(String isActive){
69this.isActive = new SimpleStringProperty(isActive);
70
71 }
72public void setCreateDate(String createDate){
73this.createDate = new SimpleStringProperty(createDate);
74
75 }
76
然后初始化Sence
1 @Override
2public void initialize(URL location, ResourceBundle resources) {
3 Date date = new Date();
4 String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
5 nowDate.setText(now);
6 dataTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
7 configTableView();
8 initButton();
9 }
1/**
2 * 配置表格,绑定表格的每列
3*/
4private void configTableView() {
5 seqnumCol.setCellValueFactory(new PropertyValueFactory<MaterialInfo,String>("num"));
6 nameCol.setCellValueFactory(new PropertyValueFactory<MaterialInfo,String>("name"));
7 codeCol.setCellValueFactory(new PropertyValueFactory<MaterialInfo,String>("code"));
8 shortCodeCol.setCellValueFactory(new PropertyValueFactory<MaterialInfo,String>("shortCode"));
9 isActiveCol.setCellValueFactory(new PropertyValueFactory<MaterialInfo,String>("isActive"));
10 createDateCol.setCellValueFactory(new PropertyValueFactory<MaterialInfo,String>("createDate"));
11 dataTable.setItems(list);
12 }
13
14/**
15 * 初始化按钮功能,绑定按钮的事件
16*/
17private void initButton() {
18
19 showData.setOnAction(new EventHandler<ActionEvent>() {
20
21 @Override
22public void handle(ActionEvent arg0) {
23 dataTable.getItems().clear();
24 showData();
25 }
26 });
27 udateDate.setOnAction(new EventHandler<ActionEvent>() {
28
29 @Override
30public void handle(ActionEvent arg0) {
31 updateData();//该⽅法还没有做,以后补上
32 }
33
34
35 });
36 }
37
38
39protected void updateData() {
40// TODO Auto-generated method stub
41
42 }
43
44/**
45 * 展⽰数据
46*/
47protected void showData() {
48if(generateDate()!=null){
49 materialList = generateDate();
50 }
51 System.out.println(materialList.size()+"----"+materialList.get(0).getName());
52 list.addAll(materialList);
53 dataTable.setItems(list);
54 }
55
56/**
57 * ⽣成数据
58 * @return
59*/
60private List<MaterialInfo> generateDate() {
61 List<MaterialInfo> miList = new ArrayList<MaterialInfo>();
62 String name = "material";
63 String code = "1101";
64 String shortCode = "A";
65 String isActive = "是";
66 Date date = new Date();
67 String nowDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:").format(date); 68for(int i=0;i<10;i++){
69 MaterialInfo mi = new MaterialInfo();
70 mi.setNum(i+1+"");
71 mi.setName(name+i);
72 mi.setCode(code+i);
73 mi.setIsActive(isActive);
74 mi.setShortCode(shortCode+i);
75 mi.setCreateDate(nowDate+i);
76 miList.add(mi);
77 }
78return miList;
79 }
⾄此结束,程序运⾏成功。
效果图如下所⽰:。