当前位置:   article > 正文

JAVAFX中高级TableColumn控件使用教程(用到scenebuilder)_javafx表格

javafx表格

1.表格实现效果

如下图所示,该表格具备选择框,序号以及升序和降序功能。这些功能的实现将在下文一一讲解。

2.CheckBox类的实现

在表格显示CheckBox需要单独创建一个类如下图所示,该类可以直接拿来使用。
  1. package Model_Service;
  2. import javafx.beans.InvalidationListener;
  3. import javafx.beans.value.ChangeListener;
  4. import javafx.beans.value.ObservableValue;
  5. import javafx.scene.control.CheckBox;
  6. /**
  7. * Checkbox服务组件
  8. * @author wjz
  9. *
  10. */
  11. public class Checkbox {
  12. /**
  13. * Checkbox组件
  14. */
  15. CheckBox checkbox=new CheckBox();
  16. /**
  17. *
  18. * @return 返回 checkbox
  19. */
  20. public ObservableValue<CheckBox> getCheckBox()
  21. {
  22. return new ObservableValue<CheckBox>() {
  23. /**
  24. * 添加监听
  25. */
  26. @Override
  27. public void addListener(ChangeListener<? super CheckBox> listener) {
  28. }
  29. /**
  30. * 移除监听
  31. */
  32. @Override
  33. public void removeListener(ChangeListener<? super CheckBox> listener) {
  34. }
  35. /**
  36. * 获取CheckBox
  37. */
  38. @Override
  39. public CheckBox getValue() {
  40. return checkbox;
  41. }
  42. /**
  43. * 添加监听
  44. */
  45. @Override
  46. public void addListener(InvalidationListener listener) {
  47. }
  48. /**
  49. * 移除监听
  50. */
  51. @Override
  52. public void removeListener(InvalidationListener listener) {
  53. }
  54. };
  55. }
  56. /**
  57. * 判读是否已经选中
  58. * @return
  59. */
  60. public Boolean isSelected()
  61. {
  62. return checkbox.isSelected();
  63. }
  64. }
此外需要在实体类中声明Checkbox如下所示,声明加入transient表示Checkbox是这个类的属性一部分,但在gson序列化的过程中无需考虑。如果不添加在序列化的过程中会报错。
  1. package Model;
  2. import java.io.Serializable;
  3. import java.time.LocalDate;
  4. import Model_Service.Checkbox;
  5. public class ServantCheck extends Servant implements Serializable {
  6. public ServantCheck(String id, String account, String password, String name, String gender, LocalDate birthday,
  7. String phoneNumber, String duty) {
  8. super(id, account, password, name, gender, birthday, phoneNumber, duty);
  9. // TODO Auto-generated constructor stub
  10. }
  11. public transient Checkbox checkbox = new Checkbox();
  12. }

3.表格属性的声明

声明表格属性如下所示,泛型为对应的实体类
  1. @FXML
  2. private TableView<Servant> tableView;
声明表格中的一列属性如下所示,CheckBox列泛型为<实体类, CheckBox>,其余列包括序号列声明为<实体类, String>
  1. @FXML
  2. private TableColumn<Servant, CheckBox> tableColumn1;
  3. @FXML
  4. private TableColumn<Servant, String> tableColumn3;
  5. @FXML
  6. private TableColumn<Servant, String> tableColumn2;
  7. @FXML
  8. private TableColumn<Servant, String> tableColumn10;
  9. @FXML
  10. private TableColumn<Servant, String> tableColumn9;
  11. @FXML
  12. private TableColumn<Servant, LocalDate> tableColumn8;
  13. @FXML
  14. private TableColumn<Servant, String> tableColumn5;
  15. @FXML
  16. private TableColumn<Servant, String> tableColumn4;
  17. @FXML
  18. private TableColumn<Servant, String> tableColumn7;
  19. @FXML
  20. private TableColumn<Servant, String> tableColumn6;

4.显示序号方法

方法如下所示可以直接复用,只需更该泛型(<Servant>或者<Servant, String>)为自己命名的实体类即可
  1. public void showServantTable(ObservableList<Servant> tarData) {
  2. tableColumn2.setCellFactory((col) -> {
  3. TableCell<Servant, String> cell = new TableCell<Servant, String>() {
  4. @Override
  5. public void updateItem(String item, boolean empty) {
  6. super.updateItem(item, empty);
  7. this.setText(null);
  8. this.setGraphic(null);
  9. if (!empty) {
  10. int rowIndex = this.getIndex() + 1;
  11. this.setText(String.valueOf(rowIndex));
  12. }
  13. }
  14. };
  15. return cell;
  16. });
  17. }
  18. }

5.表格属性与实体类属性绑定

