I have built a GUI using scene builder, through eclipse.
I am trying to run the MainApp class to view the GUI in eclipse, everything compiles, there are no error messages in the console, a java process opens but nothing actually happens and no window is created on screen.
I have been through oracle's site and attempted to alter the code but nothing works. I have updated java, reinstalled javafx etc in eclipse. I also tried the program throuhg netbeans to the same effect.
Running on MacOS X
package main;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class MainApp extends Application {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(MainApp.class, (java.lang.String[])null);
}
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("GUI.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
`package main;
The controller class
public class Controller implements Initializable {
#FXML
Button checkButton;
#FXML
Button callButton;
#FXML
Button raiseButton;
#FXML
Button foldButton;
public void buttonClicked() {
System.out.println("Button was clicked");
}
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
}
}
The FXML file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ButtonBar?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="800.0" style="-fx-background-color: #FFFFFF;" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.Controller">
<children>
<SplitPane dividerPositions="0.29949874686716793" layoutX="123.0" layoutY="67.0" prefHeight="433.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="67.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<LineChart layoutY="206.0" prefHeight="215.0" prefWidth="235.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
<LineChart prefHeight="215.0" prefWidth="235.0">
<xAxis>
<CategoryAxis side="BOTTOM" />
</xAxis>
<yAxis>
<NumberAxis side="LEFT" />
</yAxis>
</LineChart>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<SplitPane dividerPositions="0.6783216783216783" orientation="VERTICAL" prefHeight="498.0" prefWidth="555.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0" />
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="127.0" prefWidth="553.0">
<children>
<TextArea disable="true" layoutX="9.0" layoutY="23.0" prefHeight="106.0" prefWidth="536.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="9.0" AnchorPane.rightAnchor="8.0" AnchorPane.topAnchor="23.0" />
<Label layoutX="14.0" layoutY="6.0" text="Live guidance:" />
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
</items>
</SplitPane>
<ImageView fitHeight="62.0" fitWidth="200.0" layoutX="14.0" layoutY="5.0">
<image>
<Image url="#../../../../../Desktop/Dissertation%20files/logo.jpg" />
</image>
</ImageView>
<ButtonBar layoutX="456.0" layoutY="16.0" prefHeight="40.0" prefWidth="200.0">
<buttons>
<Button fx:id="checkButton" mnemonicParsing="false" onAction="#buttonClicked" onMouseClicked="#buttonClicked" style="-fx-background-radius: 10;" text="Check" />
<Button fx:id="callButon" mnemonicParsing="false" onAction="#buttonClicked" onMouseClicked="#buttonClicked" style="-fx-background-radius: 10;" text="Call" />
<Button fx:id="raiseButton" mnemonicParsing="false" onAction="#buttonClicked" onMouseClicked="#buttonClicked" prefHeight="27.0" prefWidth="81.0" style="-fx-background-radius: 10;" text="Raise" />
<Button fx:id="foldButton" mnemonicParsing="false" onAction="#buttonClicked" onMouseClicked="#buttonClicked" style="-fx-background-radius: 10;" text="Fold" />
</buttons>
</ButtonBar>
</children>
</AnchorPane>
Apologies if I am missing something simple.
If the classes and the fxml file is under same package then the loader line should be like this
Parent root = FXMLLoader.load(getClass().getResource("/package_name/fxml2GUI.fxml"));
Put your source's package name in the place of "packagename"
Related
I'm trying to create a grid/tile like page using HBoxes and display text inside of the HBoxes, but the children(Label and Button) are restricted to the top leftof the HBox. I've been using scenebuilder, but even when I set the Layout X and Y the children stay in the same positions. All of the HBoxes have this problem.
FXML Example
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.improved.HelloController">
<children>
<HBox fx:id="hBox" prefHeight="100.0">
<children>
<TabPane prefHeight="100.0" prefWidth="${hBox.width}" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Home">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0">
<children>
<HBox fx:id="lastRaceHBox" prefHeight="386.0" prefWidth="400.0">
<children>
<VBox>
<children>
<Button mnemonicParsing="false" onAction="#onHelloButtonClick" text="Hello!" />
<Label fx:id="welcomeText" prefHeight="17.0" prefWidth="68.0" />
</children>
</VBox>
</children></HBox>
<HBox fx:id="nextRaceHBox" layoutX="400.0" prefHeight="386.0" prefWidth="400.0" />
<HBox fx:id="upcomingDatesHBox" layoutX="800.0" prefHeight="386.0" prefWidth="400.0" />
<HBox fx:id="rankingsHBox" layoutY="386.0" prefHeight="386.0" prefWidth="400.0" />
<HBox fx:id="goalsHBox" layoutX="400.0" layoutY="386.0" prefHeight="386.0" prefWidth="400.0" />
<HBox fx:id="playerInfoHBox" layoutX="800.0" layoutY="386.0" prefHeight="386.0" prefWidth="400.0" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Dates">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Race">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Player Info">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Player Search">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</HBox>
</children>
</VBox>
Controller Class
package com.example.improved;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
#FXML
private Label welcomeText;
#FXML
protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
Main Application Class
package com.example.improved;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("MainMenu.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1280, 960);
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Is there any way to freely move the children?
The HBox API outlines the available constraints. In particular, "The alignment of the content is controlled by the alignment property, which defaults to Pos.TOP_LEFT." You can specify the desired Pos value. You may also want to experiment with the settings that control Resizable Range and Optional Layout Constraints.
As a concrete example, LayoutSample.java, illustrated here, specifies a grow constraint that allows helpIcon to stay on the right as the stage is resized:
HBox.setHgrow(stack, Priority.ALWAYS);
More examples are seen here.
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 am working on simple application which I need to make executable.
I am trying to switch from one scene to another in same window. Everything work perfectly when running in Intellij. However, when I created executable app suddenly control buttons stopped working.
There is my main class:
package code;
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("FxmlLoginMenu.fxml"));
primaryStage.setTitle("Quiz application");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller Class:
One method inside which is executed when button pressed
package code;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import java.io.IOException;
public class Controller {
#FXML public Button signUp;
#FXML public void signUp() throws IOException {
Parent tableViewParent = FXMLLoader.load(getClass().getResource("FXMLRegister.fxml"));
Scene tableViewScene = new Scene(tableViewParent);
Stage window = (Stage) signUp.getScene().getWindow();
window.setScene(tableViewScene);
window.show();
}
}
And the last FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.effect.InnerShadow?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Line?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane fx:controller="code.Controller" prefHeight="385.0" prefWidth="437.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" >
<children >
<MenuBar />
<AnchorPane id="login" maxHeight="-1.0" maxWidth="-1.0" prefHeight="418.0" prefWidth="626.0" style="-fx-background-color: #74AFAD#74AFAD;" VBox.vgrow="ALWAYS">
<children>
<AnchorPane id="login2" layoutX="325.0" layoutY="40.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="291.0" prefWidth="313.0" style="-fx-background-color: #D9853B#D9853B;">
<children>
<TextField fx:id="loginField" alignment="TOP_CENTER" layoutX="75.0" layoutY="95.0" promptText="Login">
<effect>
<InnerShadow />
</effect>
</TextField>
<PasswordField fx:id="passwordField" alignment="TOP_CENTER" layoutX="75.0" layoutY="159.0" promptText="Password">
<effect>
<InnerShadow blurType="TWO_PASS_BOX" />
</effect>
</PasswordField>
<Button fx:id="loginButton" layoutX="177.0" layoutY="247.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="65.0" style="-fx-background-color: #74AFAD#74AFAD #74AFAD#74AFAD;" text="Login" />
<Button fx:id="signUp" onAction="#signUp" layoutX="75.0" layoutY="247.0" mnemonicParsing="false" style="-fx-background-color: #74AFAD#74AFAD #74AFAD#74AFAD;" text="Sign up" />
<Button fx:id="signUpAdmin" layoutX="242.0" layoutY="8.0" mnemonicParsing="false" style="-fx-background-color: #74AFAD#74AFAD #74AFAD#74AFAD;" text="Admin" />
</children>
</AnchorPane>
<Line endX="100.0" fill="#ff2323" layoutX="360.0" layoutY="190.0" rotate="90.0" startX="-190.0" stroke="#e43030" strokeLineJoin="ROUND" strokeMiterLimit="0.0" />
<Text fill="#d9853b" layoutX="21.0" layoutY="202.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Welcome to Quiz app" wrappingWidth="284.21875">
<font>
<Font size="27.0" />
</font>
</Text>
</children>
</AnchorPane>
<AnchorPane />
</children>
</AnchorPane>
Ok, I managed to fix that.
The problem was in the Controller class. It tried to load FXML with
FXMLLoader.load(getClass().getResource("FXMLRegister.fxml"));
where it should be
FXMLLoader.load(getClass().getResource("FxmlRegister.fxml"));
Apparently IntelliJ did not catch that error so it was working while running by IntelliJ.
I'm new to java Fx. I have a TabPanel with 3 Tabs. Each Tab has many controls (text, buttons, etc.), and what I want is to assign a one controller for all Tab. The SceneBuilder only let me assign a controller for the whole view, I mean, only the top panel (the root) has the "Controller class" option, so How to write the code for all the tabs in one class.
i have .fxml file as:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<Pane lns="http://javafx.com/javafx/8"xmlns:fx="http://javafx.com/fxml/1"
fx:controller="Application.LoginController">
<children>
<TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight=" -
Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0"
tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Register">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0"
prefWidth="200.0">
<children>
<Label layoutX="27.0" layoutY="45.0" text="Name" />
<Label layoutX="27.0" layoutY="102.0" text="Password"
/>
<Label layoutX="27.0" layoutY="151.0" text="City" />
<Label layoutX="27.0" layoutY="204.0" text="Email" />
<Label layoutX="27.0" layoutY="246.0" text="Phone" />
<TextField fx:id="name"
layoutX="164.0"layoutY="41.0"/>
<TextField fx:id="passwd" layoutX="164.0"
layoutY="98.0" />
<TextField fx:id="city" layoutX="164.0"
layoutY="147.0" />
<TextField fx:id="email" layoutX="164.0"
layoutY="200.0" />
<TextField fx:id="phone" layoutX="164.0"
layoutY="242.0" />
<Button fx:id="register" layoutX="129.0"
layoutY="308.0" mnemonicParsing="false" text="Register" />
<Button fx:id="cancle" cancelButton="true"
layoutX="274.0" layoutY="308.0" mnemonicParsing="false" text="Cancle" />
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Login">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0"
prefWidth="200.0">
<children>
<Label layoutX="26.0" layoutY="57.0" text="User Name"
/>
<Label layoutX="26.0" layoutY="103.0" text="Password"
/>
<Button fx:id="myLogin" layoutX="145.0"
layoutY="186.0" mnemonicParsing="false" text="Login" />
<Button fx:id="cancle" cancelButton="true"
layoutX="274.0" layoutY="186.0" mnemonicParsing="false" text="Cancle" />
<TextField fx:id="uName" layoutX="145.0"
layoutY="53.0" prefHeight="25.0" prefWidth="205.0" />
<TextField fx:id="pwd" layoutX="148.0" layoutY="99.0"
prefHeight="25.0" prefWidth="200.0" />
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</Pane>
This app demonstrates how to use a controller to interact with different Nodes in different Tabs in a TabPane.
Main
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication151 extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
*
* #author blj0011
*/
public class FXMLDocumentController implements Initializable
{
//Tab1 nodes
#FXML private Label lblTab1;
#FXML private TextField tfTab1;
//Tab2 nodes
#FXML private Label lblTab2;
#FXML private TextField tfTab2;
#Override
public void initialize(URL url, ResourceBundle rb)
{
//This code set the Tab1 label's text to what is show in the TextField on Tab1
tfTab1.setOnKeyReleased((event)->{
lblTab1.setText(tfTab1.getText());
});
//This code set the Tab2 label's text to what is show in the TextField on Tab2
tfTab2.setOnKeyReleased((event)->{
lblTab2.setText(tfTab2.getText());
});
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.StackPane?>
<AnchorPane id="AnchorPane" prefHeight="353.0" prefWidth="588.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="javafxapplication151.FXMLDocumentController">
<children>
<Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
<TabPane layoutX="87.0" layoutY="20.0" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="Untitled Tab 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="tfTab1" layoutX="184.0" layoutY="84.0" prefHeight="25.0" prefWidth="220.0" />
<StackPane layoutX="27.0" layoutY="143.0" prefHeight="150.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<Label fx:id="lblTab1" text="Label" />
</children>
</StackPane>
</children>
</AnchorPane>
</content>
</Tab>
<Tab text="Untitled Tab 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="tfTab2" layoutX="220.0" layoutY="86.0" />
<StackPane layoutX="195.0" layoutY="140.0" prefHeight="150.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
<children>
<Label fx:id="lblTab2" text="Label" />
</children>
</StackPane>
</children>
</AnchorPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
You have to create a new .fxml file for the Tab then set the fx:controller=TabController then you can <fx:include source="myTab.fxml">
So you can include this 3 times then you will have three tabs with the sam controller.
So the main .fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.TabPane?>
<TabPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="TabPaneController">
<tabs>
<fx:include fx:id="FirstTab" source="CustomTab.fxml"/>
<fx:include fx:id="SecondTab" source="CustomTab.fxml"/>
<fx:include fx:id="ThirdTab" source="CustomTab.fxml"/>
</tabs>
</TabPane
>
And the child .fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<Tab xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="TabController">
<!--content-->
</Tab>
So now you have three tabs with the same Controller.
I try to display my "main.fxml" file into TapPane which connect with my other .fxml file. But unfortunately throw exception. What is wrong?
It is controller of tabs.fxml :
Tabs class
package View;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
public class Tabs implements Initializable {
#FXML
TabPane tabPane = null;
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
try {
tabPane.getTabs().addAll((Tab) FXMLLoader.load(this.getClass().getResource("main.fxml")));
} catch (IOException e) {
e.printStackTrace();
}
}
}
tabs.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane
maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="View.Tabs">
<children>
<TabPane
fx:id="tabPane"
prefHeight="400.0"
prefWidth="600.0"
tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Untitled Tab 1" >
<content>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
It is main.fxml. It contain a form and a button. I try to display this form in my "tab.fxml" through Tab class
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane
id="main"
maxHeight="-Infinity"
maxWidth="-Infinity"
minHeight="-Infinity"
minWidth="-Infinity"
prefHeight="400.0"
prefWidth="600.0"
xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="View.Chat">
<children>
<AnchorPane
minHeight="0.0"
minWidth="0.0"
prefHeight="180.0"
prefWidth="200.0" />
<TextArea
fx:id="text"
layoutX="14.0"
layoutY="317.0"
prefHeight="69.0"
prefWidth="435.0" />
<TextArea
fx:id="chatField"
editable="false"
layoutX="14.0"
layoutY="14.0"
prefHeight="289.0"
prefWidth="435.0" />
<Button
fx:id="send"
layoutX="486.0"
layoutY="334.0"
mnemonicParsing="false"
onAction="#sendMessage"
prefHeight="30.0
prefWidth="66.0"
text="Send" />
</children>
</AnchorPane>
But throw the exception:
javafx.scene.layout.AnchorPane cannot be cast to
javafx.scene.control.Tab
/E:/JavaFXTest/out/production/JavaFXTest/View/tabs.fxml
at View.Tabs.initialize(Tabs.java:21)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2193)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2069)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2830)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2809)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2795)
The error message tells you the problem:
javafx.scene.layout.AnchorPane cannot be cast to
javafx.scene.control.Tab
The root element of your tab.fxml file is an AnchorPane, but you are trying to treat it as a Tab:
(Tab) FXMLLoader.load(this.getClass().getResource("main.fxml"))
You can either change the FXML file so the root element is a tab:
<Tab xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="View.Chat">
<AnchorPane> id="main" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" >
<children>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
<TextArea fx:id="text" layoutX="14.0" layoutY="317.0" prefHeight="69.0" prefWidth="435.0" />
<TextArea fx:id="chatField" editable="false" layoutX="14.0" layoutY="14.0" prefHeight="289.0" prefWidth="435.0" />
<Button fx:id="send" layoutX="486.0" layoutY="334.0" mnemonicParsing="false" onAction="#sendMessage" prefHeight="30.0" prefWidth="66.0" text="Send" />
</children>
</AnchorPane>
</Tab>
or
you can create the Tab in Java code in the controller:
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
try {
Tab tab = new Tab();
tabPane.getTabs().add(tab);
tab.setContent((Node) FXMLLoader.load(this.getClass().getResource("main.fxml")));
} catch (IOException e) {
e.printStackTrace();
}
}