1

I am building an application that uses a JDesktopPane and JInternalFrames which at the moment works like this

desktopPane.java

// this program handles the main desktop pane

public class desktopPane extends JFrame {

    static JDesktopPane mainDesktopPane;
    static JMenuItem itemChildProgram;

    public desktopPane() {
        mainSetup();
        mainBuild();
        mainActions();
    }

    public static void main(String[] args) {
        desktopPane mainFrame = new desktopPane();
        mainFrame.setVisible(true);
    }

    public void mainSetup() {
        // set title and size and close operation etc.
    }

    public void mainBuild() {
        mainDesktopPane      = new JDesktopPane();

        JMenuBar mainMenuBar = new JMenuBar();
        JMenu menuRun        = new JMenu("Run");
        itemChildProgram     = new JMenuItem("Child Program");

        add(mainDesktopPane);

        setJMenuBar(mainMenuBar);
        mainMenuBar.add(menuRun);
        menuRun.add(itemChildProgram);
    }

    public void mainActions() {
        actionChildProgram execChildProgram = new actionChildProgram();
        itemChildProgram.addActionListener(execChildProgram);
    }

    public class actionChildProgram implements ActionListener {
        public void actionPerformed(ActionEvent execChildProgram) {
            childProgram classChildProgram = new childProgram(mainDesktopPane);
        }
    }

}

So that program just builds my main screen and it has a button which calls my other program

childProgram.java

public class childProgram {

    public childProgram(JDesktopPane mainDesktopPane) {
        // database connection and data retrieval goes here

        childProgramGUI child = new childProgramGUI();
        child.setVisible(true);
        mainDesktopPane.add(child);
    }

}

So this program just prepares some data and a bunch of other stuff and then calls the gui where the data is visible. The gui file for now is terribly simple

childProgramGUI.java

public class childProgramGUI extends JInternalFrame {
    // some gui design goes here
}

This works fine when calling childProgram.java from the JDesktopPane in desktopPane.java but how can I call childProgram.java as a standalone program without adding it to the mainDesktopPane as a child.

I only need the childProgram to run outside of the desktopPane when a condition is met.

If I make the following alterations to the code

  • change childProgramGUI.java to JFrame instead of JInternalFrame
  • remove the line in childProgram.java: mainDesktopPane.add(child);

Then it works as I intend but I can't change the extends of the childProgramGUI.java to JFrame with just code (that I know of)

UPDATE

So far the only solution I have for this is to just duplicate my gui programs so now I have

childProgramGUI.java

public class childProgramGUI extends JInternalFrame {
    // some gui design goes here
}

As well as the standalone one with all the exact same code except it's a JFrame

childProgramGUI_Standalone.java

public class childProgramGUI_Standalone extends JFrame {
    // the exact same gui design goes here
}

And then put a condition on my mainDesktopPane.add(child). I'm not happy with it but it works to run the program in and out of it's JDesktopPane

TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
  • Possible duplicate of [View JInternalFrames without JDesktopPanes](http://stackoverflow.com/questions/20041622/view-jinternalframes-without-jdesktoppanes) – Arnaud Feb 12 '16 at 14:45
  • A related example is examined [here](http://stackoverflow.com/q/4089717/230513). – trashgod Feb 12 '16 at 16:13
  • 2
    Possible lesson learned: Build your GUI with `JPanels`, and don't use `extends JFrame` or `extends JInternalFrame` (unless there is a *really* good reason to do so, but there hardly ever is...) – Marco13 Feb 12 '16 at 16:45
  • @Marco13 how would you build a GUI with just jpanels? Don't they need to be added to a jframe? – TheLovelySausage Feb 12 '16 at 23:14
  • The key point is that your "main" GUI components should usually be panels. These can then be easily added, either to the content pane of a `JFrame` or to an `JInternalFrame`. (As the name suggests: These are just the *frames* (!) around the actual content). Usually, there is no reason to extend either of them. You usually do not override methods of these classes. Extending them is mainly done in (questionable) introducory tutorials, or, rarely, in small examples. Also see http://stackoverflow.com/questions/1143923/why-shouldnt-you-extend-jframe-and-other-components .... – Marco13 Feb 13 '16 at 01:08

0 Answers0