javafx2.0 在表格(TableView)中顯示選擇按鈕(CheckBox)
要在JFX的表格中顯示自定義的單元(TableCell), 必須實現一個繼承於javafx.scene.control.TableCell的自定義單元. JFX官方沒有為TableView提供像列表/選項按鈕/文字框這樣的常用控制元件, 如果你需要這些控制元件, 可以在另外一個專案中下載這些控制元件. 參見:http://www.javafxdata.org .(以下程式碼的實現參考了www.javafxdata.org)
首先定義一個TableCell來顯示CheckBox.
/* CheckBoxTableCell.java 1.0 2010-2-2 * * Copyright (c) 2012 by Chen Zhiwu * All rights reserved. * * The copyright of this software is own by the authors. * You may not use, copy or modify this software, except * in accordance with the license agreement you entered into * with the copyright holders. For details see accompanying license * terms. */ public class CheckBoxTableCell<S, T> extends TableCell<S, T> { private final CheckBox checkBox; private final boolean showText; private final Callback<T, String> toString; private final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty; private ObservableValue<Boolean> booleanProperty; public CheckBoxTableCell() { this(null, null); } public CheckBoxTableCell( Callback<Integer, ObservableValue<Boolean>> toString) { this(toString, null); } public CheckBoxTableCell( Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, Callback<T, String> toString) { this.getSelectedProperty = getSelectedProperty; this.toString = toString; this.showText = toString != null; this.checkBox = new CheckBox(); setAlignment(Pos.CENTER); setGraphic(checkBox); if (showText) { checkBox.setAlignment(Pos.CENTER_LEFT); } } public CheckBoxTableCell( Callback<T, ObservableValue<Boolean>> callback, Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, Callback<T, String> toString) { this.getSelectedProperty = getSelectedProperty; this.toString = toString; this.showText = toString != null; this.checkBox = new CheckBox(); setAlignment(Pos.CENTER); setGraphic(checkBox); if (showText) { checkBox.setAlignment(Pos.CENTER_LEFT); } } @Override protected void updateItem(T item, boolean empty) { super.updateItem(item, empty); if (empty) { setText(null); setGraphic(null); return; } if (this.showText) { setText(this.toString.call(item)); } setGraphic(this.checkBox); if (this.booleanProperty instanceof BooleanProperty) this.checkBox.selectedProperty().unbindBidirectional( (BooleanProperty) this.booleanProperty); ObservableValue localObservableValue = getSelectedProperty(); if (localObservableValue instanceof BooleanProperty) { this.booleanProperty = localObservableValue; this.checkBox.selectedProperty().bindBidirectional( (BooleanProperty) this.booleanProperty); } this.checkBox.visibleProperty().bind(getTableView().editableProperty() .and(getTableColumn().editableProperty()) .and(editableProperty())); }; private ObservableValue getSelectedProperty() { return ((this.getSelectedProperty != null) ? (ObservableValue) this.getSelectedProperty .call(Integer.valueOf(getIndex())) : getTableColumn() .getCellObservableValue(getIndex())); } }
CellFactory作為便利方法, 方便外部使用CheckBoxTableCell.
/* CellFactory.java 1.0 2010-2-2 * * Copyright (c) 2012 by Chen Zhiwu * All rights reserved. * * The copyright of this software is own by the authors. * You may not use, copy or modify this software, except * in accordance with the license agreement you entered into * with the copyright holders. For details see accompanying license * terms. */ public class CellFactory { ////// table check box public static <S> Callback<TableColumn<S, Boolean>, TableCell<S, Boolean>> tableCheckBoxColumn() { return tableCheckBoxColumn(null, null); } public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> tableCheckBoxColumn( Callback<Integer, ObservableValue<Boolean>> paramCallback) { return tableCheckBoxColumn(paramCallback, null); } public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> tableCheckBoxColumn( Callback<Integer, ObservableValue<Boolean>> paramCallback, boolean paramBoolean) { Callback<T, String> callback = new Callback<T, String>() { @Override public String call(T t) { return ((t == null) ? "" : t.toString()); } }; return tableCheckBoxColumn(paramCallback, callback); } public static <S, T> Callback<TableColumn<S, T>, TableCell<S, T>> tableCheckBoxColumn( final Callback<Integer, ObservableValue<Boolean>> getSelectedProperty, final Callback<T, String> toString) { return new Callback<TableColumn<S, T>, TableCell<S, T>>() { @Override public TableCell<S, T> call(TableColumn<S, T> paramTableColumn) { return new CheckBoxTableCell<S,T>(getSelectedProperty,toString); } }; } }
定義一個用於測試的實體類.
public class Goods{//商品類 private boolean available;//是否有效 用原子型別表示 private BooleanProperty hotSaleProperty;//是否熱銷 用可繫結屬性表示 public boolean getAvailable(){ return avaliable; } public void setAvailable(boolean newValue){ this.available=newValue; } public boolean getHotSale(){ return hotSaleProperty.get(); } public boolean setHotSale(boolean newValue){ hotSaleProperty.set(newValue); } }
外部使用CheckBoxTableCell.
TableColumn<Goods, Boolean> availableColumn=new TableColumn<Goods, Boolean>("是否有效");
col.setCellValueFactory(new PropertyValueFactory<Goods,Boolean>("available");
col.setCellFactory(CellFactory.tableCheckBoxColumn(new Callback<Integer, ObservableValue<Boolean>>() {
@Override
public ObservableValue<Boolean> call(Integer index) {
final Goods g= table.getItems().get(index);
ObservableValue<Boolean> retval = new SimpleBooleanProperty(g,"available",g.getAvailable());
retval.addListener(new ChangeListener<Boolean>() {
@Override
public void changed(
ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue) {
g.setAvailable(newValue);
}
});
return retval;
}
}));
TableColumn<Goods, Boolean> hotSaleColumn=new TableColumn<Goods, Boolean>("是否熱銷");
col.setCellValueFactory(new PropertyValueFactory<Goods,Boolean>("hotSale");
col.setCellFactory(CellFactory.tableCheckBoxColumn());
上面兩個列使用不同的繫結方法. 第一列"是否有效"對應Goods中的boolean屬性available. 該屬性不支援JFX的動態繫結, 所以在定義列的時候建立一個Callback來動態更新屬性.
第二個列"是否熱銷"對應Goods的boolean屬性hotSaleProperty. 該屬性支援JFX的動態繫結, 在定義列時無需做任何修改就可以實現實體與表格的繫結.
從上面的例子也可以看到, 在新版的JFX中, 如果實體類原生支援JFX的繫結型別在實現資料繫結會方便很多.
以上是JFX中自定義單選框的實現. 關於列表(ListView)和樹(TreeView)的實現也類似.
參考閱讀:
相關推薦
javafx2.0 在表格(TableView)中顯示選擇按鈕(CheckBox)
要在JFX的表格中顯示自定義的單元(TableCell), 必須實現一個繼承於javafx.scene.control.TableCell的自定義單元. JFX官方沒有為TableView提供像列表/選項按鈕/文字框這樣的常用控制元件, 如果你需要這些控制元件, 可以在
ListView中顯示和隱藏CheckBox
<pre name="code" class="java">bindView(){ if(checkboxVisible){ views.c
Ext可編輯表格中timefield選擇後會顯示中國標準時間
選擇的時候正常,但是選擇完之後,點選其他地方,就變成Tue Jan 01 2008 04:45:00 GMT+0800 (中國標準時間) 了。 嘗試使用datefield也是一個效果。 使用的是extjs4.1.1只有在grid中editor裡,才會出現
Extjs4.2 GridPanel中顯示單選按鈕
spa check mod extend after get radi tco blog 效果:如上圖。 代碼:其中需要顯示單選按鈕的列 { dataIndex: ‘FeeModel‘, t
後端list集合中的數據傳遞到前臺HTML中顯示(表格形式)
UNC 姓名 char 響應 防止 數據 前端 格式 ble 關鍵字:web項目中前後臺數據傳遞問題 在學習web項目的過程中,我們肯定會遇到前後臺數據交換問題。這個問題我也思考了很久,今天借此總結一下。由於博主水平有限,如有不當之處,還請大家多多指正,,廢話
ArcGIS操作小技巧(一)之屬性表中顯示出小數點前面的 0
當你開啟ArcGIS屬性表,對向量進行面積或其他計算時,發現如下圖所示問題,兄弟莫方...........,接下來用兩種方法保證你滿意。 主要方法: 一、設定系統時間。 二、欄位屬性設定。 具體操作: 一、(1) 點選系統時間設定(環境
程式設計之旅-Ext4.X匯入excel表格在grid中顯示
1:關於匯入excel就不說了 網上很多教程,關鍵是在ext grid中顯示問題,其實就是對Ext的瞭解和api熟悉程度不高。 目前在ext中匯入excel嘗試通過了兩種方法。 需求:匯入excel 在Ext grid中顯示 然後點選按鈕確定是否儲存(後臺做資料驗證,固定列名) 分析需求
jqGrid-表格中顯示圖片
colNames : [ 'alarmId', '事件分類', '事件子類', '事件級別','發生時間', '報警描述' ], colModel : [ {name : 'alarmId', index : 'alarmId',hidden : true },
微信小遊戲選擇按鈕選項錯誤顯示錯誤同時顯示正確
效果如下: wxml: <view class='difficult2 '> <block wx:for="{{dif2_select}}" wx:key="{{index}}"> <view class="select
點選事件,選擇按鈕,點選從前端傳到後臺,查詢資料,並返回前段,顯示出來。---tp5
//這是HTML頁面 <div class="form-group"> <label class="col-sm-3">聽讀寫說分類</label>
Django釋出文章時選擇標籤以及在文章中顯示標籤
一 釋出文章時候選擇標籤 1 後端:修改mysite/article/views.py @login_required(login_url='/account/login') @csrf_exempt def article_post(request): if
ios開發中WKWebView在iOS11.0上部分連結顯示不出來
// ************ 解決不能載入微信公眾號文章在iOS11.0裝置上的問題************ //configuretion.preferences.minimumFontSize =
PyQT中讓QMessageBox按鈕顯示中文
QMessageBox.warning(self,'錯誤', '使用者名稱和密碼不匹配', QMessageBox.Yes, QMessageBox.Yes) 以上一條語句輸出,但更改前這樣顯示的,
PopupWindow在Android7.0系統中顯示位置錯誤
如上圖所示,我是希望使用者點選左上角的 “全部行業” 按鈕,在標題欄的底部彈出popWindow的 cb_selectShopCondition.setOnCheckedChangeListener(new CompoundButton.O
VC++6.0中顯示開啟檔案路徑對話方塊
上傳檔案、儲存檔案、新增檔案之類的操作經常,就需要找到檔案的全路徑。程式中就要彈出選擇路徑的“開啟檔案”對話方塊。下面就用VC++6.0 MFC來實現。 在對話方塊程式中,新增編輯框 IDC_EDIT 和按鈕 IDC_Open 程式主要程式碼如下: void CMyDl
HTML的input標籤為file型別時按鈕中顯示上傳檔案的名字
主要html程式碼: <a href="javascript:;" class="a-upload"> <div id="shangchuan" onchange="successload()"> </div> //使用onchan
學習筆記:WinEdt 7.0(Latex)在pdf中顯示中文
在.tex檔案中的\documentclass[12pt]{article}這行程式碼下 呼叫\usepackage{ctex} 這個包,這個包是內建的,不需要下載其他字型包。 \documentclass[12pt]{article} \usepackage{ctex}
在tableView中新增webView顯示商品圖文詳情 自適應高度
最近做了一個購物的應用 涉及到顯示商品的圖文詳情 需要載入html文字 用webView + tableView的形式展示 首先新增webView屬性 初始化 webView loa
如何在FineUIMvc(ASP.NET MVC)中顯示覆雜的表格列資料(列表和物件)?
起源 最初,這個問題是知識星球內的一個網友提出的,如何在FineUIMvc中展現複雜的列資料? 在FineUIPro中,我們都知道有一個 TemplateField 模板列可以使用,我們只需要在後臺定義一個 C# 方法,就可以返回任意想要的資料。 可是在FineUIMvc中沒有這麼個列型別,那又
LaTeX中表格多行顯示的最簡單設定方法
這其實是一個很簡單的問題,但是這兩天發現我之前的解決方案太麻煩了。簡單介紹一下這種最簡單的方法: 之前設定多行顯示的時候,用類似於下面這種方法進行多行顯示: \begin{table} \newcommand{\tabincell}[2]{\begin{tabular}