简体   繁体   中英

Add an action in the Scene Builder

I am newly using the Scene builder and I would like to add a click action to a button. However, I don't see the dropdown menu to add the click action. I am in the Mac OS platform and using the IntelliJ. The screenshot is provided,

在此处输入图片说明

The sample.fxml file is provided below,

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane cache="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="445.0" prefWidth="597.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Button fx:id="btnn" layoutX="228.0" layoutY="33.0" mnemonicParsing="false" text="Button" />
   </children>
</Pane>

I redownloaded the installed the scene builder and this doesn't solve the issue. How do I add an action to the Scene builder?

There are few things you must keep in mind while working with Scenebuilder .

  1. Specify the controller in the root element. It can be done as given below. 在此处输入图片说明
  2. Without specifying controller, you won't get any drop down for selecting the action. You first need to specify controller. Don't proceed furthur, if you haven't.

  3. Below given is the correct way to create a method for button action.

    @FXML private void buttonAction(ActionEvent event) { //your code }

  4. Before looking for onAction drop down menu. Make sure you have created the method (that you're looking for in the drop down) in the controller class.

I had to manually edit the sample.fxml file to have the drop-down menu available.

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.Pane?>

<Pane cache="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="445.0" prefWidth="597.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainController">
   <children>
      <Button fx:id="btnn" layoutX="228.0" layoutY="33.0" mnemonicParsing="false" onAction="#onPress" text="Button" />
   </children>
</Pane>

The other answer is informing that we can have the Document menu to set the controller which I don't have here.

在此处输入图片说明

Solution

Open the sample.xml file in the Scene Builder app than IntelliJ Interface and the issue is solved. It seems there might be a bug in the IntelliJ or a compatibility issue.

This is the first time I use the JavaFX, so not sure what is the exact issue, but, I find a way to walk though.

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