Java FX error when i want to run the application [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
This is my Main Class
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("application/anwendung.fxml"));
primaryStage.setTitle("Benutzerverwaltung");
root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(new Scene(root));
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
This is my Controller class
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.input.InputEvent;
import javafx.stage.Stage;
class AnwendungsController {
#FXML
public Button closeButton;
#FXML
public void handleCloseButtonAction(ActionEvent event) {
Stage stage = (Stage) closeButton.getScene().getWindow();
stage.close();
}
}
And this is my fxml file
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="140.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AnwendungsController">
<children>
<Button fx:id="closeButton" layoutX="18.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" onMouseClicked="#onMouseClickedCancelBtn" prefHeight="26.0" prefWidth="77.0" text="Abbrechen" />
<Label layoutX="10.0" layoutY="27.0" prefHeight="27.0" prefWidth="342.0" text="Sie können das System nun verwenden" textAlignment="CENTER" textOverrun="CLIP">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
How can I fix it? I try everything from here JavaFX "Location is required." even though it is in the same package
UPDATE:
java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Unknown Source)

Two problems exist:
1)The File path
2)The Controller was not set properly
Recommendation:
If you are using SceneBuilder, when you want to see what your controller might look like, you can go to View -> Show Sample Controller Skeleton.
Solution
Main class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
System.out.println(getClass().getResource("anwendung.fxml").getPath());
//other solution
//FXMLLoader loader = new FXMLLoader(getClass().getResource("anwendung.fxml"));
//Parent root = loader.load();
//Keep in mind that you are calling a static method
Parent root = FXMLLoader.load(getClass().getResource("anwendung.fxml"));
primaryStage.setTitle("Benutzerverwaltung");
root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(new Scene(root));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
public class AnwendungsController {
#FXML
private Button closeButton;
#FXML
void handleCloseButtonAction(ActionEvent event) {
}
#FXML
void onMouseClickedCancelBtn(MouseEvent event) {
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="140.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AnwendungsController">
<children>
<Button fx:id="closeButton" layoutX="18.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" onMouseClicked="#onMouseClickedCancelBtn" prefHeight="26.0" prefWidth="77.0" text="Abbrechen" />
<Label layoutX="10.0" layoutY="27.0" prefHeight="27.0" prefWidth="342.0" text="Sie können das Systema nun verwenden" textAlignment="CENTER" textOverrun="CLIP">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
</children>
</AnchorPane>

Related

Javafx button does nothing

I'm creating a simple login GUI form using eclipse, JavaFX and scene builder. I've coded my program so that every time I click the login button, it would switch to another window but it doesn't work. i'm a beginner in java but we've been tasked to create a system in my class so I've only relied on youtube tutorials, any help would be appreciated!
this is my main code
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
Scene scene = new Scene(root);
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);
}
}
my login form
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Login {
#FXML
private Button button;
#FXML
private PasswordField password;
#FXML
private TextField username;
#FXML
private Label wrongLogin;
#FXML
public void Login(ActionEvent event) {
Stage primaryStage = new Stage();
if (username.getText().equals("admin") && password.getText().equals("admin")) {
try {
Parent root = FXMLLoader.load(getClass().getResource("BookRooms.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
my fxml file
<?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.AnchorPane?>
<AnchorPane prefHeight="345.0" prefWidth="545.0" style="-fx-background-color: #fff2cc;" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Login">
<children>
<AnchorPane layoutX="132.0" layoutY="62.0" prefHeight="221.0" prefWidth="279.0" style="-fx-background-color: #545454; -fx-border-radius: 10;">
<children>
<Label layoutX="34.0" layoutY="60.0" prefHeight="25.0" prefWidth="56.0" text="Username:" textFill="WHITE" />
<Label layoutX="34.0" layoutY="98.0" prefHeight="25.0" prefWidth="56.0" text="Password:" textFill="WHITE" />
<TextField layoutX="106.0" layoutY="60.0" promptText="Enter Username ID:" />
<TextField layoutX="106.0" layoutY="98.0" promptText="Enter Password" />
<Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />
</children></AnchorPane>
<Label layoutX="225.0" layoutY="36.0" text="HOTEL DEL LUNA" />
</children>
</AnchorPane>
Your button in fxml can't call Login() in its controller class because there is no onAction attribute in fxml button tag .
So ,in <Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" /> add onAction="#Login" inside Button Tag:
<Button alignment="CENTER" layoutX="106.0" layoutY="137.0" mnemonicParsing="false" onAction="#Login" prefHeight="25.0" prefWidth="73.0" style="-fx-background-color: #fff2cc;" text="LOGIN" />
And , naming a method in a class with exact the same name of that class is concidered a bad practice. Only constructors must match its name with class identifier

JavaFX Controller - how to get him

I have a problem with getting to the Controller.
I have a main template with two parts: Menu and Main Content
In two separate FXML files I have the Menu (MenuTemplate.fxml) and Main Content (Content1Template.fxml).
The loading of the Menu and Main Content proceeds correctly
The problem appears when Main Content wants to change the text after pressing the button.
Content1Utils.java has to set text in Content1Controller.java.
I guess I have to get to the controller. I just have a problem how to do it.
Link >> My Project in rar
Luncher.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import sample.main.MainController;
import java.io.File;
import java.net.URL;
public class Launcher extends Application {
private static MainController mainController;
#Override
public void start(Stage primaryStage) throws Exception{
URL mainTemplate = new File("src/sample/main/MainTemplate.fxml").toURI().toURL();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(mainTemplate);
Parent root = loader.load();
mainController = loader.getController();
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public MainController getMainController(){
return mainController;
}
}
MainController.java
package sample.main;
import com.jfoenix.controls.JFXDrawer;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class MainController {
#FXML private VBox rootBox;
#FXML private JFXDrawer menuBox;
#FXML private VBox contentBox;
#FXML
void initialize() throws IOException {
URL menuTemplate = new File("src/sample/menu/MenuTemplate.fxml").toURI().toURL();
VBox contentMenu = FXMLLoader.load(menuTemplate);
menuBox.setContent(contentMenu);
}
public void changeContentBox(VBox content){
contentBox.getChildren().setAll(content);
}
}
MenuController.java
package sample.menu;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import sample.Launcher;
import sample.main.MainController;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class MenuController {
#FXML private VBox menuRoot;
#FXML private Button menuButton1;
#FXML private Button menuButton2;
#FXML private Button menuButton3;
private MainController mainController;
#FXML
void initialize(){
}
public void clickButton1() throws IOException {
URL content1Template = new File("src/sample/content1/Content1Template.fxml").toURI().toURL();
VBox contentContent1 = FXMLLoader.load(content1Template);
mainController = new Launcher().getMainController();
mainController.changeContentBox(contentContent1);
}
}
Content1Controller.java
package sample.content1;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
public class Content1Controller {
#FXML private VBox content1root;
#FXML private Button content1Button;
#FXML private TextArea content1TextArea;
#FXML
void initialize(){
}
public void content1Button(){
Content1Utils contentUtils = new Content1Utils();
contentUtils.prepareText("XXX");
}
public void setTextInArea(String txt){
content1TextArea.setText(txt);
}
}
Content1Utils.java
package sample.content1;
public class Content1Utils {
public void prepareText(String txt){
String newTxt = txt + " - Test123";
new Content1Controller().setTextInArea(newTxt);
}
}
MainTemplate.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXDrawer?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="rootBox" prefHeight="442.0" prefWidth="665.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.main.MainController">
<children>
<HBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" VBox.vgrow="ALWAYS">
<children>
<JFXDrawer fx:id="menuBox" style="-fx-background-color: #ebd534;" />
<VBox fx:id="contentBox" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" style="-fx-background-color: #34b7eb;" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
MenuTemplate.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="menuRoot" prefHeight="442.0" prefWidth="100.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.menu.MenuController">
<children>
<Button fx:id="menuButton1" maxHeight="100.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#clickButton1" text="Button 1" VBox.vgrow="ALWAYS" />
<Button fx:id="menuButton2" maxHeight="100.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button 2" VBox.vgrow="ALWAYS" />
<Button fx:id="menuButton3" maxHeight="100.0" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Button3" VBox.vgrow="ALWAYS" />
</children>
</VBox>
Content1Template.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="content1root" alignment="CENTER" prefHeight="442.0" prefWidth="565.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.content1.Content1Controller">
<children>
<Button fx:id="content1Button" contentDisplay="CENTER" mnemonicParsing="false" onAction="#content1Button" prefHeight="48.0" prefWidth="400.0" text="Button" textAlignment="CENTER">
<VBox.margin>
<Insets />
</VBox.margin>
</Button>
<TextArea fx:id="content1TextArea" maxWidth="400.0" prefHeight="200.0" prefWidth="400.0">
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</TextArea>
</children>
</VBox>
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 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:394)
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$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
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.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
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:1771)
... 48 more
Caused by: java.lang.NullPointerException
at sample.content1.Content1Controller.setTextInArea(Content1Controller.java:25)
at sample.content1.Content1Utils.prepareText(Content1Utils.java:8)
at sample.content1.Content1Controller.content1Button(Content1Controller.java:21)
... 58 more
The problem is here:
public class Content1Utils {
public void prepareText(String txt){
String newTxt = txt + " - Test123";
new Content1Controller().setTextInArea(newTxt);
}
}
You are making a new controller instance that is not initialized from the FXML so it doesn't have a valid TextArea field. You don't want to have your utility class work this way.
Try:
public void content1Button(){
Content1Utils contentUtils = new Content1Utils(this);
contentUtils.prepareText("XXX");
}
Content1Utils.java:
package sample.content1;
public class Content1Utils {
private final Content1Controller controller;
public Content1utils(Content1Controller ctrl) {
this.controller = ctrl;
}
public void prepareText(String txt){
String newTxt = txt + " - Test123";
controller.setTextInArea(newTxt);
}
}

The controller 'MainController' has no event slot 'PassPhrase' [duplicate]

This question already has answers here:
JavaFx, Problems with #FXML
(2 answers)
Closed 5 years ago.
So im trying to create Action event, to change the text on my status label button, but i keep getting the error listed in the title "The controller 'MainController' has no event slot 'PassPhrase", Im new to this and not quite sure exactly what to do.
Also I want the text entered into my passPhrase field to be saved under a variable called pass, which i think i did correctly if anyone could double check? Thanks.
This is my mainController
package application;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import javafx.fxml.FXML;
public class MainController {
#FXML
private Label lblStatus;
#FXML
private TextField txtPassPhrase;
String pass = txtPassPhrase.getText();
public void PassPhrase (ActionEvent event) {
if (txtPassPhrase.getText().isEmpty()) {
lblStatus.setText("You must Enter in Characters");
} else {
lblStatus.setText("Your PassPhrase has been accepted.");
}
}
}
Here is my passPhrase 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.AnchorPane?>
<?import javafx.scene.text.Font?>
<fx:root prefHeight="150.0" prefWidth="500.0" type="AnchorPane"
xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.MainController">
<children>
<Button layoutX="210.0" layoutY="103.0" mnemonicParsing="false"
onAction="#PassPhrase" text="Submit">
<font>
<Font size="18.0" />
</font>
</Button>
<TextField fx:id="txtPassPhrase" layoutX="152.0" layoutY="56.0"
prefHeight="39.0" prefWidth="199.0" promptText="Enter in a PassPhrase">
<font>
<Font size="18.0" />
</font>
</TextField>
<Label fx:id="lblStatus" layoutX="80.0" layoutY="14.0"
prefHeight="17.0" prefWidth="423.0" text="Press submit once you enter in a
PassPhrase" textFill="#e05858">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</fx:root>
and my main class
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root =
FXMLLoader.load(getClass().getResource("/application/passPhrase.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);
}
}
Try changing your imports to `JavaFX' imports.
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.event.ActionEvent;

JavaFX FXML not responding to text input or Click (Events)

I have simple JavaFx project, with main class as eclipse JavaFx project default.
I have defined a controller , it looks like the skeleton generated by SceneBuilder, so everything is pretty much default.
When I run my application, In the UI I can't input data to the two textfields, or see the console output from my button event handlers.
A welcome.fxml file as shown below (deleted all other code to simplify)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.*?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mouseTransparent="true" prefHeight="720.0" prefWidth="1100.0" style="-fx-background-color: #000000;" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Welcome_Controller">
<children>
<Button alignment="CENTER" contentDisplay="CENTER" depthTest="ENABLE" graphicTextGap="10.0" layoutX="496.0" layoutY="360.0" mouseTransparent="true" onAction="#OnSignInClick" opacity="0.82" prefHeight="38.0" prefWidth="109.0" style="-fx-background-radius: 100;" text="Sign In" textAlignment="CENTER" />
<Button alignment="CENTER" contentDisplay="CENTER" layoutX="496.0" layoutY="409.0" mnemonicParsing="false" opacity="0.82" prefHeight="38.0" prefWidth="109.0" style="-fx-background-radius: 1000;" text="Sign Up" textAlignment="CENTER" />
<Text fill="WHITE" layoutX="899.0" layoutY="702.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Copyright 2015, Dawood and Irtiza." />
<TextField fx:id="tf_username" layoutX="427.0" layoutY="277.0" prefHeight="25.0" prefWidth="279.0" promptText="Enter new Username if you dont have an account " />
<TextField fx:id="tf_password" layoutX="427.0" layoutY="312.0" prefHeight="25.0" prefWidth="279.0" promptText="Enter new password if you dont have an account" />
<Label layoutX="355.0" layoutY="281.0" text="Username :" textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</Label>
<Label layoutX="355.0" layoutY="315.0" text="Password :" textFill="WHITE">
<font>
<Font size="14.0" />
</font>
</Label>
</children>
</Pane>
Welcome_controller.java looks like a default skeleton generated by scene builder, code below
package application;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class Welcome_Controller implements Initializable {
#FXML // fx:id="btn_xxx"
private Button btn_signin; // Value injected by FXMLLoader
#FXML
private Button btn_signup;
#FXML
private TextField tf_username;
#FXML
private TextField tf_password;
#Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources)
{
//assert btn_signin != null : "fx:id=\"btn_signin\" was not injected: check your FXML file 'welcome.fxml'.";
//assert btn_signup != null : "fx:id=\"btn_signup\" was not injected: check your FXML file 'welcome.fxml'.";
// initialize your logic here: all #FXML variables will have been injected
}
#FXML private void OnSignInClick(ActionEvent event) throws IOException
{
System.out.println("clicked");
}
#FXML
void OnSignupClick(ActionEvent event) {
System.out.println("clicked 2");
}
}
EDIT : This is the class with the main method.
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Pane root = (Pane) FXMLLoader.load(Main.class.getResource("welcome.fxml"));
Scene scene_1 = new Scene(root,1100,720);
//scene_1.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
//we can set the different scenes when it need to
primaryStage.setScene(scene_1);
primaryStage.setTitle("Translate Messenger");
primaryStage.show();
} catch(Exception e) {
System.out.println("main may exception");
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
When I run my application, In the UI I can't input data to the two textfields, or see the console output from my button event handlers.
Any help would be appreciated.
You have made the root pane transparent to the mouse with mouseTransparent="true". This means that any mouse clicks on the root pane, or any of its descendent nodes (i.e. the whole UI) will be ignored. Consequently you cannot click the buttons or put focus on the text fields using the mouse. (Notice though you can navigate the UI using the tab key, and use the space key to generate a button click when it has the focus.)
Remove mouseTransparent="true" from the pane and from the "Sign In" button and it will work fine.

How to send Message from FXML controller class to Main Application class

Hello stackoverflowers,
I have a question "I have an fxml file "CrawlerView.fxml" and its controller "CrawlerController.java" and a main Application class I want to send Message(database insert) from controller class to main Application class(because i cant do business logic in GUI/fxml controller class) . So my question is: Is it possible to send message from fxml controller class to main application class"
code is below
any help
thanx in advance`
Crawler.java
package app.model.main;
import java.io.IOException;
import java.sql.Statement;
import java.util.Stack;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Crawler extends Application {
private Stage MStage;
private BorderPane rootLayout;
#FXML Button SEARCH;
#FXML TextField URL;
static Conection conection;
static url_entries URLS;//self implemented classes
static word_count count;//self implemented classes
static Statement stmt;
//static public Stack DATABASE;
public static void test(){}
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage MStage)
{
try {
FXMLLoader RootLoader = new FXMLLoader();
FXMLLoader CrawlerLoader = new FXMLLoader();
RootLoader.setLocation(Crawler.class.getResource("view/RootView.fxml"));
CrawlerLoader.setLocation(Crawler.class.getResource("view/CrawlerView.fxml"));
rootLayout = (BorderPane) RootLoader.load();
AnchorPane CV = (AnchorPane) CrawlerLoader.load();
rootLayout.setCenter(CV);
// Show the scene containing the root layout.
Scene scene = new Scene(rootLayout);
MStage.setScene(scene);
MStage.show();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
CrawlerView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="628.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.model.main.CrawlerController">
<children>
<SplitPane dividerPositions="0.14424635332252836" layoutX="8.0" layoutY="-2.0" orientation="VERTICAL" prefHeight="628.0" prefWidth="600.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">
<children>
<Label layoutX="105.0" layoutY="14.0" prefHeight="52.0" prefWidth="388.0" text="Khurasani Web Crawler" textFill="#792323">
<font>
<Font name="Bauhaus 93" size="34.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
<children>
<TextField id="URL" fx:id="URL" alignment="CENTER" blendMode="DARKEN" layoutX="110.0" layoutY="25.0" prefHeight="34.0" prefWidth="359.0" text="Enter URL">
<font>
<Font name="Berlin Sans FB Demi Bold" size="17.0" />
</font>
<effect>
<InnerShadow choke="0.56" color="#722424" height="26.93" radius="12.965" width="26.93" />
</effect>
</TextField>
<Button id="SEARCH" fx:id="SEARCH" layoutX="247.0" layoutY="82.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="79.0" text="Search" textFill="#5e2929">
<font>
<Font name="Arial Black" size="15.0" />
</font>
</Button>
</children>
</AnchorPane>
</items>
</SplitPane>
CrawlerController
package app.model.main;
import java.net.URL;
import java.sql.Statement;
import java.util.ResourceBundle;
import java.util.Stack;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class CrawlerController implements Initializable
{
#FXML Button SEARCH;
#FXML TextField URL;
#Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert SEARCH != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
SEARCH.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event)
{
//How to send message to main application class
}
});}
}

Categories