当前位置:   article > 正文

javafx2.0 在表格(TableView)中显示选择按钮(CheckBox)

javafx tableview checkboxtablecell 可选择 显示文字

 

 

要在JFX的表格中显示自定义的单元(TableCell), 必须实现一个继承于javafx.scene.control.TableCell的自定义单元.  JFX官方没有为TableView提供像列表/选项按钮/文本框这样的常用控件, 如果你需要这些控件, 可以在另外一个项目中下载这些控件. 参见:http://www.javafxdata.org .(以下代码的实现参考了www.javafxdata.org)

 

首先定义一个TableCell来显示CheckBox.

 

  1. /* CheckBoxTableCell.java 1.0 2010-2-2
  2. *
  3. * Copyright (c) 2012 by Chen Zhiwu
  4. * All rights reserved.
  5. *
  6. * The copyright of this software is own by the authors.
  7. * You may not use, copy or modify this software, except
  8. * in accordance with the license agreement you entered into
  9. * with the copyright holders. For details see accompanying license
  10. * terms.
  11. */
  12. public class CheckBoxTableCell<S, T> extends TableCell<S, T> {
  13. private final CheckBox checkBox;
  14. private final boolean showText;
  15. private final Callback<T, String> toString;
  16. private final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty;
  17. private ObservableValue<Boolean> booleanProperty;
  18. public CheckBoxTableCell() {
  19. this(null, null);
  20. }
  21. public CheckBoxTableCell(
  22. Callback<Integer, ObservableValue<Boolean>> toString) {
  23. this(toString, null);
  24. }
  25. public CheckBoxTableCell(
  26. Callback<Integer, ObservableValue<Boolean>> getSelectedProperty,
  27. Callback<T, String> toString) {
  28. this.getSelectedProperty = getSelectedProperty;
  29. this.toString = toString;
  30. this.showText = toString != null;
  31. this.checkBox = new CheckBox();
  32. setAlignment(Pos.CENTER);
  33. setGraphic(checkBox);
  34. if (showText) {
  35. checkBox.setAlignment(Pos.CENTER_LEFT);
  36. }
  37. }
  38. public CheckBoxTableCell(
  39. Callback<T, ObservableValue<Boolean>> callback,
  40. Callback<Integer, ObservableValue<Boolean>> getSelectedProperty,
  41. Callback<T, String> toString) {
  42. this.getSelectedProperty = getSelectedProperty;
  43. this.toString = toString;
  44. this.showText = toString != null;
  45. this.checkBox = new CheckBox();
  46. setAlignment(Pos.CENTER);
  47. setGraphic(checkBox);
  48. if (showText) {
  49. checkBox.setAlignment(Pos.CENTER_LEFT);
  50. }
  51. }
  52. @Override
  53. protected void updateItem(T item, boolean empty) {
  54. super.updateItem(item, empty);
  55. if (empty) {
  56. setText(null);
  57. setGraphic(null);
  58. return;
  59. }
  60. if (this.showText) {
  61. setText(this.toString.call(item));
  62. }
  63. setGraphic(this.checkBox);
  64. if (this.booleanProperty instanceof BooleanProperty)
  65. this.checkBox.selectedProperty().unbindBidirectional(
  66. (BooleanProperty) this.booleanProperty);
  67. ObservableValue localObservableValue = getSelectedProperty();
  68. if (localObservableValue instanceof BooleanProperty) {
  69. this.booleanProperty = localObservableValue;
  70. this.checkBox.selectedProperty().bindBidirectional(
  71. (BooleanProperty) this.booleanProperty);
  72. }
  73. this.checkBox.visibleProperty().bind(getTableView().editableProperty()
  74. .and(getTableColumn().editableProperty())
  75. .and(editableProperty()));
  76. };
  77. private ObservableValue getSelectedProperty() {
  78. return ((this.getSelectedProperty != null) ? (ObservableValue) this.getSelectedProperty
  79. .call(Integer.valueOf(getIndex())) : getTableColumn()
  80. .getCellObservableValue(getIndex()));
  81. }
  82. }
 

 

