I'm doing Internationalization on my JavaFX(11) application.
In resources.languages, I have multiple .properties files which are saved as UTF-8 character encoding.
But when I get with resourceBundle and pass them to fxml, they becomes squares like below.
Main.java
package application;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import jfxtras.styles.jmetro.JMetro;
import jfxtras.styles.jmetro.Style;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Locale locale = new Locale("my_MM");
ResourceBundle bundle = ResourceBundle.getBundle("resources.languages.langs", locale);
AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("Sample.fxml"), bundle);
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);
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="760.0" prefWidth="1200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/15.0.1" fx:controller="application.SampleController">
<children>
<VBox prefHeight="760.0" prefWidth="268.0" style="-fx-background-color: #3367D6;" styleClass="sidePanel">
<children>
<AnchorPane prefHeight="134.0" prefWidth="268.0">
<children>
<Label layoutX="41.0" layoutY="27.0" styleClass="title" text="Title" textFill="WHITE">
<font>
<Font size="33.0" />
</font>
</Label>
<Label layoutX="41.0" layoutY="68.0" styleClass="title" text="%interTitleDescription" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0" style="-fx-background-color: white;">
<children>
<Label alignment="TOP_LEFT" layoutX="36.0" layoutY="18.0" text="%interStudent" textFill="#3367d6">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="34.0" layoutY="17.0" text="%interTransactions" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interDonators" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interAdmins" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interSettings" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</AnchorPane>
langs_my_MM.properties
interTitleDescription = Management System
interStudent = ကျောင်းသားများ
interTransactions = အလှူငွေထုတ်မှတ်တမ်း
interDonators = အလှူရှင်များ
interAdmins = စီမံခန့်ခွဲသူများ
interSettings = Setting များ
Related
I'm new with JavaFX and made a Textfield in the Scene Builder. But I don't find a tutorial where someone explains how to get the input from the Textfield. My code I called my Textfield Texfield(fx:id) and left the On Action empty can you tell me what I have to fill in those two boxes and than in my code?
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));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import com.sun.javafx.scene.control.IntegerField;
import javafx.fxml.FXML;
import java.awt.*;
import java.awt.event.ActionEvent;
public class Controller {
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="72.0" prefHeight="20.0" prefWidth="457.0" text="GUESSING GAME">
<font>
<Font name="Arial Black" size="48.0" />
</font>
</Label>
<Label layoutX="72.0" layoutY="187.0" text="Your guess:">
<font>
<Font name="Arial" size="22.0" />
</font>
</Label>
<Label layoutX="395.0" layoutY="187.0" text="Attempts:">
<font>
<Font name="Arial" size="22.0" />
</font>
</Label>
<TextField fx:id="Textfield" layoutX="197.0" layoutY="188.0" prefHeight="25.0" prefWidth="66.0" />
<Label layoutX="496.0" layoutY="188.0" text="0">
<font>
<Font name="Arial" size="22.0" />
</font>
</Label>
</children>
</AnchorPane>
you need to add an id to the element in your fxml and then link it in the controller using #FXML the variable name must be the same name you set as the id.
And then you can access the text field or any UI element
I am dealing with JavaFX , I am trying to get my graphical composant from my FXML page into the java classes, but it always return null value
Page.fxml
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.BorderPane?>
<ScrollPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<content>
<BorderPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" fx:id="Mycontent">
<top>
<fx:include source="TopMenu.fxml" />
</top>
<center>
<fx:include source="Operation.fxml" />
</center>
<left>
<fx:include source="SideBar_Inhumer.fxml" />
</left>
<bottom>
<fx:include source="Footer.fxml" />
</bottom>
</BorderPane>
</content>
</ScrollPane>
Controller.java
package sample;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.BorderPane;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
#FXML
public BorderPane Mycontent;
#FXML
public void goToDemandeur(){
System.out.println(Mycontent);
//Mycontent.setCenter(FXMLLoader.load(getClass().getResource("Demandeur.fxml")));
}
#Override
public void initialize(URL location, ResourceBundle resources) {
}
}
My code always print "null"
if I try for example Mycontent.getCenter()it gives me this error
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
(which normal because Mycontent is null, but why it is null? )
PS: my method goToDemandeur() is called into an other fxml page SideBar_Inhumer.fxml after a click onMouseClicked="#goToDemandeur"
SideBar_Inhumer.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox layoutX="77.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Pane >
<children>
<Label alignment="CENTER" text="Inhumer" >
<font>
<Font size="24.0" />
</font>
<padding>
<Insets bottom="20.0" left="25.0" right="25.0" top="30.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #F08080;" onMouseClicked="#goToDemandeur">
<children>
<Label text="Demandeur" >
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</Pane>
<Pane style="-fx-background-color: #C6E2B9;">
<children>
<Label text="Defunt">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FBF6A5;">
<children>
<Label text="Emplacement">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #FFCC99;">
<children>
<Label text="Prestataire">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
<Pane style="-fx-background-color: #D8B46D;">
<children>
<Label text="Opération">
<font>
<Font size="15.0" />
</font>
<padding>
<Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
</padding>
</Label>
</children>
</Pane>
</children>
</VBox>
Main.java
package View;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("../View/Page.fxml"));
primaryStage.setTitle("Finalys");
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
Scene scene =new Scene(root, 600, 475);
primaryStage.setX(bounds.getMinX());
primaryStage.setY(bounds.getMinY());
primaryStage.setWidth(bounds.getWidth());
primaryStage.setHeight(bounds.getHeight());
primaryStage.setScene(scene);
primaryStage.show();
scene.getStylesheets().add(Main.class.getResource("bootstrap2.css").toExternalForm());
}
public static void main(String[] args) {
launch(args);
}
}
This question already has answers here:
What's the difference between fx:id and id: in JavaFX?
(4 answers)
Closed 4 years ago.
I have a JavaFX app that has the function to bring up a FileChooser. I just want to get the path of the file selected and set a Text Field to that path in the app. I need to re-use this function 4 times. Every time, I pass the TextField ID to the function it says it's null and then get the NullPointerException. I've tried several different solutions that said to create an initialize() for it, but that does not work either.
Multiple sources said to initialize the text field objects such as:
TextField global_dataset_1 = new TextField();
Then some sources said don't do that and to just refer to the .fxml IDs; which is what I thought originally to do... I tried the first one above by declaring it in the beginning and wrapping it into an #Override initialize(). Neither worked. I have the IDs set in the .fxml file.
I've also looked at: http://tutorials.jenkov.com/javafx/filechooser.html and https://examples.javacodegeeks.com/desktop-java/javafx/fxml/javafx-fxml-controller-example/ and https://github.com/mwilchek/Restaurant-Gift-Card-Lookup-App/blob/master/src/controller/NewAccountController.java
Below is what I have:
Main.java
package views;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
public static Stage primaryStage = new Stage();
#Override
public void start(Stage primaryStage) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
primaryStage.setTitle("Configuration Manager");
primaryStage.setScene(new Scene(root, 1100, 700));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FxmlController.java
package controller;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Accordion;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.FileChooser;
import java.io.File;
import static views.Main.primaryStage;
public class FxmlController {
#FXML
Accordion mainList;
#FXML
AnchorPane AnchorPane1;
//TextFields are all set to NULL ERROR
#FXML
TextField global_dataset_1;
#FXML
TextField global_dataset_2;
#FXML
TextField global_dataset_3;
#FXML
TextField global_dataset_4;
public void fileChooser1() {
fileChooser(global_dataset_1);
}
public void fileChooser2() {
fileChooser(global_dataset_2);
}
public void fileChooser3() {
fileChooser(global_dataset_3);
}
public void fileChooser4() {
fileChooser(global_dataset_4);
}
public FxmlController(){
}
// Error: For some reason won't pass TextField Object here...
public void fileChooser(TextField field) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Global Dataset");
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("CSV Files", "*.csv"),
new FileChooser.ExtensionFilter("All Files", "*.*"));
File selectedFile = fileChooser.showOpenDialog(primaryStage);
field.appendText(selectedFile.getPath());
}
Main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.String?>
<?import javafx.collections.FXCollections?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.SeparatorMenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox prefHeight="700.0" prefWidth="1100.0" style="-fx-background-color: white;" stylesheets="#bootstrap3.css" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.FxmlController">
<children>
<MenuBar style="-fx-background-color: #2176ff;" styleClass="context-menu" stylesheets="#bootstrap3.css" >
<menus>
<Menu mnemonicParsing="false" styleClass="menu-item" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<SplitPane dividerPositions="0.5, 0.5, 0.5" prefHeight="659.0" prefWidth="1100.0" >
<items>
<Accordion id="mainList" prefWidth="384.0">
<panes>
<TitledPane animated="false" styleClass="primary" stylesheets="#bootstrap3.css" text="Global Configurations">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="52.0" prefWidth="393.0" >
<children>
<Label layoutX="14.0" layoutY="15.0" prefHeight="41.0" prefWidth="93.0" text="Year:">
<font>
<Font size="18.0" />
</font>
</Label>
<ChoiceBox id="year" layoutX="405.0" layoutY="21.0" prefWidth="116.0" style="-fx-background-color: #2176ff;" styleClass="primary" stylesheets="#bootstrap3.css" value="2018">
<items>
<FXCollections fx:factory="observableArrayList">
<String fx:value="2018" />
<String fx:value="2019" />
<String fx:value="2020" />
<String fx:value="2021" />
<String fx:value="2022" />
</FXCollections>
</items>
</ChoiceBox>
<Label layoutX="14.0" layoutY="70.0" text="Run Name:">
<font>
<Font size="18.0" />
</font>
</Label>
<Separator layoutX="-3.0" layoutY="54.0" prefHeight="13.0" prefWidth="537.0" />
<TextField id="run_name" layoutX="298.0" layoutY="68.0" prefHeight="32.0" prefWidth="225.0" promptText="Run Name" style="-fx-background-color: #2176ff; -fx-text-fill: white;" styleClass="primary" stylesheets="#bootstrap3.css">
<font>
<Font size="14.0" />
</font>
</TextField>
<Separator layoutX="-2.0" layoutY="109.0" prefHeight="13.0" prefWidth="537.0" />
<Label layoutX="14.0" layoutY="122.0" prefHeight="35.0" prefWidth="140.0" text="Global Datasets" underline="true">
<font>
<Font name="Century" size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="160.0" text="Dataset 1: ">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="192.0" text="Dataset 2: ">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="227.0" text="Dataset 3: ">
<font>
<Font size="18.0" />
</font>
</Label>
<Label layoutX="14.0" layoutY="263.0" text="Dataset 4: ">
<font>
<Font size="18.0" />
</font>
</Label>
<TextField id="global_dataset_1" cache="true" layoutX="111.0" layoutY="158.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<Button layoutX="497.0" layoutY="157.0" mnemonicParsing="false" onMouseClicked="#fileChooser1" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
<TextField id="global_dataset_2" cache="true" layoutX="111.0" layoutY="191.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<TextField id="global_dataset_3" cache="true" layoutX="111.0" layoutY="227.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<TextField id="global_dataset_4" cache="true" layoutX="110.0" layoutY="263.0" prefWidth="379.0" promptText="Path to File" styleClass="primary" stylesheets="#bootstrap3.css" />
<Button layoutX="497.0" layoutY="192.0" mnemonicParsing="false" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
<Button layoutX="497.0" layoutY="226.0" mnemonicParsing="false" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
<Button layoutX="497.0" layoutY="262.0" mnemonicParsing="false" styleClass="primary" stylesheets="#bootstrap3.css" text="..." />
</children></AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" styleClass="primary" stylesheets="#bootstrap3.css" text="Annual 1">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
<TitledPane animated="false" styleClass="primary" stylesheets="#bootstrap3.css" text="Annual 2">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</TitledPane>
</panes>
</Accordion>
<AnchorPane id="AnchorPane1" prefHeight="200.0" prefWidth="200.0" visible="true" />
<AnchorPane id="AnchorPane2" prefHeight="200.0" prefWidth="200.0" visible="false" />
<AnchorPane id="AnchorPane3" prefHeight="200.0" prefWidth="200.0" visible="false" />
</items>
</SplitPane>
</children>
</VBox>
Any help would be greatly appreciated!
You have errors with how you've built your FXML file. Within your tags, you need to set the node's ID using fx:id but you only do id.
Your TextFields should be changed to:
<TextField fx:id="global_dataset_1" ...
A couple side notes:
You may want to use field.setText() instead of appendText() unless your intention is to allow them to select multiple files and list them all in the same text field.
Also, in your controller class, you should declare your nodes as private:
#FXML
private TextField global_dataset_1;
Lastly, sharing your primaryStage as a public field is not necessary (or recommended). Another way to open the FileChooser centered would be to get the current Window from any node declared in your controller:
File selectedFile = fileChooser.showOpenDialog(
global_dataset_1.getScene().getWindow());
So I looked through questions and found this one, which was similar to my problem, but didn't address my issues as I haven't initialized my date pickers in my java file.
I created a date picker. Its fx:id in the fxml file matches its name in the corresponding controller. For some reason, whether I manually type a value or select a date from the calendar, datePicker.getValue() ALWAYS returns null. Can anyone think of why this might be happening?
My code is exactly as the situation I've described here. I have a DatePicker called lowEndDate. I have not initialized it. Upon the click of a button, I want to get its value, but it returns null. Does anyone have any idea why this might be? Perhaps I've missed a step. Thank you.
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<SplitPane dividerPositions="0.22122571001494767" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="395.0" prefWidth="671.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="view.UserMainController">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="398.0" prefWidth="175.0">
<children>
<Button fx:id="logOut" layoutX="35.0" layoutY="261.0" mnemonicParsing="false" onAction="#logOutButtonClicked" text="Log Out">
<font>
<Font size="15.0" />
</font>
</Button>
<Label layoutX="52.0" layoutY="44.0" text="user:">
<font>
<Font size="19.0" />
</font>
</Label>
<Label fx:id="username" layoutX="16.0" layoutY="76.0" prefHeight="32.0" prefWidth="116.0" textAlignment="CENTER">
<font>
<Font size="25.0" />
</font>
</Label>
</children></AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="442.0" prefWidth="457.0">
<children>
<ListView fx:id="albumListView" layoutY="-2.0" prefHeight="335.0" prefWidth="292.0" />
<Button fx:id="deleteButton" layoutX="115.0" layoutY="348.0" mnemonicParsing="false" onAction="#deleteButtonClicked" prefHeight="31.0" prefWidth="68.0" text="Delete">
<font>
<Font size="15.0" />
</font>
</Button>
<AnchorPane layoutX="292.0" prefHeight="335.0" prefWidth="169.0">
<children>
<Label fx:id="albums" layoutX="73.0" layoutY="44.0" text="Albums">
<font>
<Font size="22.0" />
</font>
</Label>
<Label fx:id="addSuccess" layoutX="99.0" layoutY="212.0" text="ADD SUCCESSFUL" textFill="#2bee34" visible="false" />
<Label fx:id="addFail" layoutX="81.0" layoutY="212.0" text="ALBUM EXISTS. TRY AGAIN" textFill="#d01313" visible="false" />
<AnchorPane fx:id="createAlbumAnchor" layoutY="7.0" prefHeight="335.0" prefWidth="169.0" visible="false">
<children>
<Label layoutX="46.0" layoutY="39.0" text="Create Album">
<font>
<Font size="22.0" />
</font>
</Label>
<Button fx:id="albumCreate" layoutX="46.0" layoutY="229.0" mnemonicParsing="false" onAction="#OKButtonClicked" text="OK">
<font>
<Font size="15.0" />
</font>
</Button>
<Button fx:id="back" layoutX="121.0" layoutY="229.0" mnemonicParsing="false" onAction="#backButtonClicked" text="Back">
<font>
<Font size="15.0" />
</font>
</Button>
<TextField fx:id="newAlbumName" layoutX="38.0" layoutY="115.0" promptText="Album name" />
<Label fx:id="mustInputText" layoutX="64.0" layoutY="160.0" text="MUST INPUT TEXT" textFill="#cd1515" visible="false" />
</children>
</AnchorPane>
<AnchorPane fx:id="searchAnchor" layoutX="4.0" layoutY="7.0" prefHeight="321.0" prefWidth="212.0">
<children>
<Label layoutX="74.0" layoutY="35.0" text="Search">
<font>
<Font size="22.0" />
</font>
</Label>
<Label layoutX="76.0" layoutY="67.0" text="Date Range" />
<Label layoutX="91.0" layoutY="123.0" text="to" />
<Label layoutX="91.0" layoutY="185.0" text="Tags" />
<TextArea fx:id="tags" layoutX="24.0" layoutY="208.0" prefHeight="71.0" prefWidth="166.0" promptText="ex: healthy,happy,nyc" />
<Button fx:id="searchOK" layoutX="48.0" layoutY="282.0" mnemonicParsing="false" onAction="#searchOKButtonClicked" text="OK" />
<Button fx:id="searchBack" layoutX="118.0" layoutY="282.0" mnemonicParsing="false" onAction="#searchBackButtonClicked" text="Back" />
<DatePicker fx:id="highEndDate" layoutX="20.0" layoutY="92.0" promptText="dd/mm/yyyy" />**
<DatePicker fx:id="lowEndDate" layoutX="20.0" layoutY="148.0" promptText="dd/mm/yyyy" />
</children>
</AnchorPane>
</children>
</AnchorPane>
<Button fx:id="createButton" layoutX="21.0" layoutY="348.0" mnemonicParsing="false" onAction="#createButtonClicked" text="Create">
<font>
<Font size="15.0" />
</font>
</Button>
<Button fx:id="searchButton" layoutX="212.0" layoutY="348.0" mnemonicParsing="false" onAction="#searchButtonClicked" text="Search">
<font>
<Font size="15.0" />
</font>
</Button>
</children></AnchorPane>
</items>
</SplitPane>
Code:
public void searchOKButtonClicked(ActionEvent e){
boolean searchComplete = false;
User currentUser = LoginController.currentUser;
if((lowEndDate.getValue() == null) && (highEndDate.getValue() == null) && ((tags == null) || tags.getText().trim().isEmpty())){
Alert alert = new Alert(AlertType.ERROR);
alert.setContentText("Oops! At least one field is required to search.");
alert.show();
}
}
The trouble here: lowEndDate and highEndDate are ALWAYS null, regardless of input.
Sara, I've tested your fxml file using this simple project
Main
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("sample2.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.DatePicker;
public class Controller implements Initializable {
#FXML
DatePicker lowEndDate;
public void searchOKButtonClicked(ActionEvent e){
Alert alert = new Alert(AlertType.ERROR);
alert.setContentText("Oops! At least one field is required to search. " + " " + lowEndDate.getValue());
alert.show();
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
}
}
In your fxml, I've changed the naming in the two datepickers, I think that lowEndDate must be above highEndDate
<DatePicker fx:id="lowEndDate" layoutX="20.0" layoutY="92.0" promptText="dd/mm/yyyy" />
<DatePicker fx:id="highEndDate" layoutX="20.0" layoutY="148.0" promptText="dd/mm/yyyy" />
This is the result that I've obtained when I introduce a date
Maybe you have an error in another part of your controller but the datepickers seem to work fine.
Getting LoadExeption evertime i try to run this code.
the code ran fine the first time but after that i just keep getting this error.
Its just a simple login page with not much coding what so ever.
Main class below
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/Login.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); // tried changing this to string[0] didnt work
}
}
Main controller class below i wanted to add more sql stuff to this but i keep getting the same error.
In this i am just trying to change
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 TxtUsername;
#FXML
private TextField TxtPassword;
public void Login(ActionEvent event)
{
LblStatus.setText("Login sucsess");
}
}
login page
All of this was created using scene builder and youtube.
so i basically have no idea why i am getting this error.
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<AnchorPane prefHeight="400.0" prefWidth="500.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="application.MainController">
<!-- TODO Add Nodes -->
<children>
<Pane prefHeight="400.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<TitledPane animated="false" layoutX="0.0" layoutY="0.0" prefHeight="400.0000999999975" prefWidth="500.0" text="LOGIN FORM" textAlignment="CENTER" wrapText="false">
<content>
<AnchorPane id="Content" minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<TextField fx:id="TxtUsername" layoutX="227.0" layoutY="129.0" prefWidth="200.0" promptText="Enter Username" />
<Label layoutX="129.0" layoutY="129.0" prefWidth="98.0" text="USERNAME:">
<font>
<Font size="15.0" fx:id="x1" />
</font>
</Label>
<Label font="$x1" layoutX="129.0" layoutY="183.0" prefWidth="98.0" text="PASSWORD:" />
<Button font="$x1" layoutX="224.0" layoutY="225.0" mnemonicParsing="false" onAction="#Login" text="LOGIN" />
<PasswordField fx:id="TxtPassword" layoutX="224.0" layoutY="183.0" prefWidth="200.0" promptText="Enter Password" />
<Label id="Status" fx:id="LblStatus" layoutX="259.0" layoutY="69.0" text="Status">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</content>
<font>
<Font name="Arial Black" size="15.0" />
</font>
</TitledPane>
</children>
</Pane>
</children>
</AnchorPane>
You have the wrong imports in your controller. Replace
import java.awt.Label;
with
import javafx.scene.control.Label ;
etc...