javaFX中对ListView Tree Table的操作
来源:互联网 发布:网络流行语言大全 编辑:程序博客网 时间:2024/06/11 23:18
今天研究FX中插入数据
先贴一段官方的ListView插入数据的方法
<p>/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package swinginterop;</p><p>import javafx.application.Application;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.control.cell.ComboBoxListCell;import javafx.scene.layout.StackPane;import javafx.stage.Stage;</p><p>public class ListViewSample extends Application {</p><p> public static final ObservableList data = FXCollections.observableArrayList();</p><p> public static void main(String[] args) { launch(args); }</p><p> @Override public void start(Stage primaryStage) { primaryStage.setTitle("List View Sample");</p><p> final ListView listView = new ListView(data); listView.setPrefSize(200, 250); listView.setEditable(true);</p><p> for (int i = 0; i < 18; i++) { data.add("anonym"); }</p><p> listView.setItems(data);</p><p> StackPane root = new StackPane(); root.getChildren().add(listView); primaryStage.setScene(new Scene(root, 200, 250)); primaryStage.show(); }}</p>
通过FXCollection创建了一个名字的Arraylist 然后将数据插入以后放入了List,可是研究过Table以后发现了FX 给我们提供了比较强大的数据对象支持。
稍微改一下添加一个数据对象,照抄Table的例子,拿出Person对象放入类里,然后再添加一个toString方法让他显示名字
如下代码:
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package swinginterop;import javafx.application.Application;import javafx.beans.property.BooleanProperty;import javafx.beans.property.SimpleBooleanProperty;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.collections.FXCollections;import javafx.collections.ObservableList;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.control.cell.ComboBoxListCell;import javafx.scene.layout.StackPane;import javafx.stage.Stage;public class ListViewSample extends Application { public static final ObservableList data = FXCollections.observableArrayList(); public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("List View Sample"); final ListView listView = new ListView(data); listView.setPrefSize(200, 250); listView.setEditable(true); for (int i = 0; i < 18; i++) { data.add(new Person("firstName" + i, "lastName" + i, "email" + i)); } listView.setItems(data); StackPane root = new StackPane(); root.getChildren().add(listView); primaryStage.setScene(new Scene(root, 200, 250)); primaryStage.show(); } public static class Person { private StringProperty firstName; private StringProperty lastName; private StringProperty email; private Person(String fName, String lName, String email) { this.firstName = new SimpleStringProperty(fName); this.lastName = new SimpleStringProperty(lName); this.email = new SimpleStringProperty(email); } public StringProperty firstNameProperty() { return firstName; } public StringProperty lastNameProperty() { return lastName; } public StringProperty emailProperty() { return email; } public void setLastName(String lastName) { this.lastName.set(lastName); } public void setFirstName(String firstName) { this.firstName.set(firstName); } public void setEmail(String email) { this.email.set(email); } @Override public String toString() { return firstName.getValue() + ":" + lastName.getValue(); } }}
这样的话拿到这些数据就不用像Swing一样对Table重写数据了,一套数据直接放入TableView, TreeView中也能展示出来,大大简化了编码的时间和代价
而且ObservableList 能添加ChangeListener方法,对数据的操作都能通过它来获取监听,通知其他组件,方便!
以后再学习修改Cell的方法,争取把FX给用起来
0 0
- javaFX中对ListView Tree Table的操作
- JavaFx中ListView的数据更新
- 对internal table的操作
- js对table的操作
- js对table的操作
- JavaFX之ListView的使用
- JavaFX之ListView的使用
- android dialog中嵌套ListView实现对ListView的添加、删除操作
- Javafx listView中item高度问题
- WEBFOCUS 对TABLE FILE的操作
- table对tr增加删除的操作
- 一个jquery对table的操作
- jquery对table表格的常用操作
- jquery对table中各种数据的增加、保存、删除操作
- Lua中对table的复制
- lua中对table的排序
- C#中对XML文件的一些基本操作(使用DOM Tree)
- JavaFX加入对HTML的支持:JWebPane
- 笔试题及面试题2
- ios 中 对 arcgis 的 AGSDynamicMapServiceLayer 下面的子图层 进行显示和不显示的 处理方式
- Python 第一章 基础知识(6) 函数
- 古堡算式
- The connection to adb is down, and a severe error has occured.
- javaFX中对ListView Tree Table的操作
- linux目录结构
- How to Restore the Database Using AMDU after Diskgroup Corruption (Doc ID 1597581.1)
- Hadoop初探系列(一)Windows下Hadoop环境搭建
- 6.母函数
- jsp内置对象以及作用域概述
- VC获取当前窗口的进程信息
- static关键字
- 正则表达式(二)