javaFX中对ListView Tree Table的操作

 今天研究FX中插入数据

先贴一段官方的ListView插入数据的方法

/*  * 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.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("anonym");         }

        listView.setItems(data);

        StackPane root = new StackPane();         root.getChildren().add(listView);         primaryStage.setScene(new Scene(root, 200, 250));         primaryStage.show();     } }


通过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给用起来

你可能感兴趣的:(javafx)