广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州如何改变JavaFX树表视图的视觉外观?_北大青鸟IT学校

作者:邓华发布时间:2021-05-20分类:Java技术浏览:839


导读:大家知道如何改变JavaFX树表视图的视觉外观吗?下面惠州北大青鸟老师给大家科普一下如何改变JavaFX树表视图的视觉外观。

大家知道如何改变JavaFX树表视图的视觉外观吗?下面惠州北大青鸟老师给大家科普一下如何改变JavaFX树表视图的视觉外观。

treeTableView.setTableMenuButtonVisible(true)启用表格菜单按钮,以便用户可以切换表列的可见性。 该方法将“+”按钮添加到表头。


/*

 * Copyright (c) 2008, 2014, Oracle and/or its affiliates.

 * All rights reserved. Use is subject to license terms.

 *

 * This file is available and licensed under the following license:

 *

 * Redistribution and use in source and binary forms, with or without

 * modification, are permitted provided that the following conditions

 * are met:

 *

 *  - Redistributions of source code must retain the above copyright

 *    notice, this list of conditions and the following disclaimer.

 *  - Redistributions in binary form must reproduce the above copyright

 *    notice, this list of conditions and the following disclaimer in

 *    the documentation and/or other materials provided with the distribution.

 *  - Neither the name of Oracle nor the names of its

 *    contributors may be used to endorse or promote products derived

 *    from this software without specific prior written permission.

 *

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT

 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT

 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,

 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT

 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,

 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY

 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT

 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 */

import java.util.Arrays;

import java.util.List;


import javafx.application.Application;

import javafx.beans.property.ReadOnlyStringWrapper;

import javafx.beans.property.SimpleStringProperty;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.control.TreeItem;

import javafx.scene.control.TreeTableColumn;

import javafx.scene.control.TreeTableView;

import javafx.stage.Stage;


public class Main extends Application {


  List<Employee> employees = Arrays.<Employee> asList(new Employee(

      "Ethan Williams", "ethan.williams@example.com"), new Employee(

      "Emma Jones", "emma.jones@example.com"), new Employee("Michael Brown",

      "michael.brown@example.com"), new Employee("Anna Black",

      "anna.black@example.com"), new Employee("Rodger York",

      "roger.york@example.com"), new Employee("Susan Collins",

      "susan.collins@example.com"));


  final TreeItem<Employee> root = new TreeItem<>(new Employee(

      "Sales Department", ""));


  public static void main(String[] args) {

    Application.launch(Main.class, args);

  }


  @Override

  public void start(Stage stage) {

    root.setExpanded(true);

    employees.stream().forEach((employee) -> {

      root.getChildren().add(new TreeItem<>(employee));

    });

    Scene scene = new Scene(new Group(), 400, 400);

    Group sceneRoot = (Group) scene.getRoot();


    TreeTableColumn<Employee, String> empColumn = new TreeTableColumn<>(

        "Employee");

    empColumn.setPrefWidth(150);

    empColumn

        .setCellValueFactory((

            TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(

            param.getValue().getValue().getName()));


    TreeTableColumn<Employee, String> emailColumn = new TreeTableColumn<>(

        "Email");

    emailColumn.setPrefWidth(190);

    emailColumn

        .setCellValueFactory((

            TreeTableColumn.CellDataFeatures<Employee, String> param) -> new ReadOnlyStringWrapper(

            param.getValue().getValue().getEmail()));


    TreeTableView<Employee> treeTableView = new TreeTableView<>(root);

    treeTableView.getColumns().setAll(empColumn, emailColumn);

    

    treeTableView.setTableMenuButtonVisible(true);

    

    

    sceneRoot.getChildren().add(treeTableView);

    stage.setScene(scene);

    stage.show();

  }


  public class Employee {


    private SimpleStringProperty name;

    private SimpleStringProperty email;


    public SimpleStringProperty nameProperty() {

      if (name == null) {

        name = new SimpleStringProperty(this, "name");

      }

      return name;

    }


    public SimpleStringProperty emailProperty() {

      if (email == null) {

        email = new SimpleStringProperty(this, "email");

      }

      return email;

    }


    private Employee(String name, String email) {

      this.name = new SimpleStringProperty(name);

      this.email = new SimpleStringProperty(email);

    }


    public String getName() {

      return name.get();

    }


    public void setName(String fName) {

      name.set(fName);

    }


    public String getEmail() {

      return email.get();

    }


    public void setEmail(String fName) {

      email.set(fName);

    }

  }

}

我们可以通过使用TreeTableView类的setShowRoot方法显示或隐藏根树项目。


treeTableView.setShowRoot(false);

上面的代码生成以下结果。

JavaFX树表视图3.png

想了解更多关于Java的知识,联系在线客服,或者来惠州北大青鸟新方舟校区了解一下。

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学 校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表