I tried to manually specify the location of the item (button) in the window in various ways (I try to put several buttons one after another). I consistently create three buttons, but only the last one is displayed on the screen and only the press is working. I found the setLayoutX method which seems to allow you to set the location along the X axis, but this does not help, the element is in the upper left corner (apparently all three are there, under each other).
If instead of GridPane use StackPane , then the element is in the center, also only one. In general, I can not understand what's the matter)

 import javafx.application.Application import javafx.event.ActionEvent import javafx.event.EventHandler import javafx.scene.Scene import javafx.scene.control.Button import javafx.scene.layout.StackPane import javafx.stage.Stage import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.text; import javafx.scene.control; import javafx.scene.paint; import javafx.geometry; object Main { def main(args:Array[String]) { val witchers: List[String] = new List(); witchers.AddBack("The Witcher") witchers.AddBack("The Witcher 2") witchers.AddBack("The Witcher 3") Application.launch(classOf[Main], args: _*) } } class Main extends Application { override def start(primaryStage: Stage) { primaryStage.setTitle("For Romanov") var buttons = new List[Button] val pane = new GridPane//StackPane val witchers: List[String] = new List(); witchers.AddBack("The Witcher") witchers.AddBack("The Witcher 2") witchers.AddBack("The Witcher 3") var layoutX = 5.toDouble; for (witcher <- witchers) { var btn = new Button btn.setText(witcher) btn.setLayoutX(layoutX) btn.setOnAction(new EventHandler[ActionEvent] { override def handle(e: ActionEvent) { println(witcher) } }) var hbBtn = new HBox(10); hbBtn.setAlignment(javafx.geometry.Pos.BASELINE_RIGHT); hbBtn.getChildren().add(btn); //pane.getChildren.add(btn) pane.add(hbBtn, 40, 40) layoutX += 100 } primaryStage.setScene(new Scene(pane, 500, 500)) primaryStage.show } } 
  • Use HBox or FlowPane as the parent node and there will be no problems - Andrey M
  • @AndreyM but in the code in question I used HBox, another question is correct) - StriBog

1 answer 1

  override def start(primaryStage: Stage) { primaryStage.setTitle("For Romanov") val pane = new HBox val witchers: List[String] = List("The Witcher","The Witcher 2","The Witcher 3"); for (witcher <- witchers) { var btn = new Button(witcher) btn.setOnAction(ae => println(witcher)); pane.getChildren.add(btn) } primaryStage.setScene(new Scene(pane, 500, 500)) primaryStage.show } 
  • Not exactly the answer to the question, but he showed my mistake) It's all about the GridPane (table) - StriBog