I've been trying to figure out how to position my Stage Icon beside my Stage title. I was unable to formulate a solution for this. How do I get my Icon to be position beside my title?
stage.getIcons().add(new Image(""));
stage.setTitle("");
stage.setScene(scene);
stage.show();
I don't have 50 reputation to add a comment, but you don't really explain what the problem is with your current code. Those are the right methods, but maybe your file locations just aren't right? See the following example:
public class MainApp extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
Scene scene = new Scene(root);
stage.getIcons().add(new Image(this.getClass().getResourceAsStream("/images/so-icon.png")));
stage.setTitle("Icon Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related
I don't know how to close the main window in java fxml.
This part of code is in the class Main:
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Parent root2 = FXMLLoader.load(getClass().getResource("2ndwin.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
Scene scene2 = new Scene(root2);
secondaryStage.setScene(scene2);
}
public void show(){
secondaryStage.show();
}
I've got this. In controller i did this:
Main m = new Main();
m.show();`
but I still don't know how I can close primaryStage.
Please help me or tell me how I can create a new window and close the old window. I think this what I want to do - it isn't correct but I came up with it myself.
I do it by using an object in the stage you want to close to get the window
Window currentStage = OBJECTINSCENE.getScene().getWindow();
(Replace 'OBJECTINSCENE' with the id of anything in your scene). This gives you a reference to the stage you have open. Then call
currentStage.hide();
To close the stage when you want to.
So your show function would be as follows
public void show(){
Window currentStage = OBJECTINSCENE.getScene().getWindow();
secondaryStage.show();
currentStage.hide();
}
I can't find a clear example of how to write the Main class in the JavaFX application to use the fxml files created in Scene Builder, the part that loads and shows the stage and the scene. Can someone please show me one? I have created 7 different screens and controllers for my application, but the main class has me stumped. This is a different question than just creating the fxml markup in the main class.
Main class with main method:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
String fxmlResource = "MainWindow.fxml";
Parent panel;
panel = FXMLLoader.load(getClass().getResource(fxmlResource));
Scene scene = new Scene(panel);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
}
I have button that will popup new stage but primary stage wont disable it still can be click so how to disable the primary stage.
This is my code for stage and scene
private static final Stage stage = new Stage();
private static final Stage newstage = new Stage();
/**
*
*/
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().addAll(this.getClass().getResource("background.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
public void chgScene (String str) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(str));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public void addStage (String str) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource(str));
Scene scene = new Scene(root);
newstage.setScene(scene);
newstage.setResizable(false);
newstage.showAndWait();
}
public void clearStage () {
newstage.close();
}
If any mistaken so sorry since im just new to Java and i need to create a GUI for my project. Thanks for helping.
you need to add this:
newstage.initModality(Modality.APPLICATION_MODAL);
newstage.showAndWait();
see Modality
JavaFx has a various property that you can use, just it depends on the developer how he/she creative on his logic.
mainPane.setDisabled(true);
newPane.showAndWait();
Be advised that initModality() is broken and has been for a long time on the Linux platform runtime.
modal JavaFX stage initOwner prevents owner from resizing, bug?
Yes, that post directly refers to initOwner() but it's the call to initModality() that seems to trigger the bug. You can waste a lot of time playing with kludges that approximate the behavior you need but nothing seems to work just right.
I'm totally new in JavaFX... Until now, for my basic app I had all in one class - Main class. Now I want to extend my app and do this in proper way, so I would like to make controllers. As far as I know, I need to put package of my controller in root element in FXML file.
In my case it is AnchorPane element and I put something like this:
fx:controller="hr.controller.MainWindowController"
And in this controller should be all injections for ID's and methods, right? By injections I mean #FXML annotation. But how I connect this with my Main class? What should be in Main class? I know (I thinks so :P) that Main should extends Application class. So it contains this method:
#Override
public void start(Stage stage) {}
What's more and what I said in the beginning I also want be able to load/open new window after some action. Let's say that I have file mainWindow.fxml with above reference to the controller. The second file window2.fxml should be loaded after action on button in the 1st window.
Could you tell me, how should I achive this?
UPDATE!
Main class:
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.setResizable(false);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
MainWindowController:
public class MainWindowController{
#FXML
private Button btnSave;
#FXML
private MenuBar menuBar;
#FXML
private MenuItem exit;
#FXML
public void handleAction(ActionEvent event) throws IOException {
Stage stage = null;
Parent root = null;
if(event.getSource()==exit){
stage = (Stage) menuBar.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("window2.fxml"));
} else {
stage =(Stage) btnSave.getScene().getWindow();
root = FXMLLoader.load(getClass().getResource("mainWindow.fxml"));
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
Is this the proper way? It works how I want to, but I'm not sure if it should looks like this :)
I'm launching my JavaFX scene as:
Applicaiton.launch(Main.class);
form my java code.
How to return to my code after I'm done with JavaFX!
Example:
public String Method()
{
Stirng s = "MyName";
Application.launch(Main.class);//here I lauch JavaFX scene
s.trim();//how to come back here after I'm done with that scene.
}
It's not supposed to work that way. You start by extending javafx.application.Application, then the entry point is start(Stage), which you must override. That method is the place where you have to set up the Scene for your stage, build the layout with Node's (buttons, layout managers, text fields, checkboxes), and register event handlers. You can access startup parameters with getParameters().
The application can be launched by providing the usual main() that calls launch(), a facility of JavaFX. So the minimal JavaFX application looks like:
public class MyApp extends Application {
#Override
public void start(Stage primaryStage) {
VBox root = new VBox();
root.getChildren().setAll(new Label("Hello world!"));
Scene scene = new Scene(root, 600, 400);
// Add widgets and set up event handlers
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String... args) {
launch(args);
}
}
Platform.exit(); will do it click here for javafx2.2 documentation