initialize方法如下所示
  1. public void initialize(URL arg0, ResourceBundle arg1) {
  2. //表格与实体类的属性进行绑定
  3. this.tableColumn1.setCellValueFactory(cellData -> cellData.getValue().checkbox.getCheckBox());
  4. this.tableColumn3.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId()));
  5. this.tableColumn4.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAccount()));
  6. this.tableColumn5.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPassword()));
  7. this.tableColumn6.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
  8. this.tableColumn7.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getGender()));
  9. this.tableColumn8.setCellValueFactory(cellData -> new SimpleObjectProperty(cellData.getValue().getBirthday()));
  10. this.tableColumn9
  11. .setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPhoneNumber()));
  12. this.tableColumn10.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getDuty()));
  13. try {
  14. servantlist = ServantFile.servantJson();
  15. final ObservableList<ServantCheck> tarData = FXCollections.observableArrayList(servantlist);
  16. this.showServantTable(tarData);
  17. tableView.setItems(tarData);
  18. } catch (Exception e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. }
  22. }
其中以下代码块负责表格属性与实体类属性的绑定
  1. //以下代码负责绑定CheckBox,其中getValue()获取Servant实体类,checkbox为实体类中的属性
  2. //getCheckBox()为Checkbox类中的方法
  3. this.tableColumn1.setCellValueFactory(cellData -> cellData.getValue().checkbox.getCheckBox());
  4. //以下代码负责绑定实体类中的属性, SimpleStringProperty表明绑定格式是String类型的
  5. //getValue()获取Servant实体类,getPassword()为Servant实体类中的方法负责获取password(密码)
  6. this.tableColumn5.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPassword()));
  7. //以下代码负责绑定实体类中的属性,SimpleObjectProperty表明绑定格式是Object类型的,兼容LocalDate
  8. //getValue()获取Servant实体类,getBirthday()为Servant实体类中的方法负责获取birthday(出生日期)
  9. this.tableColumn8.setCellValueFactory(cellData -> new SimpleObjectProperty(cellData.getValue().getBirthday()));
以下代码负责让表格显示数据
  1. //以下代码负责根据为文件实例化list<servant>对象
  2. servantlist = ServantFile.servantJson();
  3. //以下代码负责将list<servant>对象表格可以读取的数据类型
  4. final ObservableList<Servant> tarData = FXCollections.observableArrayList(servantlist);
  5. //以下代码负责显示序号
  6. this.showServantTable(tarData);
  7. //以下代码负责将list<servant>属性添加至表格中,即让表格显示文件中的属性
  8. tableView.setItems(tarData);

6.判断CheckBox被选中的方法

该方法负责判定是否将复选框选中,如果选中返回实体类对象,否则返回空
  1. public ServantCheck check() {
  2.         //获取表格数据,需要更改泛型(<Servant>)
  3. ObservableList<Servant> list = tableView.getItems();
  4.         //判断复选框是否选中,此部分需要根据具体实体类的名称进行修改
  5. for (Servant scr : list) {
  6. if (scr.checkbox.isSelected()) {
  7. return scr;
  8. }
  9. index++;
  10. }
  11. return null;
  12. }

7.Controller类完成代码

