TableView,算是一個很重要的控件,幾乎隨處可見,而且功能強大,數(shù)據(jù)展示效果良好。所以,在JavaFX中,我們自然而然也應(yīng)該學(xué)習(xí)一下TableView的使用。
下面我們先看看TableView的效果圖:
每一列都是一個TableColumn,我們可以直接創(chuàng)建也可以在JavaFX Scene Builder中創(chuàng)建好。
TableView的數(shù)據(jù)填充,需要一個ObservableList。其中需要一個類來做數(shù)據(jù)填充。
下面看看我們數(shù)據(jù)填充的類:
/**
*
* @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ìn)行數(shù)據(jù)綁定,這樣在我們改變ObservableList的時候,TableView的數(shù)據(jù)才會實時刷新。
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ù)。我們這里簡單的使用PropertyValueFacotry。后面的要對應(yīng)你DownloadData中的Property屬性名。
CellFactory我們可以指定TableView中某一個Cell的視圖類型。大家可以看到我用到了個ProgressBar。
另外CellFactory,JavaFX中自帶部分的CellFactory,詳細(xì)的大家可以在javafx.scene.control.cell包中找到。
接著我們通過創(chuàng)建DownloadData,設(shè)置數(shù)據(jù),并添加到ObservableList中即可。
如下圖所示:
上面是TableView的數(shù)據(jù)填充。
另外,JavaFX中的事件也不像Java或者Android里面用onItemClick之類的來執(zhí)行某一項的點擊。
JavaFX中的控件的很多事件有著鮮明的特色,就是使用Property的ChangeListener來執(zhí)行。
如下:
@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的點擊等事件。
要下班了,這一節(jié)就暫時到這里了。
文章中用到的一些圖片,是最近沒事做的時候用JavaFX練手的工具。
不過由于JavaFX更新進(jìn)度較慢,最后可能會繼續(xù)其他的開發(fā)和學(xué)習(xí)。
新聞熱點
疑難解答
圖片精選