javafx form not appear [duplicate] - java

This question already has answers here:
javafx : javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.BorderPane
(3 answers)
Closed 5 years ago.
When I run the javafx programme not showing javafx windows form that I created.console shows like below picture..
here is my code:
package Employee;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class main extends Application {
private Stage primaryStage;
private BorderPane mainLayout;
#Override
public void start(Stage primaryStage) throws IOException {
this.primaryStage=primaryStage;
this.primaryStage.setTitle("My windows");
showMainView();
}
private void showMainView() throws IOException
{
FXMLLoader loader=new FXMLLoader();
loader.setLocation(main.class.getResource("view/Mainview.fxml"));
mainLayout=loader.load();
Scene scene=new Scene(mainLayout);
primaryStage.setScene(scene);
}
public static void main(String[] args) {
launch(args);
}
}
and after execute it console shows like this
enter image description here

Two things:
Your mainLayout in your FXML is not a BorderPane
You are missing a call to show:
primaryStage.show();

is the root of your view/Mainview.fxml a BorderPane? If not you will get a ClassCastException like the one you are seeing. Even if it is, you may need to write that line as an explicit cast like this: mainLayout=(BorderPane)loader.load() Also, you will need to make a call to primaryStage.show() after you set the scene, otherwise your window won't show up.

Related

JavaFX Stage not visible

I'm trying to write the simplest code to start learning how JavaFX works. Here is my code:
package app;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class mainClass extends Application{
Stage primaryStage;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
primaryStage = stage;
StackPane layout = new StackPane();
Scene scene = new Scene(layout, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
It should just show an empty window. My compilator gives no errors, but nothing shows up, even if I can see my program running in my dock. How is this possible? I'm sure I'm missing some stupid simple thing, but I cannot figure out what it is.

this statement in my javafx program is causing a nullpointerexception and I don't know why [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.stage.*;
public class Drawing extends Application{
private BorderPane mainPane;
private HBox statusBox;
private Text statusText;
public void start(Stage primaryStage) {
statusText = new Text("OFF");
statusBox = new HBox(statusText);
statusBox.setAlignment(Pos.CENTER);
mainPane.setTop(statusBox); //nullPointerException here
Scene scene = new Scene(mainPane, 1000, 1000);
primaryStage.setTitle("Draw Something");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String args[]) {
launch(args);
}
}
I want to create a BorderPane and set the top portion for some text. I created an HBox to put inside. I don't know why this error occurs.
Your mainPane.setTop(statusBox) has not been instantiated yet. Instantiate mainPane first by mainPane = new BorderPane() before using it.

Change the layout on button click [duplicate]

This question already exists:
I Can't Change The Layout
Closed 6 years ago.
I want to change the page when I click the button. I can do it as soon as the program open, but I want to change the center of the Main class' BorderPane when I clicked the button. (I have a number of buttons and I want to go to different controllers with them).
I want to change the center of the Main Controller without changing it. I tried so many things but couldn't do that. On action method addClicked is working but doesn't change the center controller to another controller.
I have Main.class, MainController.java and AddTaskController.java.
Main.class:
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
public class Main extends Application {
private Stage window;
private BorderPane layout;
#Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
try {
MainController con1 = showMainView();
setLayout(con1.setLay("addssss.fxml"));
} catch(Exception e) {
e.printStackTrace();
}
}
public void setLayout(HBox lay){
layout.setCenter(lay);
}
private MainController showMainView() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(Main.class.getResource("MainInterface.fxml"));
layout = loader.load();
Scene scene = new Scene(layout);
window.setScene(scene);
window.show();
return loader.getController();
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java:
package application;
import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.HBox;
public class MainController {
#FXML
void addClicked(ActionEvent e) throws IOException{
}
public HBox setLay(String kaynak) throws IOException{
HBox layout;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(AddTaskController.class.getResource(kaynak));
layout = loader.load();
return layout;
}
}
And AddTaskController.java (There is no code):
package application;
public class AddTaskController {
}
What exactly is your problem? What have you tried to solve it?
If you want to change the layout of your buttons, I see two possible solutions.
You remove the old layout from your scene (or where ever it was added). Then you add the buttons to a new layout and add the new layout to your scene. (Though I am not sure if this works or not, since the SceneGraph only allows components to be added once).
You have multiple layouts prepared with different buttons and in every layout there is one button calling the "addClicked" method. So there are different Buttons in different Layouts behaving the same way. (You can also prepare a new Layout with new Buttons on the fly, like the first solution does)

JavaFX multiple line

When i tried to compile code it have bug. I am trying to resolve problem one hour and i don't know this bug. Do you know how to fix it? Thanks!
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public abstract class Mainn extends Application
{
public static void main(String[] args){launch(args);
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass.getResource("/fxml/StackPaneWindow.fxml"));
StackPane stackPane = loader.load();
Scene scene = new Scene (stackPane);
primaryStage.setScene(scene);
primaryStage.setTitle("bang");
primaryStage.show();
}
}
}
The first problem I see with this is that the method start(Stage) is inside your main function, which isn't allowed java. Close your main function before defining another function.

Sending notification with javafx without stage

You can send a desktop notification with JavaFx like this (requires jdk 8u20 or later):
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import org.controlsfx.control.Notifications;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
// Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Button notifyButton = new Button("Notify");
notifyButton.setOnAction(e -> {
Notifications.create().title("Test").text("Test Notification!").showInformation();
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(notifyButton, 100, 50));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
But this way you have to create a main window (stage), is it possible to avoid this? I am looking for a way to send notification like using zenity in bash: most of the code is non-gui, but uses some gui elements for informing or interacting with user in a very basic fashion.
It looks like the ControlsFX notifications require a existing stage. You can create a hidden utility stage. Try something like this.
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.controlsfx.control.Notifications;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
}
public static void main(String[] args) {
new JFXPanel();
notifier("Good!", "It's working now!");
}
private static void notifier(String pTitle, String pMessage) {
Platform.runLater(() -> {
Stage owner = new Stage(StageStyle.TRANSPARENT);
StackPane root = new StackPane();
root.setStyle("-fx-background-color: TRANSPARENT");
Scene scene = new Scene(root, 1, 1);
scene.setFill(Color.TRANSPARENT);
owner.setScene(scene);
owner.setWidth(1);
owner.setHeight(1);
owner.toBack();
owner.show();
Notifications.create().title(pTitle).text(pMessage).showInformation();
}
);
}
}
new JFXPanel() initializes the JavaFX thread without having to extend Application. You have to call this before any calls to Platform.runLater() otherwise you will get a thread exception. You only need to call it once for the whole application though. Honestly it is probably better to create your own notification stage and display it directly. Create a stage like above and put your own contents. You can probably reuse some of the styling from the ControlsFX source.

Categories

Resources