11 Replies Latest reply: Oct 8, 2012 9:46 AM by biochemistry43 RSS

    multiple windows application in same scene. what is the best way?

    biochemistry43
      hi im inexpert in Java FX... sorry

      I need make an application with any dialogs or windows in the same scene but i dont have idea

      In swing use internal frames... how is in java fx?

      the application will be controlled for tooglebuttons. The dialogs hide/appear depending to the action in the tooglebuttons
      sorry for the very bad english.

      thanks

      Edited by: 962720 on 02-oct-2012 12:16
        • 1. Re: multiple windows application in same scene. what is the best way?
          jsmith
          Quick summary is, you probably want to vote for the following jira: http://javafx-jira.kenai.com/browse/RT-14039 "Docking Framework" or be prepared to do a bit of work if you want to build such a thing yourself.

          You could try some of the samples and links below and see if it does something similar to what you want, and, if it they don't, come back with a bit more explanation of what you would need and how you intend to use it.

          https://gist.github.com/1437374 "panels for controlling youtube video playing"
          http://mihosoft.eu/?p=314 "draggable internal nodes"
          3rd Party Docking Framework Implementation Strategy - AND Why Web GUIs Won. "docking framework discussion and very basic prototype"
          https://blogs.oracle.com/geertjan/entry/a_docking_framework_module_system "A Docking Framework & Module System for JavaFX" (using NetBeans)
          http://stackoverflow.com/questions/12681355/is-it-possible-to-create-several-subwindows-in-the-main-window-area-in-javafx2/12683139
          • 2. multiple windows application in same scene. what is the best way?
            biochemistry43
            I'm a little frustrated ... I do not know if FX is not understanding or not understanding much English.

            I need something similar to Swing JTabbedPane but instead of the panels are in the same component, I want to come from other FXML files separate from the overall structure.

            Hope it's enough explanation.

            Again thank you very much and sorry for any mistakes in English.

            The following is a picture of my general structure (prototype)



            regards

            Edited by: 962720 on 05-oct-2012 8:48
            • 3. Re: multiple windows application in same scene. what is the best way?
              jsmith
              Oh I see what you want now. Thanks for the image link - it is helpful.

              You don't need a complete docking framework to do this and can easily accomplish it using the existing JavaFX API.

              Sorry for the confusion with the links I placed in my previous post.

              I'm sure someone will post a sample code implementation soon.
              • 4. Re: multiple windows application in same scene. what is the best way?
                biochemistry43
                That's great! ...
                You are very kind ... Thank you.

                You think someone responds soon?? It's for some work and I'm stuck

                regards
                • 5. Re: multiple windows application in same scene. what is the best way?
                  James_D
                  I can try and put some code together... give me 30 mins.
                  • 6. Re: multiple windows application in same scene. what is the best way?
                    James_D
                    This is kind of rough, and not necessarily the very best way, but I hope it gives you a starting point.

                    Here's the main FXML file:

                    TabPane.fxml
                    <?xml version="1.0" encoding="UTF-8"?>
                    
                    <?import javafx.scene.layout.BorderPane?>
                    <?import javafx.scene.layout.HBox?>
                    <?import javafx.scene.control.Button?>
                    
                    <BorderPane xmlns:fx="http://javafx.com/fxml" fx:id="root" fx:controller="TabPaneController">
                         <top>
                           <HBox>
                             <Button text="Pane 1" onAction="#switchPane" fx:id="button1" />
                             <Button text="Pane 2" onAction="#switchPane" fx:id="button2" />
                             <Button text="Pane 3" onAction="#switchPane" fx:id="button3" />
                             <Button text="Pane 4" onAction="#switchPane" fx:id="button4" />
                           </HBox>
                         </top>
                    </BorderPane>
                    Here's the controller:

                    TabPaneController.java
                    import java.io.IOException;
                    
                    import javafx.event.ActionEvent;
                    import javafx.fxml.FXML;
                    import javafx.fxml.FXMLLoader;
                    import javafx.scene.Parent;
                    import javafx.scene.control.Button;
                    import javafx.scene.layout.BorderPane;
                    
                    
                    public class TabPaneController {
                         @FXML private Button button1 ;
                         @FXML private Button button2 ;
                         @FXML private Button button3 ;
                         @FXML private Button button4 ;
                         
                         @FXML private BorderPane root ;
                         
                         private Parent pane1;
                         private Parent pane2 ;
                         private Parent pane3 ;
                         private Parent pane4 ;
                         
                         
                         public void initialize() throws IOException {
                              pane1 = FXMLLoader.load(getClass().getResource("Pane1.fxml"));
                              pane2 = FXMLLoader.load(getClass().getResource("Pane2.fxml"));
                              pane3 = FXMLLoader.load(getClass().getResource("Pane3.fxml"));
                              pane4 = FXMLLoader.load(getClass().getResource("Pane4.fxml"));
                         }
                         
                         public void switchPane(ActionEvent event) {
                              Object eventSource = event.getSource();
                              if (eventSource == button1) {
                                   root.setCenter(pane1);
                              } else if (eventSource == button2) {
                                   root.setCenter(pane2);
                              } else if (eventSource == button3) {
                                   root.setCenter(pane3);
                              } else if (eventSource == button4) {
                                   root.setCenter(pane4);
                              }
                         }
                    }
                    There are obviously slightly nicer ways to do this, with less repetitive code, but it shows the idea.

                    Here is the FXML for Pane1:

                    Pane1.fxml
                    <?xml version="1.0" encoding="UTF-8"?>
                    
                    <?import javafx.scene.layout.BorderPane?>
                    <?import javafx.scene.text.Text?>
                    
                    <BorderPane xmlns:fx="http://javafx.com/fxml">
                         <center><Text text="This is pane 1"/></center>
                    </BorderPane>
                    The other fxml files, Pane2.fxml, Pane3.fxml, and Pane4.fxml look almost identical in this example (just change the text of the Text element), but would be different in the real world.

                    And then the Application for completeness:

                    TabPaneFXML.java:
                    import javafx.application.Application;
                    import javafx.fxml.FXMLLoader;
                    import javafx.scene.Parent;
                    import javafx.scene.Scene;
                    import javafx.stage.Stage;
                    
                    public class TabPaneFXML extends Application {
                    
                         @Override
                         public void start(Stage primaryStage) throws Exception {
                              Parent root = FXMLLoader.load(getClass().getResource("TabPane.fxml"));
                              Scene scene = new Scene(root, 600, 400);
                              primaryStage.setScene(scene);
                              primaryStage.sizeToScene();
                              primaryStage.show();
                         }
                    
                         public static void main(String[] args) {
                              launch(args);
                         }
                    }
                    I hope this is useful...
                    • 7. Re: multiple windows application in same scene. what is the best way?
                      James_D
                      Here's possibly a better refactoring: move the inclusion of the panes from the controller to the fxml.

                      Updated TabPane.fxml:
                      <?xml version="1.0" encoding="UTF-8"?>
                      
                      <?import javafx.scene.layout.BorderPane?>
                      <?import javafx.scene.layout.HBox?>
                      <?import javafx.scene.control.Button?>
                      
                      <BorderPane xmlns:fx="http://javafx.com/fxml" fx:id="root" fx:controller="TabPaneController">
                           <top>
                             <HBox>
                               <Button text="Pane 1" onAction="#switchPane" fx:id="button1" />
                               <Button text="Pane 2" onAction="#switchPane" fx:id="button2" />
                               <Button text="Pane 3" onAction="#switchPane" fx:id="button3" />
                               <Button text="Pane 4" onAction="#switchPane" fx:id="button4" />
                             </HBox>
                           </top>
                           <fx:define>
                                <fx:include source="Pane1.fxml" fx:id="pane1"/>
                           </fx:define>
                           <fx:define>
                                <fx:include source="Pane2.fxml" fx:id="pane2"/>
                           </fx:define>
                           <fx:define>
                                <fx:include source="Pane3.fxml" fx:id="pane3"/>
                           </fx:define>
                           <fx:define>
                                <fx:include source="Pane4.fxml" fx:id="pane4"/>
                           </fx:define>
                      </BorderPane>
                      and then a simplified controller

                      TabPaneController.java:
                      import javafx.event.ActionEvent;
                      import javafx.fxml.FXML;
                      import javafx.scene.Parent;
                      import javafx.scene.control.Button;
                      import javafx.scene.layout.BorderPane;
                      
                      public class TabPaneController {
                           @FXML private Button button1 ;
                           @FXML private Button button2 ;
                           @FXML private Button button3 ;
                           @FXML private Button button4 ;
                           
                           @FXML private BorderPane root ;
                           
                           @FXML private Parent pane1;
                           @FXML private Parent pane2 ;
                           @FXML private Parent pane3 ;
                           @FXML private Parent pane4 ;
                           
                           public void switchPane(ActionEvent event) {
                                Object eventSource = event.getSource();
                                if (eventSource == button1) {
                                     root.setCenter(pane1);
                                } else if (eventSource == button2) {
                                     root.setCenter(pane2);
                                } else if (eventSource == button3) {
                                     root.setCenter(pane3);
                                } else if (eventSource == button4) {
                                     root.setCenter(pane4);
                                }
                           }
                      }
                      Other files as in previous post.
                      • 8. Re: multiple windows application in same scene. what is the best way?
                        biochemistry43
                        my friend ... thank you very much.

                        I'll try what you've posted to see what I can do.

                        Best regards and deep appreciation

                        blessings

                        Javier Vazquez

                        Edited by: biochemistry43 on 08-oct-2012 6:37
                        • 9. Re: multiple windows application in same scene. what is the best way?
                          biochemistry43
                          I do not understand ... I tried the code you gave me and I get this exception:

                          java.lang.ClassNotFoundException: TabPaneController

                          think you can help me?
                          • 10. Re: multiple windows application in same scene. what is the best way?
                            James_D
                            I just put everything in a default package (which is not the correct way to do it in real life).

                            If you have these in a package, you need to modify the fx:controller attribute in your main fxml file to use the fully-qualified name of the controller.

                            So if it looks like this:
                            package com.example.myapp ;
                            public class TabPaneController {
                            ...
                            }
                            modify the fxml to
                            ...
                            <BorderPane xmlns:fx="http://javafx.com/fxml" fx:id="root" fx:controller="com.example.myapp.TabPaneController">
                            ...
                            • 11. Re: multiple windows application in same scene. what is the best way?
                              biochemistry43
                              I've done what you said and I've given the "Clean and Build Project" and that was enough to fix the error ...

                              Thank you very much for the help.

                              This worked perfectly and is exactly what I needed. If I have any questions I will take the liberty of asking volverles.

                              Thanks again

                              Edited by: biochemistry43 on 08-oct-2012 7:45