Can't run fxml app using Intellij and 1.8 JDK - java

I am trying to run the following very simple app
Main.java:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java
package sample;
import java.awt.*;
public class Controller {
public TextField theight;
public TextField tweight;
public Label lbmi;
public Label lresult;
public void buttonclicked(){
double height;
double weight;
double bmi;
String result;
height = Double.parseDouble(theight.getText());
weight = Double.parseDouble(tweight.getText());
double square = height * height;
bmi = weight / square;
lbmi.setText("The BMI is " + bmi);
}
}
and sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label alignment="CENTER" prefHeight="43.0" prefWidth="210.0" text="BMI Calculator" GridPane.columnIndex="1">
<font>
<Font size="29.0" />
</font>
</Label>
<TextField fx:id="theight" alignment="CENTER" promptText="Enter your height in meters" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<TextField fx:id="tweight" alignment="CENTER" promptText="Enter your weight in kg" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<Button mnemonicParsing="false" onAction="#buttonclicked" text="Calculate" GridPane.columnIndex="2" GridPane.rowIndex="3" />
<Label fx:id="lbmi" alignment="CENTER" prefHeight="17.0" prefWidth="280.0" text="Your BMI will be displayed here" textOverrun="CLIP" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Label fx:id="lresult" alignment="CENTER" prefHeight="17.0" prefWidth="211.0" text="What your BMI means" GridPane.columnIndex="1" GridPane.rowIndex="4" />
</children>
</GridPane>
I have figured out that the controller was not added by default and added it as fx:controller="sample.Controller". However, I still get an error:
Caused by: javafx.fxml.LoadException:
/D:/JAVA/BMIC/out/production/BMIC/sample/sample.fxml:30

In your controller class you are importing awt classes for TextField and Label. Import the javafx classes.
Edit:
You have to remove the awt imports and insert the javafx imports:
package sample;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class Controller {
public TextField theight;
public TextField tweight;
public Label lbmi;
public Label lresult;
public void buttonclicked() {
double height;
double weight;
double bmi;
String result;
height = Double.parseDouble(theight.getText());
weight = Double.parseDouble(tweight.getText());
double square = height * height;
bmi = weight / square;
lbmi.setText("The BMI is " + bmi);
}
}

Related

Exception running application application.TipCalculator

