gpt4 book ai didi

javafx-2 - TitledPane - 事件

转载 作者:行者123 更新时间:2023-12-04 18:11:42 26 4
gpt4 key购买 nike

我有两个不同的组件:TableView 和表格旁边的 TitledPane。我想做的是重新调整表格 View 的尺寸,但仅在标题 Pane 展开或折叠时进行。当 titledpane 折叠时,表格 View 会变大,而当它展开时,表格 View 会变小。我不知道我应该采取什么行动。有人知道解决办法吗?

问候

最佳答案

查看下面的示例代码:

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", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9d3d8dad6db97cad4d0cdd1f9dcc1d8d4c9d5dc97dad6d4" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Isabella", "Johnson", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cca5bfadaea9a0a0ade2a6a3a4a2bfa3a28ca9b4ada1bca0a9e2afa3a1" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Ethan", "Williams", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5237263a333c7c253b3e3e3b333f2112372a333f223e377c313d3f" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Emma", "Jones", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbbeb6b6baf5b1b4b5bea89bbea3bab6abb7bef5b8b4b6" rel="noreferrer noopener nofollow">[email protected]</a>"),
new Person("Michael", "Brown", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4f22262c272e2a23612d3d2038210f2a372e223f232a612c2022" rel="noreferrer noopener nofollow">[email protected]</a>"));

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。

关于javafx-2 - TitledPane - 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12510858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com