Javafx 2 标题窗格-事件
我有两个不同的组件:表视图和表旁边的标题窗格。 我试图做的是重新确定tableview的尺寸,但仅当标题窗格展开或折叠时。 当标题窗格折叠时,tableview变大,当它展开时,tableview变小。 我不知道我应该采取什么行动。 有人知道解决办法吗Javafx 2 标题窗格-事件,javafx-2,Javafx 2,我有两个不同的组件:表视图和表旁边的标题窗格。 我试图做的是重新确定tableview的尺寸,但仅当标题窗格展开或折叠时。 当标题窗格折叠时,tableview变大,当它展开时,tableview变小。 我不知道我应该采取什么行动。 有人知道解决办法吗 关于请查看下面的示例代码: import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.beans
关于请查看下面的示例代码:
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);
}
}
}
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TitledPane;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.HBox;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类MyDemo扩展了应用程序{
private TableView TableView=new TableView();
//假设这两个组件的首选高度值如下所示:
私人双人桌最小高度=30.0;
私人双桌最大高度=500.0;
私有双标题窗格高度;//将被确定
私有最终可观测列表数据=
FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com"),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com"),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com"));
公共静态void main(字符串[]args){
应用程序启动(args);
}
@凌驾
公众假期开始(阶段){
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(新属性ValueFactory(“电子邮件”);
tableview.setItems(数据);
tableview.getColumns().addAll(firstNameCol、lastNameCol、emailCol);
最终标题窗格标题窗格=新标题窗格(“标题窗格”,新文本(“内容”);
titledPane.setAnimated(false);//我们需要暂时禁用
//动画以正确计算标题窗格的高度。
//强制设置为表格视图的最小高度
tableview.setMaxHeight(表格最小高度);
tableview.setMinHeight(表格最小高度);
//这里有两种选择
int选项=2;
如果(选项==1){
//首先,只需将表视图高度强制为其首选的最大值
//当标题窗格的展开属性更改时:
titledPane.expandedProperty().addListener(新的ChangeListener()){
@凌驾
public void已更改(observeValue查看下面的示例代码:
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);
}
}
}
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.beans.value.ChangeListener;
导入javafx.beans.value.observeValue;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.scene.scene;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TitledPane;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.scene.layout.HBox;
导入javafx.scene.text.text;
导入javafx.stage.stage;
公共类MyDemo扩展了应用程序{
private TableView TableView=new TableView();
//假设这两个组件的首选高度值如下所示:
私人双人桌最小高度=30.0;
私人双桌最大高度=500.0;
私有双标题窗格高度;//将被确定
私有最终可观测列表数据=
FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com"),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com"),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com"),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com"),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com"));
公共静态void main(字符串[]args){
应用程序启动(args);
}
@凌驾
公众假期开始(阶段){
TableColumn firstNameCol=新的TableColumn(“名字”);
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(新属性ValueFactory(“firstName”));
TableColumn lastNameCol=新的TableColumn(“姓氏”);
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(新属性ValueFactory(“lastName”));
TableColumn emailCol=新的TableColumn(“电子邮件”);
设置最小宽度(200);
emailCol.setCellValueFactory(新属性ValueFactory(“电子邮件”);
tableview.setItems(数据);
tableview.getColumns().addAll(firstNameCol、lastNameCol、emailCol);
最终标题窗格标题窗格=新标题窗格(“标题窗格”,新文本(“内容”);
titledPane.setAnimated(false);//我们需要暂时禁用
//动画以正确计算标题窗格的高度。
//强制设置为表格视图的最小高度
tableview.setMaxHeight(表格最小高度);
tableview.setMinHeight(表格最小高度);
//这里有两种选择
int选项=2;
如果(选项==1){
//首先,只需将表视图高度强制为