So I was given code from the textbook to use in eclipse and it actually won't run the base code even though I did everything the professor told me to do. Im getting an error exception running application application.TipCalculator and I have no idea what to do. First code snippet is my main, second is my controller, third is my xml file.
TipCalculator.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TipCalculator extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/TipCalculator.fxml"));
Scene scene = new Scene(root); // attach scene graph to scene
stage.setTitle("Tip Calculator"); // displayed in window's title bar
stage.setScene(scene); // attach scene to stage
stage.show(); // display the stage
}
public static void main(String[] args) {
// create a TipCalculator object and call its start method
launch(args);
}
}
TipCalculatorController.java
package application;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
public class TipCalculatorController {
// formatters for currency and percentages
private static final NumberFormat currency =
NumberFormat.getCurrencyInstance();
private static final NumberFormat percent =
NumberFormat.getPercentInstance();
private BigDecimal tipPercentage = new BigDecimal(0.15); // 15% default
// GUI controls defined in FXML and used by the controller's code
#FXML
private TextField amountTextField;
#FXML
private Label tipPercentageLabel;
#FXML
private Slider tipPercentageSlider;
#FXML
private TextField tipTextField;
#FXML
private TextField totalTextField;
// calculates and displays the tip and total amounts
#FXML
private void calculateButtonPressed(ActionEvent event) {
try {
BigDecimal amount = new BigDecimal(amountTextField.getText());
BigDecimal tip = amount.multiply(tipPercentage);
BigDecimal total = amount.add(tip);
tipTextField.setText(currency.format(tip));
totalTextField.setText(currency.format(total));
}
catch (NumberFormatException ex) {
amountTextField.setText("Enter amount");
amountTextField.selectAll();
amountTextField.requestFocus();
}
}
// called by FXMLLoader to initialize the controller
public void initialize() {
// 0-4 rounds down, 5-9 rounds up
currency.setRoundingMode(RoundingMode.HALF_UP);
// listener for changes to tipPercentageSlider's value
tipPercentageSlider.valueProperty().addListener(
new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> ov,
Number oldValue, Number newValue) {
tipPercentage =
BigDecimal.valueOf(newValue.intValue() / 100.0);
tipPercentageLabel.setText(percent.format(tipPercentage));
}
}
);
}
}
TipCalculator.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="8.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-
Infinity" minWidth="-Infinity"
xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.TipCalculatorController">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Amount" />
<Label fx:id="tipPercentageLabel" text="15%" GridPane.rowIndex="1" />
<Label text="Tip" GridPane.rowIndex="2" />
<Label text="Total" GridPane.rowIndex="3" />
<TextField fx:id="amountTextField" GridPane.columnIndex="1" />
<TextField fx:id="tipTextField" editable="false" focusTraversable="false" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField fx:id="totalTextField" editable="false" focusTraversable="false" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Slider fx:id="tipPercentageSlider" blockIncrement="5.0" max="30.0" value="15.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#calculateButtonPressed" text="Calculate" GridPane.columnIndex="1" GridPane.rowIndex="4" />
</children>
<padding>
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0" />
</padding>
</GridPane>
The error im getting is:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: javafx.fxml.LoadException:
/C:/Users/Bandi/eclipse-workspace/TipCalculator/bin/TipCalculator.fxml:41
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at application.TipCalculator.start(TipCalculator.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.NumberFormatException: For input string: "- Infinity"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:549)
at java.base/java.lang.Double.valueOf(Double.java:512)
at javafx.fxml/com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:450)
at javafx.fxml/com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at javafx.fxml/com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.applyProperty(FXMLLoader.java:520)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:370)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:332)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:242)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:775)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
... 17 more
Exception running application application.TipCalculator
Please help.
firstly you need to change the controller which you added in application package:
note: the / make it to read from the src folder because they are not in the same package.
TipController
change this:
Parent root = FXMLLoader.load(getClass().getResource("TipCalculator.fxml"));
to
Parent root = FXMLLoader.load(getClass().getResource("/TipCalculator.fxml"));
then change the fxml file where you specified the path of controller:
TipCalculator.fxml
<GridPane hgap="8.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TipCalculatorController">
Complete Code:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<GridPane hgap="8.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.TipCalculatorController">
<columnConstraints>
<ColumnConstraints halignment="RIGHT" hgrow="SOMETIMES" minWidth="10.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="Amount" />
<Label fx:id="tipPercentageLabel" text="15%" GridPane.rowIndex="1" />
<Label text="Tip" GridPane.rowIndex="2" />
<Label text="Total" GridPane.rowIndex="3" />
<TextField fx:id="amountTextField" GridPane.columnIndex="1" />
<TextField fx:id="tipTextField" editable="false" focusTraversable="false" GridPane.columnIndex="1" GridPane.rowIndex="2" />
<TextField fx:id="totalTextField" editable="false" focusTraversable="false" GridPane.columnIndex="1" GridPane.rowIndex="3" />
<Slider fx:id="tipPercentageSlider" blockIncrement="5.0" max="30.0" value="15.0" GridPane.columnIndex="1" GridPane.rowIndex="1" />
<Button maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#calculateButtonPressed" text="Calculate" GridPane.columnIndex="1" GridPane.rowIndex="4" />
</children>
<padding>
<Insets bottom="14.0" left="14.0" right="14.0" top="14.0" />
</padding>
</GridPane>

Move cards inside vbox javafx?

