1

I am attempting to display a list of errors in the expandableContent area of a DialogPane. To achieve this I am using an Accordian with Titled Panes. If the user wants to view an exception stacktrace they can expand the title pane to view the details.

The difficulty I am encountering is that the dialog doesn't resize when the accordian is expanded.

I have tried adding the following as per https://stackoverflow.com/a/31208445/4931921 without success:

    tp1.expandedProperty().addListener( (obs, oldValue, newValue) -> { 
        Platform.runLater( () -> {
            tp1.requestLayout();
            tp1.getScene().getWindow().sizeToScene();
        } );
    } );

Here is an example:

import java.io.PrintWriter;
import java.io.StringWriter;

import javafx.application.Application;
import javafx.scene.control.Accordion;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TitledPane;
import javafx.stage.Stage;

public class AccordianTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        Dialog dialog = new Dialog();
        DialogPane pane = new DialogPane();
        pane.setHeaderText( "Test" );
        pane.getButtonTypes().add( ButtonType.OK );
        dialog.setDialogPane( pane );

        Accordion accordian = new Accordion();
        TitledPane tp1 = new TitledPane();
        accordian.getPanes().add(tp1);
        tp1.setText("My TitledPane");
        TextArea ta = new TextArea();
        ta.setText( getStackTrace() );
        tp1.setContent( ta );

        tp1.expandedProperty().addListener( (obs, oldValue, newValue) -> { 
            Platform.runLater( () -> {
                tp1.requestLayout();
                tp1.getScene().getWindow().sizeToScene();
            } );
        } );

        pane.setExpandableContent(accordian);

        dialog.showAndWait();

    }

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

    private String getStackTrace(){
        StringWriter sw = new StringWriter();
        new RuntimeException().printStackTrace( new PrintWriter( sw ) );
        return sw.toString();
    }

}

The following is what I would like to achieve (I have to resize the dialog manually to get this):

enter image description here

Community
  • 1
  • 1
stacktrace
  • 547
  • 1
  • 6
  • 19

2 Answers2

3

Turning off animation on the TitledPane solves the issue. There appears to be a timing issue whereby the attempt to size the window happens before the animation to resize the TitlePane is complete.

tp1.setAnimated(false);
stacktrace
  • 547
  • 1
  • 6
  • 19
  • I also need a feature closely related to you. I want to resize the titlepane textarea up/down like IntelliJ output window. my [project iamge](https://pasteboard.co/GRct6RI.png) and [project github link](https://github.com/al2helal/TextPad/blob/master/src/main/java/com/alhelal/textpad/Options.java) – alhelal Oct 29 '17 at 17:01
0

You can resize your Dialog like:

pane.getScene().getWindow().setHeight(value);

When your accordion gets expanded, buffer the current height of your dialog, update the height to your needs, and when you close the accordion, set the size back to the buffered value

Another possibility could be to change the prefHeight of accordian:

 tp1.expandedProperty().addListener((ov, b, b1) -> {
            if (b1) {
                accordian.setPrefHeight(value);
            } else {
                accordian.setPrefHeight(0);
            }

            pane.getScene().getWindow().sizeToScene();
            pane.requestLayout();
        });
jns
  • 6,017
  • 2
  • 23
  • 28
  • Thanks for the ideas. Resizing the dialog works except that when setting the dialog back to it's original size the "More Details" and "OK" button disappear?? They show up again if I manually resize the dialog. I had already tried the second recommendation but anything related to automatic resizing just doesn't work as expected. – stacktrace Apr 13 '16 at 13:45