JavaFX之TableView的使用(一)

2014-11-24 08:09:47 · 作者: · 浏览: 0

声明: 本博客文章原创类别的均为个人原创,版权所有。转载请注明出处: http://blog.csdn.net/ml3947,另外本人的个人博客:http://www.wjfxgame.com。

TableView,算是一个很重要的控件,几乎随处可见,而且功能强大,数据展示效果良好。所以,在JavaFX中,我们自然而然也应该学习一下TableView的使用。

下面我们先看看TableView的效果图:

\

每一列都是一个TableColumn,我们可以直接创建也可以在JavaFX Scene Builder中创建好。

TableView的数据填充,需要一个ObservableList。其中需要一个类来做数据填充。

下面看看我们数据填充的类:

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;
    }  
}

记住,用作数据填充的类,一定要用JavaFX的Property机制,可以进行数据绑定,这样在我们改变ObservableList的时候,TableView的数据才会实时刷新。

private final ObservableList
  
    data
            = FXCollections.observableArrayList();


ObservableList
   
     observableList = mDownloadTable.getColumns(); observableList.get(0).setCellValueFactory(new PropertyValueFactory(status)); observableList.get(1).setCellVa