JavaFX It listview uses

Source: Internet
Author: User
Tags wordpress database

ListView It is through the same control non. In JavaFX in. ListView In addition, it has a very rich feature. Following. Let's take a look at how to use the ListView.

The ListView is located in the Javafx.scene.control package. The package is comprised of all UI controls in JavaFX.

Inheritance structures such as the following:

How to use the 1.ListView base

observablelist<string> strlist = fxcollections.observablearraylist ("Red", "yellow", "green"); listview<string> ListView = new listview<> (strlist); Listview.setitems (strlist); Listview.setprefsize ( Root.getchildren (). Add (ListView);
We first passfxcollectionsTo create the data that we need to populate the ListView.

The Setitems is able to populate the ListView with data. Set the current size of the ListView at the same time.

Display effects such as the following:

selection events for 2.ListView

Sometimes, when we click on the ListView item. We do the corresponding event handling.

Of course. The events of the ListView are implemented by the property binding mechanism, and see my other article for details: on JavaFX event mechanism

Here's the code:

Label label = new Label ("..."); Label.textproperty (). Bind (Listview.getselectionmodel (). Selecteditemproperty ()); Label.setlayouty (+); Root.getchildren (). Add (label);
This is simply a simple binding of the label's Text property to the ListViewSelect PropertiesAbove, when the selection of the ListView changes, the text of the label changes along with it.

:


The same. We were able to add a change event to the ListView's Select attribute , for example the following:

Label label = new Label ("..."), Label.setlayouty (), Root.getchildren (). Add (label), Listview.getselectionmodel (). Selecteditemproperty (). AddListener ((observablevalue<? extends string> observable, string oldValue, String NewValue)->{label.settext (newvalue);});
can achieve the same effect.

Lambda expressions are used here (in fact, new ChangeListener to listen for changes in properties). No practical jdk 8 friends to download it.

3.ListView of editing

As with TableView, the ListView provides this functionality as well as the ability to edit the item.

Just, we need to change the cellfactory of the ListView.

In the demo example above, we just need to include the following code:

Listview.setcellfactory (Textfieldlistcell.forlistview ()); Listview.seteditable (true);
We set the cellfactory of the ListView to Textfieldlistcell to provide editing functionality.

In JavaFX. Provides very many cells that can be used by default. You can see the document Javafx.scene.control.cell Package yourself. I'm not going to list them all.

For example, the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2luz2zvdxjldmvy/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

Double-click on an item to enter the edit state, press ENTER, edit is complete. At the same time, the selection property also changes.

Suppose you want to control the operation of the ListView item editor, there are also three events:

Setoneditstart Edit Start

setoneditcommit Edit complete

setoneditcancel Edit Cancel

These events will run when you make edits. There is a need to be able to handle these three events in a corresponding process.


4.ListView's own definition item

No matter what it says. The officially available ListView cell does not necessarily meet our needs. We usually define Listcell ourselves to achieve the desired effect.

In this way, we need to inherit listcell to implement our own cell.

The code looks like the following:

The ListView's own definition observablelist<string> strlist = fxcollections.observablearraylist ("Red", "Blue", "gold"); listview<string> ListView = new listview<> (strlist); Listview.setitems (strlist); Listview.setprefsize ( Listview.setcellfactory (listview<string> L), New Colorcell ()); Listview.seteditable (true); Root.getchildren (). Add (ListView); Label label = new Label ("..."); Label.textproperty (). Bind (Listview.getselectionmodel (). Selecteditemproperty ()); Label.setlayouty ($); Root.getchildren (). Add (label), class Colorcell extends Listcell<string> {@ overrideprotected void UpdateItem (String item, Boolean empty) {Super.updateitem (item, empty); Rectangle rect = new Rectangle (+);        if (item! = NULL) {           Rect.setfill (Color.web (item));           Setgraphic (rect);        } else {           setgraphic (null);}}}     

By inheriting Listcell, we implement the UpdateItem method to define the contents of each cell.

For example, the following:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2luz2zvdxjldmvy/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


The usage commentary for the ListView is here.

Basically can use to the content also is here. I'll see you in the next lesson.


This article is personal original, reproduced please indicate the source: Http://blog.csdn.net/wingfourever, my personal blog: http://www.wjfxgame.com

-----------------------------------------------------------------------------------------------------------

I haven't written a blog for some time, and I don't know why. Always a bit tired of the look. Despite the company's physical examination. There's nothing wrong with that.

Looks like. It can also be a common problem in the IT industry, working for three years. From the daily energetic, full of passion, to a little tired. Passion fades away. Some people say that, time is a xx knife.

Personal site before the use of WordPress, but it is too inappropriate to write code, often change the theme. Causes the code plug-in incompatible, displays the effect various pits the father. Again toss the plugin, but also toss this toss that. The expression is tired.

A Center the WordPress database back down. We'll clear the z-blog.

Although I am also the IT industry. But just want to write a tutorial, write some text, and do not want to continue to spend so much time on these meaningless things.

Recently with Unity3d made a text game template. The script is ready to finish the game.

Just didn't have time to write the script.

The following are:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2luz2zvdxjldmvy/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2luz2zvdxjldmvy/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Because it is just a trial script, the text message can be ignored.


In addition, the recent use of JavaFX to make a game map editor, ready to use in the Unity3d development of 2D RPG or strategy game.

If you are interested, you can also open source after the map editor is finished.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvd2luz2zvdxjldmvy/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">

A detailed work plan will be presented in the personal blog. Thanks for your support.

--------------------------------------------------------------------------------------------------------------- ------------------------------------------

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

JavaFX It listview uses

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.