0

I'm using Scene Builder to build a javafx gui, and I want a ComboBox where something will happen "On Showing." It looks simple enough, but it crashes when I try to implement it. I created a simple version having nothing but a combobox, and it still crashes.

Here is the Main.java module:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
//          BorderPane root = new BorderPane();
            BorderPane root = (BorderPane) FXMLLoader.load(Main.class.getResource("Comboscreen.fxml"));

            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

Here is the fxml:

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

<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>

<BorderPane xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
   <center>
      <VBox alignment="CENTER" prefHeight="400.0" prefWidth="400.0" BorderPane.alignment="CENTER">
         <children>
            <ComboBox fx:id="ComboTestBox" onAction="#ComboDo" onShowing="#Showaction" prefWidth="150.0" promptText="Testbox" />
         </children>
      </VBox>
   </center>
</BorderPane>

and here is the Controller:

package application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.ComboBox;

public class Controller {

    @FXML
    public ComboBox<String> ComboTestBox;

    @FXML
    void ComboDo(ActionEvent event) {
        System.out.println(" Option Chosen");

    }

    ObservableList<String> options = 
            FXCollections.observableArrayList(
                "Option 1",
                "Option 2",
                "Option 3"
            );

    @FXML
  void Showaction(ActionEvent event) {
        System.out.println(" TestAction");
    }



    public void initialize() {
    ComboTestBox.setItems(options); 
    }       


}

It looks simple enough, but it crashes every time. If I remove the "onshowing" action, it works just fine. Any advice is appreciated, as I am pretty new at this.

Howard
  • 13
  • 4

1 Answers1

0

Assuming by "crashing" you mean it gives you a runtime exception (for future reference, please include the stack trace, which has lots of information for you to diagnose the problem, in your question):

The onShowing handler is an EventHandler<Event>, not an EventHandler<ActionEvent>, so you need:

@FXML
void Showaction(Event event) {
    System.out.println(" TestAction");
}

Note that if you're not using the Event parameter, you can omit it, and the FXMLLoader will still be able to map to the correct handler method:

@FXML
void Showaction() {
    System.out.println(" TestAction");
}

However, if you include the parameter, it must be of the correct type.

As an aside, you should use proper naming conventions, i.e.

@FXML
void showAction(Event event) {
    System.out.println(" TestAction");
}

with the corresponding change to your FXML file.

Community
  • 1
  • 1
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thank you so much. Your fix worked right away. The Sample Controller Skeleton generated by Scene Builder when I chose an action to occur On Showing included the ActionEvent that I used. Is that a bug in Scene Builder? – Howard Jun 07 '16 at 12:24
  • Yeah, I'd say so. I've never used the sample controller skeleton (I tend to write controllers before I write the view). – James_D Jun 07 '16 at 12:30