0

I am trying to get a pop-up message when the cake node is clicked on. It prints to the console, but "JOptionPane.showMessageDialog(null,"Test");" crashes the program when I click on the cake (no errors). Any ideas?

class Cake extends Item {

double dx=3,dy=1.6;

Cake(String imageFile, double x, double y) {
    super(imageFile, x, y);
}


@Override
public void move() {
    this.setX(this.getX()+dx);

    if(this.getX()>749 || this.getX()<-20) {
        dx=-dx;
    }

    this.setY(this.getY()+dy);

    if(this.getY()>530 || this.getY()<0) {
        dy=-dy;
    }


}


@Override
public void collision() {
    //System.out.println("Cake");
    JOptionPane.showMessageDialog(null,"Test");


}

}
JSmith
  • 1
  • 2
  • Have you already read this [article](http://stackoverflow.com/questions/21108927/joptionpane-in-javafx-making-window-not-respond) – wake-0 Jun 04 '16 at 14:29
  • 2
    But note that since that was written, JavaFX introduced a [Dialog](http://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html) class. – James_D Jun 04 '16 at 14:31
  • I think instead of `null` there should be a stage – wake-0 Jun 04 '16 at 14:33
  • 2
    Umm, no. Don't mix Swing and FX. – James_D Jun 04 '16 at 14:33
  • Okay, I read the following [link](http://code.makery.ch/blog/javafx-dialogs-official/) and they use other classes for this purpose. – wake-0 Jun 04 '16 at 14:35

1 Answers1

3

Don't use Swing's JOptionPane in a JavaFX application. Use Dialog, or in this case an Alert instead:

@Override
public void collision() {
    //System.out.println("Cake");

    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setHeaderText("Test");
    alert.showAndWait();
}
James_D
  • 201,275
  • 16
  • 291
  • 322