Reputation: 2396
Introduction:
This is simply a "Table" where cards can be "placed" on, by right clicking on the table.
I have 2 FXML objects:
Problem:
In short, a Card
needs to be added to the UI "Table", this is where my problem lies.
The adding Card
method simply adds the card to a Pane
object (which is not null), and we know that Pane
's add objects which extend a Node
I tried 2 solutions, of which both failed.
Solution 1
Using the FXMLLoader to load the Card.fxml file into a Card object:
Having the Card definition as follows
public class Card implements Initializable
causes issues:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Layouts/Card.fxml"));
Card card = loader.load();
card.setLayoutX(mouseEvent.getSceneX() - (card.getLayoutX() / 2)); <--------here setLayoutX
card.setLayoutY(mouseEvent.getSceneY() - (card.getLayoutY() / 2)); <--------here setLayoutY
card.isFaceDown.addListener((observable, oldValue, newValue) -> {
if (newValue) {
facedownCount.add(1);
} else {
facedownCount.subtract(1);
}
});
totalCount.add(1);
gamePane.getChildren().add(card); <--------here card does not extend Node
The issue is I am unable to set layout properties of the Card object if it doesn't extend a stackpane, and also I am unable to add it to another object as a child
Solution 2: (more likely to be correct)
Changing the Card definition to
public class Card extends StackPane implements Initializable
followed by attempting to add it:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/Layouts/Card.fxml"));
Card card = loader.load(); < -------- PROBLEM
which gives me this exception error:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 29 more
Caused by: java.lang.ClassCastException: javafx.scene.layout.StackPane cannot be cast to Game.Controller.Card
at Game.Controller.UI.HandleClick(UI.java:60)
... 39 more
Card.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.effect.DropShadow?>
<StackPane maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.112"
fx:controller="Game.Controller.Card"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="imgSuit" fitHeight="275.0" fitWidth="183.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="imgValue" fitHeight="275.0" fitWidth="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../suits/back.png" />
</image>
</ImageView>
</children>
<effect>
<DropShadow />
</effect>
</StackPane>
Card Controller
public class Card implements Initializable {
public static HashMap cardSuit, cardValue;
public BooleanProperty isFaceDown = new SimpleBooleanProperty(true);
private IntegerProperty value = new SimpleIntegerProperty(-1);
public StringProperty suit = new SimpleStringProperty("");
public ImageView imgValue = new ImageView();
public ImageView imgSuit = new ImageView();
public Card(){
super();
}
@Override
public void initialize(URL location, ResourceBundle resources) {
initImages();
setupBindings();
setDefaults();
}
private void setDefaults() {
isFaceDown.set(true);
}
private void setupBindings() {
isFaceDown.addListener((observable, oldValue, newValue) -> {
if (newValue){
imgValue.setImage(null);
imgSuit.setImage((Image) cardSuit.get("B"));
}
else {
imgValue.setImage((Image) cardValue.get(value.get()));
imgSuit.setImage((Image) cardSuit.get(suit.get()));
}
});
}
private void initImages() {
if (cardSuit == null) {
cardSuit = new HashMap<String, Image>();
cardSuit.put("B", new Image("/suits/back.png"));
cardSuit.put("C", new Image("/suits/clubs.png"));
cardSuit.put("D", new Image("/suits/diamonds.png"));
cardSuit.put("S", new Image("/suits/spades.png"));
cardSuit.put("H", new Image("/suits/hearts.png"));
}
if (cardValue == null) {
cardValue = new HashMap<Integer, Image>();
for (int i = 1; i <= 13; i++) {
cardValue.put(i, new Image("/values/" + String.valueOf(i) + ".png"));
}
}
}
}
UI fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMousePressed="#HandleClick" prefHeight="750.0" prefWidth="702.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Game.Controller.UI">
<children>
<MenuBar prefHeight="29.0" prefWidth="700.0">
<menus>
<Menu mnemonicParsing="false" text="Menu">
<items>
<MenuItem fx:id="mmReset" mnemonicParsing="false" text="Reset" />
<MenuItem fx:id="mmClose" mnemonicParsing="false" onAction="#CloseApp" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem fx:id="mmAbout" mnemonicParsing="false" text="About" />
</items>
</Menu>
<Menu fx:id="mInstruct" mnemonicParsing="false" text="Instructions" />
</menus>
</MenuBar>
<StackPane fx:id="gamePane" layoutX="1.0" layoutY="29.0" prefHeight="700.0" prefWidth="700.0" style="-fx-background-color: green;" />
<HBox layoutX="7.0" layoutY="731.0" prefHeight="15.0" prefWidth="693.0">
<children>
<Label fx:id="lblHearts" text="Hearts = " />
<Label fx:id="lblDiamonds" text="Diamonds = " />
<Label fx:id="lblSpades" text="Spades = " />
<Label fx:id="lblClubs" text="Clubs = " />
<Label fx:id="lblFaceUp" text="Facing Up =" />
</children>
</HBox>
</children>
</AnchorPane>
What am I doing wrong, I think that solution #2 is more 'correct', but why doesn't the loader allow loading into the Card, with the StackPane cast exception
p.s. As a sub question, what difference does the root node make in this case, when loading the fxml object, will it allow me to e.g. setlayoutX
property if I don't explicitly extends StackPane
in the definition?
Upvotes: 0
Views: 871
Reputation: 82461
You could create a custom component that extends StackPane
and is used as the controller class controller by using the <fx:root>
element
public class Card extends StackPane implements Initializable {
public Card() {
FXMLLoader loader = new FXMLLoader(getClass().getResouce("card.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch (Exception e) {
throw new IllegalStateException("Could not load fxml", e);
}
}
...
}
<fx:root type="javafx.scene.layout.StackPane"
maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.112"
xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fx:id="imgSuit" fitHeight="275.0" fitWidth="183.0" layoutX="10.0" layoutY="10.0" pickOnBounds="true" preserveRatio="true" />
<ImageView fx:id="imgValue" fitHeight="275.0" fitWidth="183.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../suits/back.png" />
</image>
</ImageView>
</children>
<effect>
<DropShadow />
</effect>
</fx:root>
Upvotes: 2