JavaFX the TableView of the use of detailed _java

Source: Internet
Author: User
Tags uuid

TableView, is a very important control, almost everywhere, and powerful, data display good results. Therefore, in the JavaFX, we naturally should also learn about the use of TableView.

Now let's take a look at the TableView effect chart:

Each column is a tablecolumn and can be created either directly or in JavaFX Scene Builder.

TableView data is populated and requires a observablelist. A class is required to do data padding.

Here's a look at our data-populated classes:

Copy Code code as follows:

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

Remember, as a class for data padding, you must use the JavaFX property mechanism to bind data so that when we change the observablelist, the TableView data is refreshed in real time.

Copy Code code as follows:

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 use Tableview.getcolumns to get all the columns of the TableView.

Cellvaluefactory refers to the data that is filled in each column of TableView. We use the propertyvaluefacotry here simply. The name of the property attribute in your downloaddata.

Cellfactory We can specify the view type of a cell in TableView. You can see that I used a ProgressBar.

In addition, the Cellfactory,javafx of the cellfactory, in detail you can find in the Javafx.scene.control.cell package.

Then we create the Downloaddata, set the data, and add it to the observablelist.

As shown in the following illustration:

Above is the TableView data padding.

In addition, events in JavaFX do not perform a click on a particular item like Java or Android with Onitemclick.

Many of the events in the JavaFX control are characterized by the use of the property's ChangeListener to execute.

As follows:

Copy Code code 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 Tasks
Refreshtabledata (0, 1, 2);
Break
Case 2://Is downloading
Refreshtabledata (0);
Break
Case 3://Completed
Refreshtabledata (2);
Break
Case 4://Trash
Refreshtabledata (-1);
Break
}
}
});

Here is the event of the TreeView, by listening to selectitemproperty changes to do the corresponding operation, similarly, TableView is also the same by listening to Selectxxxproperty properties to manipulate the item's click and so on.

To get off work, this section is here for the time being.

Some of the pictures used in the article are the tools to use JavaFX practicing when doing nothing recently.

However, due to the slow progress of the JavaFX update, other development and learning may continue.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.