最近业务需求,需要点击一个按钮然后显示数值,初涉JavaFX,查看了相关的文档,简单的做了一个Demo
1、首先用Java secene builder做出一个页面,页面如下:
设置好各个组件的fx:id。如下所示:
1 @FXML 2 private ResourceBundle resources; 3 4 @FXML 5 private URL location; 6 7 @FXML 8 private TableColumn<MaterialInfo, String> codeCol; 9 10 @FXML 11 private TableColumn<MaterialInfo, String> createDateCol; 12 13 @FXML 14 private TableView<MaterialInfo> dataTable; //tableView 15 16 @FXML 17 private TableColumn<MaterialInfo, String> isActiveCol; 18 19 @FXML 20 private TableColumn<MaterialInfo, String> nameCol; 21 22 @FXML 23 private Label nowDate; 24 25 @FXML 26 private TableColumn<MaterialInfo, String> seqnumCol; 27 28 @FXML 29 private TableColumn<MaterialInfo, String> shortCodeCol; 30 31 @FXML 32 private Button showData; 33 34 @FXML 35 private Button udateDate; 36 37 private List<MaterialInfo> materialList = new ArrayList<MaterialInfo>(); //放置数据的集合 38 39 40 41 ObservableList<MaterialInfo> list = FXCollections.observableArrayList(); //javaFX 的数据集合
初始化该Sence,其中定义的MterialInfo是放置每列的,具体如下:
1 private SimpleStringProperty num; 2 private SimpleStringProperty name; 3 private SimpleStringProperty code; 4 private SimpleStringProperty shortCode; 5 6 private SimpleStringProperty isActive; 7 private SimpleStringProperty createDate; 8 9 public String getNum(){ 10 return num.get(); 11 } 12 13 public String getName(){ 14 return name.get(); 15 } 16 17 public String getCode(){ 18 return code.get(); 19 } 20 21 public String getShortCode(){ 22 return shortCode.get(); 23 } 24 25 public String getIsActive(){ 26 return isActive.get(); 27 } 28 29 public String getCreateDate(){ 30 return createDate.get(); 31 } 32 33 public ObservableValue<String> numProperty(){ 34 return num; 35 } 36 public ObservableValue<String> nameProperty(){ 37 return name; 38 } 39 public ObservableValue<String> codeProperty(){ 40 return code; 41 } 42 public ObservableValue<String> shortCodeProperty(){ 43 return shortCode; 44 } 45 public ObservableValue<String> isActiveProperty(){ 46 return isActive; 47 } 48 public ObservableValue<String> createDateProperty(){ 49 return createDate; 50 } 51 52 public void setNum(String num){ 53 this.num = new SimpleStringProperty(num); 54 55 } 56 public void setName(String name){ 57 this.name = new SimpleStringProperty(name); 58 59 } 60 public void setCode(String code){ 61 this.code = new SimpleStringProperty(code); 62 63 } 64 public void setShortCode(String shortCode){ 65 this.shortCode = new SimpleStringProperty(shortCode); 66 67 } 68 public void setIsActive(String isActive){ 69 this.isActive = new SimpleStringProperty(isActive); 70 71 } 72 public void setCreateDate(String createDate){ 73 this.createDate = new SimpleStringProperty(createDate); 74 75 } 76
然后初始化Sence
1 @Override 2 public 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 */ 4 private 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 */ 17 private void initButton() { 18 19 showData.setOnAction(new EventHandler<ActionEvent>() { 20 21 @Override 22 public void handle(ActionEvent arg0) { 23 dataTable.getItems().clear(); 24 showData(); 25 } 26 }); 27 udateDate.setOnAction(new EventHandler<ActionEvent>() { 28 29 @Override 30 public void handle(ActionEvent arg0) { 31 updateData();//该方法还没有做,以后补上 32 } 33 34 35 }); 36 } 37 38 39 protected void updateData() { 40 // TODO Auto-generated method stub 41 42 } 43 44 /** 45 * 展示数据 46 */ 47 protected void showData() { 48 if(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 */ 60 private 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); 68 for(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 } 78 return miList; 79 }
至此结束,程序运行成功。效果图如下所示: