HiEveryone,
I'm a beginner in Javafx and learning it to make app from someone tutorials, Now the problem is .. it's giving me an error as Expression expected? What's that mean ? Can anyone pls resolve my issue?..
Have a look into this SCREENSHOT.
here is my source codes:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.AnchorPane;
public class Main extends Application {
Stage primaryStage;
BorderPane rootLayout;
#Override
public void start(Stage primaryStage){
this.primaryStage=primaryStage;
primaryStage.setTitle("Address App");
initSample2Layout();
showSample();
}
public void initSample2Layout(){
try{
//here loading files from FXML..
FXMLLoader loader=new FXMLLoader();
loader.setLocation(Main.class.getResource("sample/Sample2.fxml"));
rootLayout=(BorderPane).loader.load();
Scene scene=new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
}catch(Exception e){e.printStackTrace();}
}
public void showSample(){
try{
//here we're loading files from FXML..
FXMLLoader loader2=new FXMLLoader();
loader2.setLocation(Main.class.getResource("sample/sample.fxml"));
AnchorPane ap=(AnchorPane).loader.load();
rootLayout.setCenter(ap);
}catch(Exception e){e.printStackTrace();}
}
public Stage getPrimaryStage(){
return primaryStage;
}
public static void main(String[] args) {
launch(args);
}
}
This is not valid syntax :
rootLayout=(BorderPane).loader.load();
If you want to cast to BorderPane, do :
rootLayout=(BorderPane)loader.load();
And the same goes for AnchorPane ap=(AnchorPane).loader.load();, which should be changes to AnchorPane ap=(AnchorPane)loader2.load();.
You have rootLayout=(BorderPane).loader.load(); with an extra . character before loader.
You probably want the following:
rootLayout=(BorderPane) loader.load();
Related
I'm trying to write the path in the main class to load a FXML file MenuFXML.fxml but I can't figure out the correct way to do so. The FXML file is in the package com.developer.fxml and the main class is in com.developer.
I've already tried the path /com/developer/fxml/MenuFXML.fxml and /fxml/MenuFXML.fxml but neither of them work.
package com.developer;
import com.developer.exit.ExitApplication;
import com.developer.util.css.AddCSS;
import java.io.IOException;
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 {
private Stage stage;
private Scene scene;
private final String title = "Stock Program 2.0";
private Parent root;
private final AddCSS addcss = new AddCSS();
#Override
public void start(Stage primaryStage) throws IOException {
stage = primaryStage;
stage.setTitle(title);
stage.setOnCloseRequest(e -> {
e.consume();
ExitApplication ep = new ExitApplication();
ep.closeProgram(stage);
});
//The part that I can't get to work
root = FXMLLoader.load(getClass().getResource("/fxml/MenuFXML.fxml"));
scene = new Scene(root);
addcss.setStylesheet(scene);
stage.setScene(scene);
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
So, I'm getting a StackOverflowError (Unwrapped from a InvocationTargetException), and I can't for the life of me figure out why.
package gui;
import errorhandling.ErrorLogBook;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GUIRouter extends Application{
public static void main(String[] args)
{
try
{
Application.launch(GUIRouter.class, (String[])null);
}
catch (Exception e)
{
System.out.println(e.getCause().toString());
}
}
#Override
public void start (Stage primaryStage)
{
try
{
StackPane page = (StackPane) FXMLLoader.load(GUIRouter.class.getResource("LoginScreen.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e)
{
ErrorLogBook.logReport(e);
}
}
It fails at the first try block with:
Application.launch(GUIRouter.class, (String[])null);
This is a FXML application that I'm building with NetBeans and JavaFX Scene Builder 2.0
Any ideas about why my code keeps crashing?
My guess would be that your .fxml in "LoginScreen.fxml" has GuiRouter defined as its controller, which it then creates through reflection. My guess is that during that creation it ends up calling start(..) creating a loop.
Try this:
package gui;
import errorhandling.ErrorLogBook;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class GUIRouter extends Application{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start (Stage primaryStage)
{
try
{
StackPane page = FXMLLoader.load(getClass().getResource("/gui/LoginScreen.fxml"));
Scene scene = new Scene(page);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e)
{
e.printStackTrace();
ErrorLogBook.logReport(e);
}
}
Without having the .fxml file and/or the stack trace, I (see no way but to ) guess the main cause of the exception in exactly the same way as Kiskae did.
However, why you do not replace the line
Application.launch(GUIRouter.class, (String[])null);
with the more simpler form
Application.launch(args);
Or at least
Application.launch(args, new String[0]);
To see if the exception still remains.
Thanks for all the responses!
However, after playing around with it, I have found the root of the problem.
I replaced
StackPane page = (StackPane) FXMLLoader.load(GUIRouter.class.getResource("LoginScreen.fxml"));
with
Parent root = FXMLLoader.load(getClass().getResource("LoginScreen.fxml"));
and now it's working just fine.
Context
I just started to learn JavaFX so I'm developing a program to discover it. The program I'm on needs a array, so I used ObservableList. But here is the problem, the items I add to the list are only accessible from the function itself.
Here is the situation :
package fr.cartes;
import fr.cartes.model.MapModel;
import fr.cartes.view.ListMapsController;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.ArrayList;
public class MainApp extends Application {
private Stage primaryStage;
private ObservableList<MapModel> maps = FXCollections.observableArrayList();
public static void main(String[] args){
launch(args);
}
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("/!\\ Explorateur /!\\");
this.primaryStage.setAlwaysOnTop(true);
initializationOfProgram();
}
public void initializationOfProgram(){
initializeListMaps();
maps.add(new MapModel("Les Etats-Unis d'Amérique", "EU.jpg"));
maps.add(new MapModel("La France d'aujourdh'ui", "FR.jpg"));
for(MapModel mapModel : maps){
System.out.println(mapModel.getMapName());
}
}
public void initializeListMaps(){
for(MapModel map : maps){
System.out.println(map.getMapName());
}
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/ListMaps.fxml"));
Scene scene = new Scene(loader.load());
primaryStage.setScene(scene);
primaryStage.show();
ListMapsController controller = new ListMapsController();
controller.setMainApp(this);
controller.loadMapsOnListView(maps);
}catch(IOException exception){exception.printStackTrace();}
}
public ObservableList<MapModel> getMapsLists(){
return maps;
}
}
For example, if I put a println() test in the initializationOfProgram(), I will have "Les Etats-Unis d'Amérique" and "La France d'aujourd'hui" written on the console, but if I do this println() test in initializeListMaps(), it won't display anything.
Am I missing something obvious here ? Can you help me ?
My stage contains a ToggleSwitch and two StackPanes which we'll call A and B. Both StackPanes are located in the same space within the parent StackPane. This means that if both A and B are visible and set to managed, they each take up half the allocated space like this:
I'm trying to hide StackPane B upon initalization so that StackPane A takes up the full space... and then when I click the toggle button, it should hide StackPane A and show StackPane B, making B take up the full space.
The initial hiding of StackPane B works fine, but I'm having trouble writing a change listener for the ToggleSwitch in my controller class. Here is my code, and where I'm having trouble:
application class:
public class showPanes extends Application {
Stage stage = new Stage();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws IOException {
StackPane root = (StackPane) FXMLLoader.load(Drag.class.getResource("twoPanes.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Pane Switcher");
scene.getStylesheets().add("styleMain.css");
stage.setScene(scene);
stage.show();
}
}
The answer found here uses Toggle Groups, and James' answer here uses Buttons. I can't find a solution for ToggleSwitch. I tried to adapt the first answer for use with ToggleSwitch but it's producing an error like:
and says
Cannot resolve method 'addListener(anonymous
javafx.beans.value.ChangeListener)'
How do I fix the listener?
controller class:
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import java.net.URL;
import java.util.ResourceBundle;
import org.controlsfx.control.ToggleSwitch;
public class compsController implements Initializable {
#FXML
private StackPane paneA, paneB;
#FXML
private ToggleSwitch toggleSwitch;
#Override
public void initialize(URL location, ResourceBundle resources) {
paneB.setManaged(false);
paneB.setVisible(false);
toggleSwitch.selectedProperty().addListener(new ChangeListener < ToggleSwitch > () {
#Override
public void changed(ObservableValue < ? extends ToggleSwitch > ov, ToggleSwitch t, ToggleSwitch t1) {
paneA.setManaged(false);
paneA.setVisible(false);
paneB.setManaged(true);
paneB.setVisible(true);
}
});
}
}
You could also use the Binding API of JavaFx like below,
#Override
public void initialize(URL location, ResourceBundle resources) {
paneA.managedProperty().bind(toggleSwitch.selectedProperty());
paneA.visibleProperty().bind(toggleSwitch.selectedProperty());
paneB.managedProperty().bind(toggleSwitch.selectedProperty().not());
paneB.visibleProperty().bind(toggleSwitch.selectedProperty().not());
}
}
Please have a look at my first JavaFX application code
package helloworld;
import javafx.application.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
public class HelloWorld2 extends Application
{
#Override
public void start(Stage stage)
{
stage.setTitle("Hello World");
Button btn = new Button();
btn.setText("Hello");
btn.setOnAction(new Action());
StackPane pane = new StackPane();
pane.getChildren().add(btn);
stage.setScene(new Scene(pane, 300,250));
stage.show();
}
private class Action implements EventHandler
{
#Override
public void handle(Event arg0)
{
System.out.println("JavaFX World");
}
}
public static void main(String[]args)
{
launch(args);
}
}
I am getting "Unsafe Operation" warning when I run this. Application runs without any exceptions. I believe unsafe thing is coming because I have to put keyword in some place, but I don't know where. Please help!
You should to specify the type of Event
private class Action implements EventHandler<ActionEvent>
{
#Override
public void handle(ActionEvent arg0)
{
System.out.println("JavaFX World");
}
}