该类只负责显示表格使用具体结构,按钮方法省略以便减少代码行数
  1. package Controller;
  2. import java.io.IOException;
  3. import java.net.URL;
  4. import java.time.LocalDate;
  5. import java.util.List;
  6. import java.util.ResourceBundle;
  7. import Model.Servant;
  8. import Model.ServantCheck;
  9. import Model_File.ServantFile;
  10. import Model_Service.ServantService;
  11. import View.Main;
  12. import javafx.beans.property.SimpleObjectProperty;
  13. import javafx.beans.property.SimpleStringProperty;
  14. import javafx.beans.value.ChangeListener;
  15. import javafx.beans.value.ObservableValue;
  16. import javafx.collections.FXCollections;
  17. import javafx.collections.ObservableList;
  18. import javafx.event.ActionEvent;
  19. import javafx.event.EventHandler;
  20. import javafx.fxml.FXML;
  21. import javafx.fxml.FXMLLoader;
  22. import javafx.geometry.Insets;
  23. import javafx.geometry.Pos;
  24. import javafx.scene.Scene;
  25. import javafx.scene.control.Alert;
  26. import javafx.scene.control.Button;
  27. import javafx.scene.control.ButtonBar;
  28. import javafx.scene.control.ButtonType;
  29. import javafx.scene.control.ChoiceBox;
  30. import javafx.scene.control.DatePicker;
  31. import javafx.scene.control.RadioButton;
  32. import javafx.scene.control.TableCell;
  33. import javafx.scene.control.TableColumn;
  34. import javafx.scene.control.TableView;
  35. import javafx.scene.control.TextField;
  36. import javafx.scene.control.Toggle;
  37. import javafx.scene.control.ToggleGroup;
  38. import javafx.scene.control.Alert.AlertType;
  39. import javafx.scene.layout.GridPane;
  40. import javafx.scene.layout.Pane;
  41. import javafx.scene.text.Text;
  42. import javafx.stage.Stage;
  43. import javafx.application.Application;
  44. import javafx.fxml.Initializable;
  45. import javafx.scene.control.*;
  46. /**
  47. * 用户管理控制类
  48. * @author wjz
  49. *
  50. */
  51. public class AdminController extends Application implements Initializable {
  52. @FXML
  53. private Button button5;
  54. @FXML
  55. private TableColumn<Servant, CheckBox> tableColumn1;
  56. @FXML
  57. private Button button4;
  58. @FXML
  59. private TableColumn<Servant, String> tableColumn3;
  60. @FXML
  61. private Button button2;
  62. @FXML
  63. private TableColumn<Servant, String> tableColumn2;
  64. @FXML
  65. private Button button3;
  66. @FXML
  67. private TableView<Servant> tableView;
  68. @FXML
  69. private TableColumn<Servant, String> tableColumn10;
  70. @FXML
  71. private TextField textField1;
  72. @FXML
  73. private TableColumn<Servant, String> tableColumn9;
  74. @FXML
  75. private TableColumn<Servant, LocalDate> tableColumn8;
  76. @FXML
  77. private Button quit;
  78. @FXML
  79. private Button search1;
  80. @FXML
  81. private TableColumn<Servant, String> tableColumn5;
  82. @FXML
  83. private TableColumn<Servant, String> tableColumn4;
  84. @FXML
  85. private Button button1;
  86. @FXML
  87. private TableColumn<Servant, String> tableColumn7;
  88. @FXML
  89. private TableColumn<Servant, String> tableColumn6;
  90. private List<Servant> servantlist;
  91. private List<ServantCheck> listline;
  92. private TextField tx1, tx2, tx3, tx4, tx7;
  93. private Button newButton1, newButton2, newButton3;
  94. private RadioButton rb1;
  95. private RadioButton rb2;
  96. private DatePicker dp;
  97. private ChoiceBox<String> cB;
  98. private ToggleGroup tG;
  99. private Stage newStage;
  100. private String s = "男";
  101. private int index;
  102. private Stage nnewStage;
  103. private TextField ntx1;
  104. private TextField ntx3;
  105. private TextField ntx2;
  106. private TextField ntx4;
  107. private RadioButton nrb1;
  108. private RadioButton nrb2;
  109. private TextField ntx7;
  110. private DatePicker ndp;
  111. private ChoiceBox<String> ncB;
  112. private ToggleGroup ntG;
  113. private Button nnewButton1;
  114. private Button nnewButton2;
  115. private Button nnewButton3;
  116. private String ns;
  117. public ServantCheck check() {
  118. ObservableList<Servant> list = tableView.getItems();
  119. for (Servant scr : list) {
  120. if (scr.checkbox.isSelected()) {
  121. return scr;
  122. }
  123. index++;
  124. }
  125. return null;
  126. }
  127. /**
  128. * 提前画好函数表格
  129. */
  130. public void initialize(URL arg0, ResourceBundle arg1) {
  131. this.tableColumn1.setCellValueFactory(cellData -> cellData.getValue().checkbox.getCheckBox());
  132. this.tableColumn3.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getId()));
  133. this.tableColumn4.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getAccount()));
  134. this.tableColumn5.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPassword()));
  135. this.tableColumn6.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getName()));
  136. this.tableColumn7.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getGender()));
  137. this.tableColumn8.setCellValueFactory(cellData -> new SimpleObjectProperty(cellData.getValue().getBirthday()));
  138. this.tableColumn9
  139. .setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getPhoneNumber()));
  140. this.tableColumn10.setCellValueFactory(cellData -> new SimpleStringProperty(cellData.getValue().getDuty()));
  141. try {
  142. servantlist = ServantFile.servantJson();
  143. final ObservableList<Servant> tarData = FXCollections.observableArrayList(servantlist);
  144. this.showServantTable(tarData);
  145. tableView.setItems(tarData);
  146. } catch (Exception e) {
  147. // TODO Auto-generated catch block
  148. e.printStackTrace();
  149. }
  150. }
  151. /**
  152. * 显示序号
  153. * @param tarData
  154. */
  155. public void showServantTable(ObservableList<Servant> tarData) {
  156. tableColumn2.setCellFactory((col) -> {
  157. TableCell<Servant, String> cell = new TableCell<Servant, String>() {
  158. @Override
  159. public void updateItem(String item, boolean empty) {
  160. super.updateItem(item, empty);
  161. this.setText(null);
  162. this.setGraphic(null);
  163. if (!empty) {
  164. int rowIndex = this.getIndex() + 1;
  165. this.setText(String.valueOf(rowIndex));
  166. }
  167. }
  168. };
  169. return cell;
  170. });
  171. }
  172. }
  173. //这些按钮的具体方法为了节省代码行数故不一一显示
  174. public void start(Stage stage) throws IOException {
  175. }
  176. @FXML
  177. void quitAction(ActionEvent event) {
  178. }
  179. @FXML
  180. void buttonAction1(ActionEvent event) {
  181. }
  182. @FXML
  183. void buttonAction2(ActionEvent event) {
  184. }
  185. @FXML
  186. void buttonAction3(ActionEvent event) {
  187. }
  188. @FXML
  189. void buttonAction4(ActionEvent event) {
  190. }
  191. @FXML
  192. void searchAction1(ActionEvent event) {
  193. }
  194. @FXML
  195. void newAction(ActionEvent event) {
  196. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/191699
推荐阅读
相关标签
  

闽ICP备14008679号