JavaFX TableView and Java Beans Pattern Convention

Source: Internet
Author: User

Computer Science II Homework Teacher asked to complete a course Schedule exporter, in the background interface needs to implement three tables: Holiday Schedule, Lecture Schedule, Assignments Schedule.

A classmate is busy for a long time to create a tableview, but when the cell in the Edit table, the corresponding value is always the same. I watched for more than 10 minutes last night and I went through all the wrong things before, and still couldn't find the wrong. However, adhering to the spirit of death to the end, today saw Oracle's TableView tutorial and StackOverflow on the same kind of questions, finally have a solution to the idea. Really mountain poor water vista no way! If it is a normal program bug then it is not recorded in the meaning of the blog. Because this bug is not easy to find, because the cause of this bug is due to the mechanism of JAVAFX components.

In this blog post I mainly want to write two parts: the simple implementation of JavaFX's TableView and Java Beans Pattern convention.

JavaFX tableview-oracle JavaFX TableView Tutorial

Main actions for creating TableView:

1. Create a table

First build the basic skeleton of the table:

TableView table = new TableView ();

TableColumn Firstnamecol = new TableColumn ("First name"); "First Name" is the title of this tablecolumn.

TableColumn Lastnamecol = new TableColumn ("Last Name"); "Last Name" is the title of this tablecolumn.

Table.getcolumns (). AddAll (Firstnamecol, Lastnamecol); TableColumn "package" into table

Then set the data type of each column table:

The following two lines initialize the data type of each column, respectively. Propertyvaluefactory<s, t> ("Nameinmodel"), S represents the basic type in the data model (see below), T denotes the type displayed in the table, Nameinmodel represents the variable name of this data field in the data model. These are the rules of the Java Beans pattern, and if you do not follow this standard, tables cannot be changed automatically after the GUI interface is updated.

Firstnamecol.setcellvaluefactory (New propertyvaluefactory<string, string> ("FirstName")); //

Lastnamecol.setcellvaluefactory (New propertyvaluefactory<string, string> ("LastName"));

  

2. Defining the Data Model

Public Class person{

Final Stringproperty firstName = new Simplestringproperty (); Stringproperty is a virtual class, Simplestringproperty is a concrete class

Final Stringproperty lastName = new Simplestringproperty ();

Getters Setters

Attention! The following two functions are very important, because Java beans (described later) code specification, the Data Model class variable need to implement a function similar to Xproperty (), x is the variable name.

Public Stringproperty Firstnameproperty () {return firstName;}

Public Stringproperty Lastnameproperty () {return lastName;}

}

We will then create a observablelist to store the instance data for these person.

Observablelist data = ...//Add the instance of person in anyway.

3. Data Model and Table Association

Table.setitems (data);

Okay, now the data is tied to the table! Since our data is in observablelist, the table's view is automatically updated whenever there is a change in the table.

Not to be continued ....

Resources:

Http://stackoverflow.com/questions/20048072/cant-fill-table-with-items-with-javafx
Http://stackoverflow.com/questions/17035478/javafx-propertyvaluefactory-not-populating-tableview
http://blog.netopyr.com/2011/05/13/javafx-properties/
Https://github.com/marcojakob/code.makery.ch/blob/master/collections/blog/14-05-10-update-to-javafx-8-whats-new.md
Https://docs.oracle.com/javafx/2/ui_controls/table-view.htm

JavaFX TableView and Java Beans Pattern Convention

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.