Use of JavaFX's TableView

  • 2020-04-01 02:36:06
  • OfStack

TableView, which is a very important control, can be seen almost everywhere, and it has powerful functions and good data display effect. So, in JavaFX, it's natural to learn how to use TableView.

Now let's take a look at the rendering of the TableView:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/2013128163239957.png" >

Each column is a TableColumn, which we can create either directly or in JavaFX Scene Builder.

To populate the TableView, you need an ObservableList. You need a class to populate the data.

Let's take a look at our data-filled classes:


import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;

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);
       }
    
    public String getFileName() {
        return fileName.get();
    }
    
    public void setFileName(String fileName) {
        this.fileName.set(fileName);
    }

    public SimpleStringProperty fileNameProperty(){
        return fileName;
    }
    
    public String getStatus() {
        return status.get();
    }
    
    public void setStatus(String status) {
        this.status.set(status);
    }

   public SimpleStringProperty statusProperty(){
        return status;
    }
    
    public String getDlSpeed() {
        return dlSpeed.get();
    }
    
    public void setDlSpeed(String dlSpeed) {
        this.dlSpeed.set(dlSpeed);
    }
    public SimpleStringProperty dlSpeedProperty(){
        return dlSpeed;
    }

    
    public double getProgress() {
        return progress.get();
    }
    
    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;
    }  
}

Remember, the class used for data padding must use JavaFX's Property mechanism for data binding so that the TableView's data is refreshed in real time when we change ObservableList.


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

We get all the columns of the TableView through tableview. getColumns.

CellValueFactory refers to the data that is populated in each column of the TableView. Here we simply use PropertyValueFacotry. The following corresponds to the Property Property name in your DownloadData.

CellFactory we can specify the view type of a Cell in the TableView. You can see I used a ProgressBar.

CellFactory CellFactory additionally, take part in the deployment headaches, detailed as you can in the deployment headaches. Scene. The control. The cell found in the package.

We then create a DownloadData, set the data, and add it to ObservableList.

As shown in the following figure:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/2013128163431892.png" >

This is the data population for the TableView.

In addition, events in JavaFX do not perform a click on an item like onItemClick in Java or Android.

Many of the events for the controls in JavaFX are typically executed using the ChangeListener of Property.

As follows:


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:   //All the tasks
                        refreshTableData(0, 1, 2);
                        break;
                    case 2:   //Is downloading
                        refreshTableData(0);
                        break;
                    case 3:  //Has been completed
                        refreshTableData(2);
                        break;
                    case 4:  //bin
                        refreshTableData(-1);
                        break;
                }
            }
        });

So here's the TreeView event, which does that by listening for the selectItemProperty to change, and the TableView does the same thing by listening for the selectXXXProperty to click on the Item and so on.

It's time to call it a day, so I'll stop here for the moment.

Some of the pictures used in the article are tools for practicing with JavaFX when I have nothing to do recently.

However, due to the slow pace of the JavaFX update, you may end up continuing with other development and learning.


Related articles: