TitledPane-活动 [英] TitledPane - Event

查看:44
本文介绍了TitledPane-活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的组件:TableView和表旁边的TitledPane. 我想做的是重新定义表格视图,但仅当标题窗格展开或折叠时才可以. 当标题窗格折叠时,表视图变大,而在展开时,表视图变小. 我不知道该怎么办. 有人知道解决方案吗?

I have two distinct components: a TableView and a TitledPane next to the table. What I´m trying to do is to redimension the tableview but only when the titledpane expands or collapse. When the titledpane collapse the tableview gets bigger and when it expands the tableview gets smaller. I don´t know what action should I take. Anybody knows the solution?

致谢

推荐答案

查看下面的示例代码:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
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.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TitledPane;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class MyDemo extends Application {

    private TableView<Person> tableview = new TableView<Person>();

    // Suppose your preferred height values for those 2 component are as follows:
    private double TABLE_MIN_HEIGHT = 30.0;
    private double TABLE_MAX_HEIGHT = 500.0;
    private double TITLED_PANE_HEIGHT; // will be determined

    private final ObservableList<Person> data =
            FXCollections.observableArrayList(
            new Person("Jacob", "Smith", "jacob.smith@example.com"),
            new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
            new Person("Ethan", "Williams", "ethan.williams@example.com"),
            new Person("Emma", "Jones", "emma.jones@example.com"),
            new Person("Michael", "Brown", "michael.brown@example.com"));

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage stage) {
        TableColumn firstNameCol = new TableColumn("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));

        TableColumn lastNameCol = new TableColumn("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));

        TableColumn emailCol = new TableColumn("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(new PropertyValueFactory<Person, String>("email"));

        tableview.setItems(data);
        tableview.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        final TitledPane titledPane = new TitledPane("TitledPane", new Text("Content\n\n\n\n"));
        titledPane.setAnimated(false); // we need to temporarily disable
        // animation to get the titledpanes computed height correctly.

        // Force to min height of table view
        tableview.setMaxHeight(TABLE_MIN_HEIGHT);
        tableview.setMinHeight(TABLE_MIN_HEIGHT);

        // Here you have 2 options
        int option = 2;

        if (option == 1) {
            // 1st simply force the table view height to its preferred max value
            // when the titled pane's expanded property changed:
            titledPane.expandedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    tableview.setMaxHeight(newValue ? TABLE_MIN_HEIGHT : TABLE_MAX_HEIGHT);
                    tableview.setMinHeight(newValue ? TABLE_MIN_HEIGHT : TABLE_MAX_HEIGHT);
                }
            });
        } else if (option == 2) {
            // 2nd. Similar to first but with "animation". Here observe height changes of titled pane:
            titledPane.heightProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                    tableview.setMaxHeight(TABLE_MAX_HEIGHT - (TABLE_MAX_HEIGHT * (newValue.doubleValue() / TITLED_PANE_HEIGHT)));
                    tableview.setMinHeight(TABLE_MAX_HEIGHT - (TABLE_MAX_HEIGHT * (newValue.doubleValue() / TITLED_PANE_HEIGHT)));
                }
            });
        }

        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(tableview, titledPane);

        Scene scene = new Scene(hBox);
        stage.setTitle("Table View Sample");
        stage.setWidth(650);
        stage.setHeight(700);
        stage.setScene(scene);

        TITLED_PANE_HEIGHT = titledPane.getHeight();
        System.out.println("TITLED_PANE_HEIGHT = " + TITLED_PANE_HEIGHT);

        stage.show();

        // Determine the titledPane computed height value after stage has been shown.
        TITLED_PANE_HEIGHT = titledPane.getHeight();
        System.out.println("TITLED_PANE_HEIGHT = " + TITLED_PANE_HEIGHT);
        // .. then enable animation
        titledPane.setAnimated(true);
    }

    public static class Person {

        private final SimpleStringProperty firstName;
        private final SimpleStringProperty lastName;
        private final SimpleStringProperty 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 String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }
    }
}

这是演示,请根据您的需要进行改进.也许还有其他方法. HTH.

It is demo, improve it according to your needs. There maybe other approaches as well. HTH.

这篇关于TitledPane-活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