Every time I ran JavaFX app I encounter the error below.
15:11:52.778 [JavaFX Application Thread] ERROR org.fhl.Manifesto - javafx.fxml.LoadException:
/C:/ManGenFX/target/classes/dataController.fxml
/C:/ManGenFX/target/classes/ManifestoMain.fxml:7
But it only happens if I add the initialize method on Main Controller.
public class MainController{
#FXML
GenerateController genCont;
#FXML
private Pane generatePane;
#FXML
AnchorPane mainFx;
private Node source;
//Other Button, TextFields and Labels declaration
#FXML
private void browseFile(ActionEvent x) {
//Browse file definition
}
#FXML
private void savePath(ActionEvent x) {
//Save file definition
}
//Problem with this method
#FXML
public void initialize() {
System.out.println("Initialize generate controller panel");
logger.info("Initialize generate controller panel");
genCont.init(this);
}
}
If I remove the initialize method on MainController class it will not throw any error but nothing happens either if I click on the generate button which is defined in GenerateController class below.
public class GenerateController {
#FXML
Button btnGenerate;
#FXML
Pane paneGen;
#FXML
public void generateMan(ActionEvent event) {
//generateMan body when Generate button is clicked
}
public void init(MainController mc) {
System.out.println("Init mainControl");
mainControl = mc;
}
public void validate() {
//Definition of method body here
}
}
This is the main Class
public class Man extends Application {
private Stage mainStage;
#Override
public void start(Stage primaryStage) {
this.mainStage = primaryStage;
this.mainStage.setTitle("Manifest");
try{
Parent root = FXMLLoader.load(getClass().getResource("/ManifestoMain.fxml"));
Scene sMain = new Scene(root);
mainStage.setScene(sMain);
mainStage.show();
}catch(IOException ioE) {
logger.error(ioE);
}
}
public static void main(String[] args) {
launch(args);
}
}
Also, I placed my dataController.fxml, ManifestoMain.fxml, and generateView.fxml under resources folder but it don't have any problem accessing the files. Appreciate help on this.
Related
I'm making an application with JavaFX and Scene Builder.
I have two controllers:Controller and FontController
I have Main class that launch my program and open Stage with first fontroller (Controller)
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("/card/card.fxml"));
Scene scene = new Scene(root, 1600, 600);
primaryStage.setScene(scene);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setMaximized(true);
primaryStage.setResizable(true);
primaryStage.getIcons().add(new Image("card/resources/logo-icon.png"));
primaryStage.show();
//adding resize and drag primary stage
ResizeHelper.addResizeListener(primaryStage);
//assign ALT+ENTER to maximize window
final KeyCombination kb = new KeyCodeCombination(KeyCode.ENTER, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (kb.match(event)) {
primaryStage.setMaximized(!primaryStage.isMaximized());
primaryStage.setResizable(true);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
There is a label and a button in Controller. When I click on the button a method is called and new window with second controller appears(FontController):
#FXML private Button btnFont;
#FXML private Label category1
#FXML
void changeFont(ActionEvent event) {
try {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("font.fxml"));
Parent rootFont = (Parent) fxmlLoader.load();
Stage stage = new Stage();
stage.setTitle("Select Font");
stage.setScene(new Scene(rootFont));
stage.show();
} catch (Exception e) {
System.out.println("can't load new window");
}
}
There is the button "OK" and label in FontCOntroller:
#FXML private Label fontLabel;
#FXML private Button btnFontOk;
Please tell me, what should I do to send and apply text from label in FontController when I click on the burtton "OK" to label in Controller?
SOLUTION FOUND:
I created class "Context" in my project directory to make all controllers communicate each other. You can add as many controllers as you want there.
Here it looks like:
package card;
public class Context {
private final static Context instance = new Context();
public static Context getInstance() {
return instance;
}
private Controller controller;
public void setController(Controller controller) {
this.controller=controller;
}
public Controller getController() {
return controller;
}
private FontController fontController;
public void setFontController(FontController fontController) {
this.fontController=fontController;
}
public FontController getFontController() {
return fontController;
}
}
Controller:
I created getters and setters (ALT + Insert in IDEA) for Label that I wanna change
public Label getCategory1() {
return category1;
}
public void setCategory1(Label category1) {
this.category1 = category1;
}
To get FontController variables and methods through Context class I placed line of code
//getting FontController through Context Class
FontController fontCont = Context.getInstance().getFontController();
I registered Controller in Context class through my initialize method (my class implements Initializable)
#FXML
public void initialize(URL location, ResourceBundle resources) {
//register Controller in Context Class
Context.getInstance().setController(this);
}
FontController:
to get Controller variables and methods I placed this code:
//getting Controller variables and methods through Context class
Controller cont = Context.getInstance().getController();
I also registered FontController in Context class through initialize method:
#Override
public void initialize(URL location, ResourceBundle resources) {
//register FontController in Context Class
Context.getInstance().setFontController(this);
}
Method that send text and text color from label in this FontController to label in Controller when I click on button:
#FXML
void applyFont(ActionEvent event) {
cont.getCategory1().setText(fontLabel.getText());
cont.getCategory1().setTextFill(fontLabel.getTextFill());
}
*By creating Context class you can make controllers communicate each other and create as many controllers as you want there. Controllers see variables and methods of each other
I am currently experimenting with JavaFX and SceneBuilder in eclipse to create and design my own program. In my first class "StartController" I am using a method called makeFadeIn. Basically, when I click a button another page loads up with a fade effect.
This is the code from StartController.java (notice makeFadeIn):
public class StartController {
#FXML
private AnchorPane rootPane;
private void makeFadeIn() {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
fadeTransition.setNode(rootPane);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
#FXML
private void loadSecondPage(ActionEvent event) throws IOException {
AnchorPane startPage = FXMLLoader.load(getClass().getResource("SecondController.fxml"));
rootPane.getChildren().setAll(startPage);
makeFadeIn();
}
Next, my other class loads up called "SecondController.java". In this class, I'm using the exact same method makeFadeIn (but I had to write it twice since it didn't let me run the program).
This is the code from SecondController.java:
public class SecondController {
#FXML
private AnchorPane rootPane;
private void makeFadeIn() {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
fadeTransition.setNode(rootPane);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
#FXML
private void loadFirstPage(ActionEvent event) throws IOException {
AnchorPane startPage = FXMLLoader.load(getClass().getResource("StartController.fxml"));
rootPane.getChildren().setAll(startPage);
}
My question is: can I somehow call the makeFadeIn method from the first class so I don't have to write it in my second class? I guess I need to inherit it in some way but I'm not sure how. I tried declaring it public instead of private but that did not help much.
You could move this functionality to a base class:
public class BaseController {
#FXML
private AnchorPane rootPane;
protected AnchorPane getRootPage() {
return rootPane;
}
protected void makeFadeIn() {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.millis(1000));
fadeTransition.setNode(rootPane);
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
}
And then have the other controllers extend it:
public class StartController extends BaseController {
#FXML
private void loadSecondPage(ActionEvent event) throws IOException {
AnchorPane startPage =
FXMLLoader.load(getClass().getResource("SecondController.fxml"));
getRootPane().getChildren().setAll(startPage);
makeFadeIn();
}
}
public class SecondController extends BaseController {
#FXML
private void loadFirstPage(ActionEvent event) throws IOException {
AnchorPane startPage =
FXMLLoader.load(getClass().getResource("StartController.fxml"));
getRootPane().getChildren().setAll(startPage);
}
}
I'm trying to change a text in a label, a text which was entered in a text field in a different scene.
I made 2 FXML files, the first one contains a textfield and "ok" button, the second one contains a label(with the text "Label").
My goal is to enter a text in the textfield, and when I press "ok"-> open the new scene and the label will change it's text to the text I entered in the text field.
I easily changed the label text when the label, the textfield and the ok button were all in the same scene, but when I do it while opening a new scene I fail...
After some research, I made a controller for each FXML file, and a "MainController" that will communicate between them.
This is my main class:
public class MainBanana extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("view/Welcome.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("MokaApp");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
}
public static void main(String[] args) {
launch(args);
}
}
my first scene controller:
public class WelcomeController {
#FXML
public TextField nameField;
#FXML
private Button okButton;
private MainController main;
#FXML
public void okClicked(ActionEvent event) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource("Person.fxml"));
okButton.getScene().setRoot(root);
System.out.println(nameField.getText());
main.setLblFromTf(nameField.getText());
}
public void init(MainController mainController) {
main=mainController;
}
}
second scene controller:
public class PersonController {
#FXML
public Label nameLabel;
private MainController main;
public void init(MainController mainController) {
main=mainController;
}
}
When I launch the program, The Welcome scene is opened, I enter a text to the textfield, but whenever I press the "ok" button, the scene changes to the second scene, but the label text stays the same(label) and I get a nullpointerexception error on this line(located in WelcomeController): main.setLblFromTf(nameField.getText());
Sorry for the long post..
You don't need the references to MainController all over the place.
The easiest way is:
public class PersonController {
#FXML
private Label nameLabel ;
public void setName(String name) {
nameLabel.setText(name);
}
}
Then you can do
public class WelcomeController {
#FXML
private TextField textField ;
#FXML
private Button okButton ;
#FXML
public void okClicked() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Person.fxml"));
Parent root = loader.load();
PersonController personController = loader.getController();
personController.setName(textField.getText());
okButton.getScene().setRoot(root);
}
}
I have created two java classes which have a static method which returns an AnchorPane after setting all properties of required labels and buttons.
For example:
class HomePageScene {
static AnchorPane getHomePageScene() {
//some code
//a button which is to be clicked to go to Login Page
//some code
}
}
class LoginPageScene {
static AnchorPane getLoginPageScene() {
//some code
}
}
And there is another class which has the main().
public class JavaFXEventDemo extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage myStage) {
myStage.setTitle("Program Windiw");
AnchorPane HomePane = HomePageScene.getHomePageScene();
AnchorPane LoginPane = LoginPageScene.getLoginPageScene();
Scene HomePage = new Scene(HomePane, 400.0, 300.0);
Scene LoginPage = new Scene(LoginPane, 400.0, 300.0);
myStage.setScene(HomePage);
myStage.show();
}
}
First I set the HomePage as the scene on the stage. In the screen there is a button, which when I click, I want the scene to the LoginPage. How do I do this?
All the three classes are in different files.I tried setting onAction() method, but in that, handle() method's return type is void, whereas I need to return an AnchorPane.
Bind a function for your button (onAction). In this function, call a function in your main class which will load the scene you want (void javafx.scene.Scene.setRoot(Parent value)) ?
EDIT:
What I meant :
public class JavaFXEventDemo extends Application {
private static Scene HomePage;
private static Scene LoginPage;
private static Stage myStage;
public static void main(String[] args) {
launch(args);
}
public void start(Stage myStage) {
JavaFXEventDemo.myStage = myStage;
myStage.setTitle("Program Windiw");
AnchorPane HomePane = HomePageScene.getHomePageScene();
AnchorPane LoginPane = LoginPageScene.getLoginPageScene();
HomePage = new Scene(HomePane, 400.0, 300.0);
LoginPage = new Scene(LoginPane, 400.0, 300.0);
loadHomePage();
myStage.show();
}
public static void loadHomePage(){
JavaFXEventDemo.myStage.setScene(HomePage);
}
public static void loadLoginPage(){
JavaFXEventDemo.myStage.setScene(LoginPage);
}
}
And just call loadXXXXPage() on your button.
I have an application in JavaFX FXML by using Model View Controller. I want to move some sliders with keyboard. To do this in the main class I put a KeyEvent to hear what happens on the keyboard. In the controller class FXMLDocumentController where are FXML variables. These variables are passed to a third class. Where there sliders are changed when you click any.
The problem is that when I pass the variables of the sliders in the third class are perfectly stored but when you run the code to modify sliders coming past the main class when clicked are FXML variables are null.
Here you have the code:
Main class:
public class OpenPilot extends Application {
Movements Movements = new Movements();
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent Key) {
Movements.GetKeys(Key);
}
});
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Controller:
public class FXMLDocumentController implements Initializable {
Movements Movements = new Movements();
#FXML public Slider SpeedSlider;
#FXML public Slider TurnsSlider;
#Override
public void initialize(URL url, ResourceBundle rb) {
//Send GUI Information
Movements.GetSliders(SpeedSlider, TurnsSlider);
}
}
Movements:
public class Movements {
//Define Data Variables
public double SpeedValue;
public double TurnsValue;
//Define GUI Variables
private Slider SpeedSlider;
private Slider TurnsSlider;
public void GetSliders(Slider SpeedSlider, Slider TurnsSlider) {
this.SpeedSlider = SpeedSlider;
this.TurnsSlider = TurnsSlider;
}
//Get Sliders
public void GetKeys(KeyEvent Key) {
System.out.println(Key.getCode());
System.out.println(SpeedSlider);
Platform.runLater(new Runnable() {
#Override public void run() {
TurnsSlider.setValue(10);
}
});
}
}
You use 2 different instances of the Movements class (Movements Movements = new Movements(); in OpenPilot and FXMLDocumentController). Since you don't use static fields / methods, you won't get any data from one instance to the other. You have to get the Movements object from the controller:
Controller:
public class FXMLDocumentController implements Initializable {
Movements movements = new Movements();
public Movements getMovements() {
return movements;
}
#FXML public Slider SpeedSlider;
#FXML public Slider TurnsSlider;
#Override
public void initialize(URL url, ResourceBundle rb) {
//Send GUI Information
movements.GetSliders(SpeedSlider, TurnsSlider);
}
}
Use a instance of the FXMLLoader instead of the static method to get the Movements field from the controller:
Main class:
public class OpenPilot extends Application {
Movements movements;
#Override
public void start(Stage stage) throws Exception {
FXMLLoader fxmlLoader = new FXMLLoader();
// use non-static load method here
Parent root = fxmlLoader.load(getClass().getResource("FXMLDocument.fxml").openStream());
// get movements from via controller
FXMLDocumentController controller = fxmlLoader.<FXMLDocumentController>getController();
movements = controller.getMovements();
Scene scene = new Scene(root);
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent Key) {
movements.GetKeys(Key);
}
});
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}