Search code examples
javaswingmainwindow

Make a mainWindow for JAVA/SWING application


When i started developping my application, I just developped Seprate JFrame frames and test them one by one. Now, i want to make a main window for my app. I read a lot, but until now, it's difficult for me to do this in java and swing. I tried this by creating a main window as an instance of JFrame, but i got errors that shows i can't show JFrame inside another JFrame.

public class MainWindow extends JFrame{
private JFrame frame1;
private JFrame frame2;


public MainWindow(){

frame1 = new JFrame();
frame2 = new JFrame();

setLayout(new BorderLayout());

add(frame1,BorderLayout.CENTER);
add(frame2,BorderLayout.NORTH);

pack(); 

}

}

Solution

  • This is one of the reasons why it is generally discouraged to extend directly from top level containers like JFrame, they lock you into a single use.

    You can't add window based components to other containers. You will have to separate each of your current frames into a more basic container, like JPanel, only then can you add them to another window.

    You may consider using a CardLayout or JTabbedPane or even a JDesktopPane or other layout manager to make your individual views available to your users depending on your needs.

    See...

    for some more ideas