简体   繁体   中英

JavaFX how to change the size of the scene via button.setOnAction

I am a Java and JavaFx newbie here, and I would like to ask a question. To put it simple, I have this one stage that has a button and a scene with a specific size, I declared the size of the scene with such coding:

public class ClientBaseDialog
extends Stage
{

GridPane grid;
ScrollPane scroll;
Boolean counter = false;

public ClientBaseDialog( String Label1, String Label2 )
{

    setResizable( false );
    final Label label = new Label( Label1 );
    final Label label2 = new Label( Label2 );

    label2.setWrapText( true );


    BorderPane borderPane = new BorderPane();

    final VBox myView = new VBox();
    label.setWrapText( true );
    myView.getChildren().addAll( label2 );

    Button closeButton = new Button( "Close" );
    closeButton.setOnAction( new EventHandler<ActionEvent>()
    {
        @Override
        public void handle( ActionEvent t )
        {
            ClientBaseDialog.this.close();

        }
    } );

    Button detailButton = new Button( "Detail" );
    detailButton.setOnAction( new EventHandler<ActionEvent>()
    {
        @Override
        public void handle( ActionEvent t )
        {
            if ( counter == false )
            {
                grid.add( scroll, 1, 5 );
                counter = true;

                   Scene scene = new Scene( grid, 600, 500 );
                   setScene( scene );
            }

            else
            {

                grid.getChildren().remove( scroll );
                counter = false;
            }

        }
    } );

    label.setWrapText( true );
    label.setPrefSize( 500, 250 );
    label2.setWrapText( true );

    HBox hbox = new HBox();
    HBox hbox2 = new HBox();

    hbox.setAlignment( Pos.CENTER );
    hbox2.setAlignment( Pos.CENTER );

    hbox.getChildren().add( closeButton );
    hbox2.getChildren().add( detailButton );
    hbox.setSpacing( 10 );

    scroll = new ScrollPane();
    scroll.setContent( myView );
    scroll.setPrefHeight(300);
    scroll.setHbarPolicy( ScrollPane.ScrollBarPolicy.NEVER );
    borderPane.setBottom( scroll );
    scroll.setFitToWidth( true );

    grid = new GridPane();
    ColumnConstraints col1 = new ColumnConstraints();
    col1.setPercentWidth( 5 );
    ColumnConstraints col2 = new ColumnConstraints();
    col2.setPercentWidth( 90 );
    ColumnConstraints col3 = new ColumnConstraints();
    col3.setPercentWidth( 5 );
    grid.getColumnConstraints().addAll( col1, col2, col3 );

    RowConstraints row1 = new RowConstraints();
    row1.setPercentHeight( 10 );
    RowConstraints row2 = new RowConstraints();
    row2.setPercentHeight( 2);
    RowConstraints row3 = new RowConstraints();
    row3.setPercentHeight( 2 );
    grid.getRowConstraints().addAll( row1, row2, row3 );

    grid.setHgap( 0 );
    grid.setVgap( 10 );
    grid.setPadding( new Insets( 0, 10, 0, 10 ) );

    grid.add( label, 1, 0 );
    grid.add( hbox, 1, 3 );
    grid.add( hbox2, 1, 4 );
    grid.setAlignment( Pos.CENTER );
    Scene scene = new Scene( grid, 600, 250 );
    setScene( scene );




    }

}

so basically I am trying to increase the size of the current stage/scene with a click of a button, but somehow something went wrong and I got runtime error, it says "java.lang.IllegalArgumentException: Grid hgap=0.0, vgap=10.0, alignment=CENTERis already set as root of another scene", sorry that I did not share the full coding, it is very long to share it here, anyone mind to advice? sorry for my poor English

edit: to avoid any confusion, I'll share the full coding in the class, sorry for the inconvenience :(

The exception clearly says that the root node of one scene is being tried to set as root to another scene.

However the approach for changing the scene size is wrong.
So instead of creating new scene, change the size of stage.
In other words, instead of

Scene scene = new Scene(grid, 600, 500);
setScene(scene);

just do

setWidth(600);
setHeight(500);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM