Sembako.java this Main class
public class Sembako extends Application {
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("rootScene.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Login");
stage.setResizable(false);
stage.show();
}
rootSceneController.java
#FXML
private void loginAction(ActionEvent event) throws IOException {
for (indrian16.oulook.id.co.sembako.entity.Login l : listLogin) {
if(username.getText().equals(l.getUsername())) {
if(password.getText().equals(l.getPassword())) {
Parent rootDashBoard = FXMLLoader.load(getClass().getResource("dashboard.fxml"));
Stage stage = new Stage();
Scene scene = new Scene(rootDashBoard);
stage.setScene(scene);
stage.setTitle("Sembako");
stage.show();
stage.close(); //how close Sembako scene :)
} else {
msgLogin.setText("Password Invalid");
}
} else {
msgLogin.setText("Username Invalid");
}
}
}
how close window Sembako scene in rootSceneController.java
sorry my bad english
I think you are asking how to close the previously-opened window. Assuming this is the window containing the username text field, you can do
username.getScene().getWindow().hide();
Related
I have a Java-application that looks like this:
#Override
public void start(Stage primaryStage) throws Exception {
root.getChildren().add(FXMLLoader.load(getClass().getResource("FXML.fxml")));
Scene scene = new Scene(root, 1680, 1050);
stage = primaryStage;
stage.setScene(scene);
stage.setMaximized(true);
stage.show();
//some code
stage.close();
//normal java code in another class
}
public static void main(boolean first) {
if (first) launch(new String[]{});
else{
//never prints hello, only hi
System.out.println("hi");
Platform.runLater(new Runnable() {
#Override public void run() {
System.out.println("hello");
stage.show();
}
});
}
}
My question is: What do I have to do to open fxml again from another class?
I tried to use Platform.runLater(), but it isn't working.
Thanks for any help.
I want to assign an existing handleModellAction method to a generated Hyperlink with the setOnAction method, but I don't know how to do this.
Here is my code example:-
#FXML
private void handleModellAction(ActionEvent event) throws IOException{
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getId();
stage.setTitle(model);
}
public void addNeuesModell(String bauart, String modelName){
modelHyperlink = new Hyperlink();
modelHyperlink.setId(modelName);
modelHyperlink.setText(modelName);
modelHyperlink.setOnAction(#handleModellAction);
}
Does somebody know how to do this?
Thanks a lot :)
You could try to call the setOnAction method on the modelHyperlink and pass as parameter an anonymous class as a handler, where in you could transfer the logic of your handleModellAction method. Below you can find an example:
Hyperlink link = new Hyperlink();
link.setText("http://example.com");
link.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
FXMLLoader load = new
FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = (Parent) load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
link = (Hyperlink) event.getTarget();
model = link.getId();
stage.setTitle(model);
}
});
instead of defining
<HyperLink fx:id="myLink" onAction="#handleModelAction"/>
you can use just:
<HyperLink fx:id="myLink"/>
Then in the code:
refactor your handleMethod like this:
private void handleModellAction() throws IOException {
FXMLLoader load = new FXMLLoader(getClass().getResource("InEX.fxml"));
Parent root = load.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
}
after that:
myLink.setOnAction(action -> {
try {
handleModellAction();
} catch (IOException e) {
e.printStackTrace();
}
});
Then you can use that handleModellAction() anywhere you want in any button, hyperlink and so on..
I am fairly new to JAVAFX so please bear with me. Any help is greatly appreciated. Firstly, i have a Main app that loads addItems.fxml and addItemsController.
public class UncookedApp extends Application {
private Stage stage;
#Override
public void start(Stage primaryStage) {
stage = primaryStage;
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/uncooked/view/addItems.fxml"));
AnchorPane root=loader.load();
addItemsController itemsCtrl=loader.getController();
itemsCtrl.setMainApp(this);
Scene scene=new Scene(root,1024,768);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e){
e.printStackTrace();
}
}
public Stage getStage() {
return stage;
}
public static void main(String[] args) {
launch(args);
}
}
This is part of the addItemsController:
private UncookedApp mainApp;
public UncookedApp getMainApp() {
return mainApp;
}
public void setMainApp(UncookedApp mainApp) {
this.mainApp = mainApp;
}
public void toMeat(ActionEvent event) {
Stage stage1=mainApp.getStage();
try {
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/uncooked/view/addMeat.fxml"));
Parent root=loader.load();
addMeatCtrl itemsCtrl=loader.getController();
// itemsCtrl.setMainApp(this);
Scene scene=new Scene(root,1024,768);
stage1.setScene(scene);
stage1.show();
} catch(Exception e){
e.printStackTrace();
}
}
The addItemsController will load a different page, addMeat. I got that to work but when I try to go back from addMeat to addItems with a button handle, it doesn't work. Is it something to do with retrieving the mainApp's stage? How can I solve this?
This is what I've tried.
public void handleBackFromMeat(ActionEvent event) {
try{
Stage stage=mainApp.getStage();
FXMLLoader loader=new FXMLLoader();
loader.setLocation(getClass().getResource("/uncooked/view/addItems.fxml"));
Parent root=loader.load();
addItemsController itemsCtrl=loader.getController();
Scene scene=new Scene(root,1024,768);
stage.setScene(scene);
stage.show();
}catch (Exception e){
e.printStackTrace();
}
}
I also had posted a similar question and then i went ahead with my way
I need to create a back button functionality in javafx?
What you can do as i did for my application was make two static list at the start of the application and then fill it with the Stage and the Fxml page name if you are using fxml and then have a central back button method on each page to move the previous page by iterating on the last item added to the list .
Two static lists in the main class
public static List<Stage>stageval = new ArrayList<Stage>();
public static List<String>fxmlval = new ArrayList<String>();
When ever i switch my fxml view i added the stage and the fxml file name to their respective lists.
fxmlval.add("Instance.fxml");
stage = (Stage)validate.getScene().getWindow();
stageval.add((Stage) delete.getScene().getWindow());
And a central back button method when ever the user clicked the back button i iterated to last item of both the lists and then after changing the stage deleted the values from the list .
public class BackButton {
public void back(String instance,Stage stage) throws IOException{
Parent parent = FXMLLoader.load(getClass().getResource(instance));
Scene scene = new Scene(parent);
scene.getStylesheets().add("/css/Style.css");
stage.setResizable(false);
stage.setScene(scene);
stage.show();
}
}
#FXML
public void back() throws IOException {
BackButton bb = new BackButton();
bb.back(fxmlval.get(fxmlval.size() - 1),
stageval.get(stageval.size() - 1));
fxmlval.remove(fxmlval.size() - 1);
stageval.remove(stageval.size() - 1);
}
Hope this helps its a work around but the solution works.
I have decided to update my application using JavaFXML, However I am having difficulties passing a scene into my controller. Here is my Controller;
public class MainApp extends Application {
#FXML
public Stage primaryStage;
#FXML
private AnchorPane rootLayout;
#FXML
private JobInterface jInterface;
#Override
public void start(Stage primaryStage) {
primaryStage = new Stage();
setPrimaryStage(primaryStage);
initRootLayout();
}
#FXML
public void initRootLayout(){
try {
primaryStage = getPrimaryStage();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("MainInterface.fxml"));
rootLayout = (AnchorPane) loader.load();
Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene);
primaryStage.show();
setPrimaryStage(primaryStage);
} catch (IOException e) {
e.printStackTrace();
}
}
#FXML
private void setJobLayout(){
primaryStage = getPrimaryStage();
jInterface = new JobInterface();
jInterface.initJobLayout();
primaryStage.setScene(jInterface.getScene());
}
public static void main(String[] args) {
launch(args);
}
public Stage getPrimaryStage() {
return primaryStage;
}
public void setPrimaryStage(Stage primaryStage) {
this.primaryStage = primaryStage;
}
}
Here is a method that is changing the scene using a different FXML file and attempting to pass the scene back to the controller;
public class JobInterface {
private AnchorPane rootLayout;
private Scene scene;
public void initJobLayout(){
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("JobInterface.fxml"));
rootLayout = (AnchorPane) loader.load();
scene = new Scene(rootLayout);
setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
}
The issue I'm having now is a NullPointerException on this line in the main app;
primaryStage.setScene(jInterface.getScene());
I am trying to pass a Stage between methods so that I can only update the Scene and not have to open a new Stage everytime a new method is called. Any ideas on where I am going wrong?
There should be no need to pass the stage or scene. Your Main will load the fxml resource which will have your fxml controller 'in charge' of your fxml file.
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("jobController.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
Your controller might look something like this (depending on your fxml design):
public class JobController {
#FXML
private Label label;
#FXML
private void handleButtonAction(ActionEvent event) {
label.setText("This is the controller speaking");
}
}
Now you can 'control' your stage (scene) from the controller. If you are going to create another class which also has to update the scene, pass a reference to the controller to it e.g. from the controller:
TimeClock timeClock = new TimeClock();
timeClock.init(this);
and then in TimeClock.java you have:
private final JobController controller;
public void init (JobController control){
this.controller = control;
}
Now you can access any public method in your controller from the TimeClock class. E.g.
controller.updateLabel("Time clock speaking!");
please help me.
I have following code.
public class Main extends Application {
private static Locale locale = new Locale("de", "DE");
private Scene scene;
public static Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
ResourceBundle bundle = ResourceBundle.getBundle("bundles.lang", locale);
fxmlLoader.setResources(bundle);
Parent root = fxmlLoader.load();
scene = new Scene(root);
stage.setMaximized(true);
stage.setScene(scene);
stage.show();
}
public void reload() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
fxmlLoader.setResources(ResourceBundle.getBundle("bundles.lang", locale));
Parent root = fxmlLoader.load();
scene = new Scene(root);
stage.setMaximized(true);
stage.setScene(scene);
stage.show();
}
}
in my Controller class
public class FXMLDocumentController implements Initializable {
#FXML
AnchorPane root;
#FXML
private void handleChinese(final ActionEvent event) throws IOException {
Main.setLocale(new Locale("zh", "CN")); // change to english
//JavaFXApplication4.stage.close();
Main reload = new Main();
reload.reload();
}
#FXML
private void handleRussian(final ActionEvent event) throws IOException {
Main.setLocale(new Locale("de", "DE")); // change to english
Main reload = new Main();
reload.reload();
}
it works!, but when change language my window did not show correctly, it means stage.setMaximized(true) did not work, my window did not show as Maximized.
Why does stage.setMaximized(true) not work correctly?
Instead of creating a new scene , just set root to the previous scene.
public void reload() throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
Parent root = fxmlLoader.load();
stage.getScene().setRoot(root);
// Scene scene = new Scene(root);
// stage.setMaximized(true);
// stage.setScene(scene);
//
// stage.show();
}