CellFactory作为便利方法, 方便外部使用CheckBoxTableCell.

 

  1. /* CellFactory.java 1.0 2010-2-2
  2. *
  3. * Copyright (c) 2012 by Chen Zhiwu
  4. * All rights reserved.
  5. *
  6. * The copyright of this software is own by the authors.
  7. * You may not use, copy or modify this software, except
  8. * in accordance with the license agreement you entered into
  9. * with the copyright holders. For details see accompanying license
  10. * terms.
  11. */
  12. public class CellFactory {
  13. // table check box
  14. public static <S> Callback<TableColumn<S, Boolean>, TableCell<S, Boolean>> tableCheckBoxColumn() {
  15. return tableCheckBoxColumn(null, null);
  16. }
  17. public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> tableCheckBoxColumn(
  18. Callback<Integer, ObservableValue<Boolean>> paramCallback) {
  19. return tableCheckBoxColumn(paramCallback, null);
  20. }
  21. public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> tableCheckBoxColumn(
  22. Callback<Integer, ObservableValue<Boolean>> paramCallback,
  23. boolean paramBoolean) {
  24. Callback<T, String> callback = new Callback<T, String>() {
  25. @Override
  26. public String call(T t) {
  27. return ((t == null) ? "" : t.toString());
  28. }
  29. };
  30. return tableCheckBoxColumn(paramCallback, callback);
  31. }
  32. public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> tableCheckBoxColumn(
  33. final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty,
  34. final Callback<T, String> toString) {
  35. return new Callback<TableColumn<S, T>, TableCell<S, T>>() {
  36. @Override
  37. public TableCell<S, T> call(TableColumn<S, T> paramTableColumn) {
  38. return new CheckBoxTableCell<S,T>(getSelectedProperty,toString);
  39. }
  40. };
  41. }
  42. }
 

 

定义一个用于测试的实体类. 

 

  1. public class Goods{//商品类
  2. private boolean available;//是否有效 用原子类型表示
  3. private BooleanProperty hotSaleProperty;//是否热销 用可绑定属性表示
  4. public boolean getAvailable(){
  5. return avaliable;
  6. }
  7. public void setAvailable(boolean newValue){
  8. this.available=newValue;
  9. }
  10. public boolean getHotSale(){
  11. return hotSaleProperty.get();
  12. }
  13. public boolean setHotSale(boolean newValue){
  14. hotSaleProperty.set(newValue);
  15. }
  16. }
 

 

 

外部使用CheckBoxTableCell.

 

 

 

 

  1. TableColumn<Goods, Boolean> availableColumn=new TableColumn<Goods, Boolean>("是否有效");
  2. col.setCellValueFactory(new PropertyValueFactory<Goods,Boolean>("available");
  3. col.setCellFactory(CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
  4. @Override
  5. public ObservableValue<Boolean> call(Integer index) {
  6. final Goods g= table.getItems().get(index);
  7. ObservableValue<Boolean> retval = new SimpleBooleanProperty(g,"available",g.getAvailable());
  8. retval.addListener(new ChangeListener<Boolean>() {
  9. @Override
  10. public void changed(
  11. ObservableValue<? extends Boolean> observable,
  12. Boolean oldValue, Boolean newValue) {
  13. g.setAvailable(newValue);
  14. }
  15. });
  16. return retval;
  17. }
  18. }));
  19. TableColumn<Goods, Boolean> hotSaleColumn=new TableColumn<Goods, Boolean>("是否热销");
  20. col.setCellValueFactory(new PropertyValueFactory<Goods,Boolean>("hotSale");
  21. col.setCellFactory(CellFactory.tableCheckBoxColumn());
 

 

上面两个列使用不同的绑定方法. 第一列"是否有效"对应Goods中的boolean属性available. 该属性不支持JFX的动态绑定, 所以在定义列的时候建立一个Callback来动态更新属性.

第二个列"是否热销"对应Goods的boolean属性hotSaleProperty. 该属性支持JFX的动态绑定, 在定义列时无需做任何修改就可以实现实体与表格的绑定.

从上面的例子也可以看到, 在新版的JFX中, 如果实体类原生支持JFX的绑定类型在实现数据绑定会方便很多.

 

以上是JFX中自定义单选框的实现. 关于列表(ListView)和树(TreeView)的实现也类似.

 

参考阅读:

JFX的数据绑定:http://docs.oracle.com/javafx/2.0/binding/jfxpub-binding.htm

用于数据绑定的JFX框架: http://www.javafxdata.org

 

 

 

 

 

 

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/191642
推荐阅读
相关标签
  

闽ICP备14008679号