I have a vbox which contains a list of cards(Anchor panes). I am trying to make each card draggable so that I can change the order of the list whenever I want. I am using a scene builder.
However, I keep getting the following message after I drag and attempt to drop.
Java Messsge:class javafx.scene.layout.VBox cannot be cast to class javafx.scene.layout.AnchorPane(javafx.scene.layout.VBox and javafx.scene.layout.AnchorPane are in module javafx.graphics of loader 'app')
My code:
minimal code provided only contains two cards, one of which is being used only in my code.
Edit: I no longer get the error message however, I am not getting my desired result as the cards are not changing positions
mport javafx.beans.property.ObjectProperty;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TitledPane;
import javafx.scene.image.WritableImage;
import javafx.scene.input.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Controller
{
#FXML public AnchorPane card1, card2;
#FXML public AnchorPane current;
#FXML public GridPane gridPane;
#FXML public GridPane column1;
#FXML public AnchorPane col1;
#FXML public Button button1, button2;
#FXML public Button currentButton;
#FXML public VBox v1;
#FXML
public void detect() { //detect card
card1.setOnDragDetected((MouseEvent event) -> { //card1 is the card
Dragboard db = card1.startDragAndDrop(TransferMode.MOVE);
ClipboardContent content = new ClipboardContent();
content.putString(card1.getId());
WritableImage snapshot = card1.snapshot(new SnapshotParameters(), null);
db.setDragView(snapshot);
db.setContent(content);
event.consume();
});
}
#FXML
public void accept() { //add to vbox
v1.addEventHandler(DragEvent.DRAG_OVER, (DragEvent event) -> { //v1 is the vbox
if (event.getGestureSource() != v1
&& event.getDragboard().hasString()) {
event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
}
event.consume();
});
}
#FXML
public void drop() {
v1.addEventHandler(DragEvent.DRAG_DROPPED, (DragEvent event) -> {
Dragboard db = event.getDragboard();
boolean success = false;
if (db.hasString()) {
VBox parent = (VBox) card1.getParent();
//int targetIndex = parent.getChildren().indexOf(card1);
List<Node> nodes = new ArrayList<Node>(parent.getChildren());
parent.getChildren().clear();
parent.getChildren().addAll(nodes);
success = true;
}
event.setDropCompleted(success);
event.consume();
});
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="772.0" prefWidth="1295.0" style="-fx-background-color: #C5F5D4;" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="KanbanBoard.Controller">
<children>
<AnchorPane layoutY="47.0" prefHeight="687.0" prefWidth="1295.0">
<children>
<AnchorPane fx:id="col1" layoutX="-1.0" prefHeight="724.0" prefWidth="216.0">
<children>
<GridPane fx:id="column1" layoutX="33.0" layoutY="15.0" onMouseDragOver="#accept" prefHeight="724.0" prefWidth="209.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="7.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="353.8666748046875" minHeight="10.0" prefHeight="103.20001831054685" vgrow="SOMETIMES" />
<RowConstraints maxHeight="621.5999816894531" minHeight="10.0" prefHeight="621.5999816894531" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<VBox fx:id="v1" onDragDropped="#drop" onDragOver="#accept" prefHeight="200.0" prefWidth="210.0" GridPane.rowIndex="1">
<children>
<AnchorPane fx:id="card1" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#detect" onMouseDragged="#handleLabel" prefHeight="125.0" prefWidth="198.0" style="-fx-background-color: #E5EAE6;">
<children>
<TextField layoutX="5.0" layoutY="3.0" prefHeight="26.0" prefWidth="65.0" promptText="ID" />
<TextField layoutX="72.0" layoutY="3.0" prefHeight="26.0" prefWidth="124.0" promptText="Title" />
<TextArea layoutX="5.0" layoutY="32.0" prefHeight="50.0" prefWidth="190.0" promptText="Description" />
<TextField layoutX="5.0" layoutY="85.0" prefHeight="26.0" prefWidth="124.0" promptText="Story point" />
<Button fx:id="button1" layoutX="164.0" layoutY="85.0" mnemonicParsing="false" onMouseClicked="#removeCard" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="-" />
<Button layoutX="129.0" layoutY="85.0" mnemonicParsing="false" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="+" />
</children>
</AnchorPane>
<AnchorPane fx:id="card2" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onDragDetected="#detect" onMouseClicked="#handleLabel" prefHeight="125.0" prefWidth="198.0" style="-fx-background-color: #E5EAE6;">
<children>
<TextField layoutX="5.0" layoutY="3.0" prefHeight="26.0" prefWidth="65.0" promptText="ID" />
<TextField layoutX="72.0" layoutY="3.0" prefHeight="26.0" prefWidth="124.0" promptText="Title" />
<TextArea layoutX="5.0" layoutY="32.0" prefHeight="50.0" prefWidth="190.0" promptText="Description" />
<TextField layoutX="5.0" layoutY="85.0" prefHeight="26.0" prefWidth="124.0" promptText="Story point" />
<Button fx:id="button2" layoutX="164.0" layoutY="85.0" mnemonicParsing="false" onMouseClicked="#removeCard" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="-" />
<Button layoutX="129.0" layoutY="85.0" mnemonicParsing="false" prefHeight="24.0" prefWidth="31.0" style="-fx-background-color: #ECF4EE;" text="+" />
</children>
</AnchorPane>
</children>
</VBox>
<Pane prefHeight="98.0" prefWidth="226.0" style="-fx-background-color: #D1D3D1;">
<children>
<Button layoutX="124.0" layoutY="64.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="26.0" text="-" />
<TextArea layoutY="39.0" prefHeight="50.0" prefWidth="124.0" promptText="Role" />
<TextField layoutX="1.0" layoutY="5.0" prefHeight="26.0" prefWidth="124.0" promptText="Name" />
<Button fx:id="addColumnButton" layoutX="124.0" layoutY="36.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="26.0" text="+" />
</children>
</Pane>
</children>
</GridPane>
</children>
</AnchorPane>
<AnchorPane layoutX="1082.0" prefHeight="726.0" prefWidth="213.0">
<children>
<Pane prefHeight="50.0" prefWidth="214.0" style="-fx-background-color: #8F9691;">
<children>
<Label layoutX="49.0" layoutY="7.0" prefHeight="36.0" prefWidth="46.0" text="Log" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS">
<font>
<Font size="22.0" />
</font>
</Label>
</children>
</Pane>
<Pane layoutY="49.0" prefHeight="676.0" prefWidth="214.0" style="-fx-background-color: #C6C8C6;" />
</children>
</AnchorPane>
<GridPane fx:id="gridPane" layoutX="228.0" onDragDropped="#drop" onDragOver="#accept" prefHeight="726.0" prefWidth="856.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="627.1999938964843" minHeight="10.0" prefHeight="622.4000061035156" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</AnchorPane>
<Label layoutX="381.0" layoutY="-4.0" prefHeight="50.0" prefWidth="180.0" text="Kanban Board">
<font>
<Font size="28.0" />
</font>
</Label>
</children>
</AnchorPane>
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.*;
import javafx.stage.Stage;
import java.io.IOException;
public class Kanban extends Application {
//private Button b;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("Kanban.fxml"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Controller controller = new Controller();
FXMLLoader loader = new FXMLLoader();
loader.setController(controller);
//Controller c = loader.getController();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Can someone help me out please.
AnchorPane parent = (AnchorPane) card1.getParent(); (In the drop() method, at the bottom of your code)
card1.getParent() with a type of VBox cannot be cast to AnchorPane

I ran a jar file using Java fx on raspberrypi3, but the background image gif is not output

The background image is normally output on the desktop, but it is not output on the raspberry pie.
public class Main extends Application {
#Override
public void start(Stage stage)throws IOException {
Parent root=FXMLLoader.load(getClass().getResource("prototype.fxml"));
stage.setScene(new Scene(root));
stage.setMaximized(true);
stage.setTitle("Prototype");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is the code for my Javafx Controller.java
public class Controller implements Initializable{
#FXML private StackPane background;
File five=new File("/home/pi/Desktop/image/snow.gif");
private Image snow=new Image(five.toURI().toString());
#Override
public void initialize(URL location, ResourceBundle resources) {
Thread thread = new Thread() {
#Override
public void run() {
while(true) {
Platform.runLater(()->{
BackgroundImage e=new BackgroundImage(snow,BackgroundRepeat.NO_REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.CENTER,new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, true, true, true, true));
background.setBackground(new Background(e));
});
try {Thread.sleep(1000);}catch(InterruptedException e) {}
}
};
};
thread.setDaemon(true);
thread.start();
};
}
And this is xml written by a scene builder.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<StackPane fx:id="background" maxHeight="500.0" maxWidth="1000.0" minHeight="250.0" minWidth="500.0" prefHeight="500.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<GridPane alignment="CENTER" maxHeight="250.0" maxWidth="300.0" minHeight="50.0" minWidth="50.0" prefHeight="254.0" prefWidth="276.0" rotate="180.0" style="-fx-background-color: #ffffff; -fx-background-color: rgba(255,255,255,0.5); -fx-background-radius: 50;" StackPane.alignment="CENTER">
<children>
<Label fx:id="watch" alignment="CENTER" style="-fx-font-size: 60;" text="Time" />
<Label fx:id="weeken" style="-fx-font-size: 30;" text="Week" GridPane.rowIndex="1" />
<Label fx:id="weathering" style="-fx-font-size: 30;" text="Weathing" GridPane.rowIndex="2" />
</children>
<columnConstraints>
<ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rotationAxis>
<Point3D y="1.0" />
</rotationAxis>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</children>
</StackPane>
It’s part of the original program. I need to convert weather data into a gif background image. But the image is not output. I want help.

JavaFX - Multiple Grid Panes - Only One is connecting, the rest are null

In my JavaFX program I have multiple grid panes. The FXML code I have is here.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.shape.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="553.0" prefWidth="586.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pkg3dtictactoe.FXMLDocumentController">
<children>
<VBox prefHeight="489.0" prefWidth="586.0">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem id="NewGame" mnemonicParsing="false" onAction="#handleNewGameAction" text="New Game" />
<MenuItem id="SaveGame" mnemonicParsing="false" onAction="#handleSaveGameAction" text="Save Game" />
<MenuItem id="Quit" mnemonicParsing="false" onAction="#handleQuitGameAction" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<GridPane fx:id="topGrid" alignment="CENTER" gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#topGridClicked" prefHeight="175.0" prefWidth="175.0" rotate="-73.0" style="-fx-background-color: #FFFFFF;" VBox.vgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<rotationAxis>
<Point3D x="1.0" y="1.0" z="1.0" />
</rotationAxis>
<VBox.margin>
<Insets left="200.0" top="50.0" />
</VBox.margin>
</GridPane>
<GridPane fx:id="middleGrid" gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#middleGridClicked" prefHeight="175.0" prefWidth="175.0" rotate="-73.0" style="-fx-background-color: #FFFFFF;" VBox.vgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<rotationAxis>
<Point3D x="1.0" y="1.0" z="1.0" />
</rotationAxis>
<VBox.margin>
<Insets left="200.0" top="-45.0" />
</VBox.margin>
</GridPane>
<GridPane fx:id="lowerGrid" gridLinesVisible="true" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" onMouseClicked="#lowerGridClicked" prefHeight="175.0" prefWidth="175.0" rotate="-73.0" style="-fx-background-color: #FFFFFF;" VBox.vgrow="NEVER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<rotationAxis>
<Point3D x="1.0" y="1.0" z="1.0" />
</rotationAxis>
<VBox.margin>
<Insets left="200.0" top="-40.0" />
</VBox.margin>
</GridPane>
<Label prefHeight="17.0" prefWidth="588.0" />
</children>
</VBox>
<Line endX="172.0" endY="410.0" startX="172.0" startY="145.0" style="-fx-opacity: .2;" />
<Line endX="403.0" endY="447.0" startX="403.0" startY="182.0" style="-fx-opacity: .2;" />
<Line endX="265.0" endY="353.0" startX="265.0" startY="89.0" style="-fx-opacity: .2;" />
<Line endX="310.0" endY="500.0" startX="310.0" startY="235.0" style="-fx-opacity: .2;" />
</children>
</AnchorPane>
The FXML Controller code is the following
package pkg3dtictactoe;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.GridPane;
/**
*
*/
public class FXMLDocumentController implements Initializable {
#FXML
private GridPane topGrid;
private GridPane middleGrid;
private GridPane lowerGrid;
private Image imageX = null;
private Image imageO = null;
private Image imageEmpty = null;
public FXMLDocumentController(){
imageEmpty = new Image("resources/empty.png");
imageX = new Image("resources/x.png");
imageO = new Image("resources/0.png");
}
#FXML
private void handleButtonAction(ActionEvent event) {
}
#FXML
private void handleNewGameAction(ActionEvent event) throws IOException {
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
resetNode(topGrid, i, j);
resetNode(middleGrid, i, j);
resetNode(lowerGrid, i, j);
}
}
}
private void resetNode(GridPane grid, int i, int j) {
Node node = getNodeByRowColumnIndex(j, i, grid);
grid.getChildren().remove(node);
grid.add(new ImageView(imageEmpty), j, i);
}
public Node getNodeByRowColumnIndex(final int row,final int column,GridPane gridPane) {
Node result = null;
ObservableList<Node> childrens = gridPane.getChildren();
for(Node node : childrens) {
if(node instanceof ImageView && gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) {
result = node;
break;
}
}
return result;
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
When I run debug into the constructor, topGrid has information, however middleGrid and lowerGrid do not. There doesn't appear to be anything special when it comes to topGrid and it matches the code for the other GridPanes. Is it not possible to have multiple grid panes in JavaFX?
#FXML annotations apply to the Object declared directly after them. You need to write the annotation multiple times on the lines above every component with an fx:id to connect them.

how to resize javafx tab length

I create a simple javafx page.I have a tabPane that has tabs. I wrap tabPane in gridPane that when my page resize it resize too. It work correct and tabPane resize but it's tabs not resize(the size of the tab , not size of the tab content). how can i do that???
TabPane.java:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class TabPane extends Application {
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
GridPane ap = (GridPane) FXMLLoader.load(getClass().getResource("gridTest.fxml"));
Scene scene = new Scene(ap);
stage.setScene(scene);
stage.show();
}
}
gridTest.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="50.0" minWidth="50.0" prefHeight="588.0" prefWidth="600.0" stylesheets="#../GridTest.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints maxHeight="190.0" minHeight="10.0" percentHeight="10.0" prefHeight="11.0" vgrow="SOMETIMES" />
<RowConstraints maxHeight="381.0" minHeight="10.0" percentHeight="10.0" prefHeight="381.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" percentHeight="84.4" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children><VBox prefHeight="200.0" prefWidth="100.0" style="-fx-background-color: red;" /><TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" GridPane.rowIndex="1">
<tabs>
<Tab styleClass="tb" text="Untitled Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab styleClass="tb" text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane><StackPane prefHeight="150.0" prefWidth="200.0" style="-fx-background-color: blue;" GridPane.rowIndex="2" />
</children>
</GridPane>

Categories