JavaFX之TableView的使用詳解
TableView,算是一個(gè)很重要的控件,幾乎隨處可見,而且功能強(qiáng)大,數(shù)據(jù)展示效果良好。所以,在JavaFX中,我們自然而然也應(yīng)該學(xué)習(xí)一下TableView的使用。
下面我們先看看TableView的效果圖:

每一列都是一個(gè)TableColumn,我們可以直接創(chuàng)建也可以在JavaFX Scene Builder中創(chuàng)建好。
TableView的數(shù)據(jù)填充,需要一個(gè)ObservableList。其中需要一個(gè)類來做數(shù)據(jù)填充。
下面看看我們數(shù)據(jù)填充的類:
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
/**
*
* @author wing
*/
public final class DownloadData {
private final SimpleStringProperty fileName = new SimpleStringProperty();
private final SimpleStringProperty status = new SimpleStringProperty();
private final SimpleStringProperty dlSpeed = new SimpleStringProperty();
private final SimpleDoubleProperty progress = new SimpleDoubleProperty();
private final SimpleStringProperty downloadSize = new SimpleStringProperty();
private final SimpleStringProperty dlPercent = new SimpleStringProperty();
private String uuid;
public DownloadData(String filename, double progress) {
setFileName(filename);
setProgress(progress);
}
public DownloadData(String status, String filename, String dlSpeed, double progress) {
setStatus(status);
setFileName(filename);
setDlSpeed(dlSpeed);
setProgress(progress);
}
/**
* @return the fileName
*/
public String getFileName() {
return fileName.get();
}
/**
* @param fileName the fileName to set
*/
public void setFileName(String fileName) {
this.fileName.set(fileName);
}
public SimpleStringProperty fileNameProperty(){
return fileName;
}
/**
* @return the status
*/
public String getStatus() {
return status.get();
}
/**
* @param status the statusto set
*/
public void setStatus(String status) {
this.status.set(status);
}
public SimpleStringProperty statusProperty(){
return status;
}
/**
* @return the String
*/
public String getDlSpeed() {
return dlSpeed.get();
}
/**
* @param dlSpeed the dlSpeed to set
*/
public void setDlSpeed(String dlSpeed) {
this.dlSpeed.set(dlSpeed);
}
public SimpleStringProperty dlSpeedProperty(){
return dlSpeed;
}
/**
* @return the progress
*/
public double getProgress() {
return progress.get();
}
/**
* @param progress the progress to set
*/
public void setProgress(double progress) {
this.progress.set(progress);
}
public SimpleDoubleProperty progressProperty(){
return progress;
}
public String getDownloadSize() {
return downloadSize.get();
}
public void setDownloadSize(String downloadSize) {
this.downloadSize.set(downloadSize);
}
public SimpleStringProperty downloadSizeProperty(){
return downloadSize;
}
public String getDlPercent() {
return dlPercent.get();
}
public void setDlPercent(String dlPercent) {
this.dlPercent.set(dlPercent);
}
public SimpleStringProperty dlPercentProperty(){
return dlPercent;
}
public String getUUID() {
return uuid;
}
public void setUUID(String uuid) {
this.uuid = uuid;
}
}
記住,用作數(shù)據(jù)填充的類,一定要用JavaFX的Property機(jī)制,可以進(jìn)行數(shù)據(jù)綁定,這樣在我們改變ObservableList的時(shí)候,TableView的數(shù)據(jù)才會(huì)實(shí)時(shí)刷新。
private final ObservableList<DownloadData> data
= FXCollections.observableArrayList();
ObservableList<TableColumn> observableList = mDownloadTable.getColumns();
observableList.get(0).setCellValueFactory(new PropertyValueFactory("status"));
observableList.get(1).setCellValueFactory(new PropertyValueFactory("fileName"));
observableList.get(2).setCellValueFactory(new PropertyValueFactory("dlSpeed"));
observableList.get(3).setCellValueFactory(new PropertyValueFactory("downloadSize"));
observableList.get(4).setCellValueFactory(new PropertyValueFactory("progress"));
observableList.get(4).setCellFactory(ProgressBarTableCell.forTableColumn());
observableList.get(5).setCellValueFactory(new PropertyValueFactory("dlPercent"));
mDownloadTable.setItems(data);
我們通過TableView.getColumns來獲取TableView的所有列。
CellValueFactory指的是TableView每一列里填充的數(shù)據(jù)。我們這里簡(jiǎn)單的使用PropertyValueFacotry。后面的要對(duì)應(yīng)你DownloadData中的Property屬性名。
CellFactory我們可以指定TableView中某一個(gè)Cell的視圖類型。大家可以看到我用到了個(gè)ProgressBar。
另外CellFactory,JavaFX中自帶部分的CellFactory,詳細(xì)的大家可以在javafx.scene.control.cell包中找到。
接著我們通過創(chuàng)建DownloadData,設(shè)置數(shù)據(jù),并添加到ObservableList中即可。
如下圖所示:

上面是TableView的數(shù)據(jù)填充。
另外,JavaFX中的事件也不像Java或者Android里面用onItemClick之類的來執(zhí)行某一項(xiàng)的點(diǎn)擊。
JavaFX中的控件的很多事件有著鮮明的特色,就是使用Property的ChangeListener來執(zhí)行。
如下:
mMenuTree.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
mMenuTree.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue ov, Object t, Object t1) {
int index = mMenuTree.getSelectionModel().getSelectedIndex();
switch (index) {
case 1: //所有任務(wù)
refreshTableData(0, 1, 2);
break;
case 2: //正在下載
refreshTableData(0);
break;
case 3: //已完成
refreshTableData(2);
break;
case 4: //垃圾箱
refreshTableData(-1);
break;
}
}
});
這里是TreeView的事件,通過監(jiān)聽selectItemProperty的改變來做相應(yīng)的操作,同理,TableView也是一樣的通過監(jiān)聽selectXXXProperty屬性來操作Item的點(diǎn)擊等事件。
要下班了,這一節(jié)就暫時(shí)到這里了。
文章中用到的一些圖片,是最近沒事做的時(shí)候用JavaFX練手的工具。
不過由于JavaFX更新進(jìn)度較慢,最后可能會(huì)繼續(xù)其他的開發(fā)和學(xué)習(xí)。
相關(guān)文章
Java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,落子動(dòng)作的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
idea啟動(dòng)多個(gè)SpringBoot服務(wù)實(shí)例的最優(yōu)解決方法
啟動(dòng)SpringBoot項(xiàng)目其實(shí)就是啟動(dòng)Tomcat等服務(wù)容器,只要這個(gè)端口不同就能啟動(dòng)多個(gè)服務(wù)實(shí)例了,本文主要介紹了idea啟動(dòng)多個(gè)SpringBoot服務(wù)實(shí)例的最優(yōu)解決方法,感興趣的可以了解一下2024-05-05
Java concurrency線程池之線程池原理(四)_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java concurrency線程池之線程池原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
JDK9為何要將String的底層實(shí)現(xiàn)由char[]改成了byte[]
String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲(chǔ)字符串內(nèi)容,為什么要這樣做呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下2022-03-03
Exception in thread main java.lang.NoClassDefFoundError錯(cuò)誤解決方
這篇文章主要介紹了Exception in thread main java.lang.NoClassDefFoundError錯(cuò)誤解決方法,需要的朋友可以參考下2016-08-08

