Scene resizing on opening and closing TitledPane
I was wondering how I can resize Scene
as it expands and collapses TitledPane
.
I have tried using only 1 TitledPane
1 TitledPane
package fxinswing;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class FXInSwing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override public void start(Stage stage) {
TitledPane tp = new TitledPane();
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("First Name: "), 0, 0);
grid.add(new TextField(), 1, 0);
grid.add(new Label("Last Name: "), 0, 1);
grid.add(new TextField(), 1, 1);
grid.add(new Label("Email: "), 0, 2);
grid.add(new TextField(), 1, 2);
tp.setText("Grid 1");
tp.setContent(grid);
stage.setTitle("TitledPane");
Scene scene = new Scene(new Group());
scene.setFill(Color.GHOSTWHITE);
Group root = (Group)scene.getRoot();
root.getChildren().add(tp);
stage.setScene(scene);
stage.show();
}
}
What do I expect at closing time
And here is what I expect at the opening
However, currently, when it opens or closes, it always stays the same (except the direction of the arrow)
I was wondering how I can resize mine accordingly Scene
, so that it will always "fit" the actual size TitledPane
during open and close.
Update:
@James_D suggested a great way to overcome case 1 TitledPane
.
However, when using 2 TitledPane
s
2 TitledPanes
package fxinswing;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class FXInSwing extends Application {
public static void main(String[] args) {
launch(args);
}
@Override public void start(final Stage stage) {
stage.setTitle("TitledPane");
Scene scene = new Scene(new GridPane());
scene.setFill(Color.GHOSTWHITE);
final GridPane root = (GridPane )scene.getRoot();
TitledPane tp = new TitledPane();
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
grid.add(new Label("First Name: "), 0, 0);
grid.add(new TextField(), 1, 0);
grid.add(new Label("Last Name: "), 0, 1);
grid.add(new TextField(), 1, 1);
grid.add(new Label("Email: "), 0, 2);
grid.add(new TextField(), 1, 2);
tp.setText("Grid 1");
tp.setContent(grid);
tp.heightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue ov, Object t, Object t1) {
stage.sizeToScene();
}
});
TitledPane tp2 = new TitledPane();
GridPane grid2 = new GridPane();
grid2.setVgap(4);
grid2.setPadding(new Insets(5, 5, 5, 5));
grid2.add(new Label("First Name: "), 0, 0);
grid2.add(new TextField(), 1, 0);
grid2.add(new Label("Last Name: "), 0, 1);
grid2.add(new TextField(), 1, 1);
grid2.add(new Label("Email: "), 0, 2);
grid2.add(new TextField(), 1, 2);
tp2.setText("Grid 2");
tp2.setContent(grid2);
tp2.heightProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue ov, Object t, Object t1) {
stage.sizeToScene();
}
});
root.add(tp, 0, 0);
root.add(tp2, 0, 1);
stage.setScene(scene);
stage.show();
}
}
The result looks like this. Scene
will never resize, no matter what you open or close TitledPane
s.
I do not use Accordion
as I want the two to TitledPane
be independent of each other.
I tried
root.requestLayout();
But, nevertheless, nothing has changed. Any idea how I can make 2 TitledPane
work?
source to share