How do I add components to a layout after it has become part of a scene?

228 Views Asked by At

The root layout in this case which has content...

object SomeApp extends JFXApp {

  stage = new PrimaryStage {
    title = "SomeApp"
    width = 800
    height = 600

    val TheButton = new Button("Add a Button") {
      onAction = {
        e: ActionEvent => {
          root.dd(new Button("Some Button"))
        }
      }
    }

    val SomeLayout = new AnchorPane {
      styleClass += "someStyle"

      content = TheButton
    }

    scene = new Scene {
      stylesheets = List(getClass.getResource("Extend.css").toExternalForm)

      root = new AnchorPane {

        content = SomeLayout
      }
    }
  }
}

I am trying to get it to add a button to the root layout when TheButton is clicked...

1

There are 1 best solutions below

0
On

This works:

object SFXAddElemAfterStart extends JFXApp {

   stage = new PrimaryStage {
      title = "SFXAddElemAfterStart"
      width = 800
      height = 600


      scene = new Scene {

         val paneNeedingButton = new AnchorPane {
            content = layout
         }
         root = paneNeedingButton

         def layout: AnchorPane = new AnchorPane {
            styleClass += "someStyle"

            content = button
         }

         def button = new Button("Add a Button") {
            onAction = {
               e: ActionEvent => {
                  paneNeedingButton.children += new Button("Some Button")
               }
            }
         }
      }
   }
}

You just need to put the button in a reasonable place.

(BTW sorry for the long wait, scalafx is a low traffic tag)