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