2

I have two JFrames (mainFrame.java & child.java) designed in NetBeans editor. I want to call second JFrame from the first frame as a child window. How can I call it?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Pankaj Kumar Thapa
  • 131
  • 1
  • 2
  • 6

3 Answers3

2
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Make the object of the first frame and then call it in the second frame.

firstframe fm = new firstframe();
fm.setVisible(true);
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
0

If I understand correctly you would like to have a main Window with child/children window inside of it. If this is so, check out the below code.

Screen Shot: enter image description here

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
/**
 * Sample demonstrates using JInternalFrame as a child window of a main
 * JFrame window  TicTacToe class extends JInternalFrame.
 * @author Kirk
 *
*/
public class BoardBuilder extends JFrame {

    private TicTacToe board;
    JDesktopPane desktopPane = new JDesktopPane();

    public BoardBuilder() {
        SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               board = new TicTacToe("TicToc Toe", true);
               board.setSize(250, 250);
               board.setClosable(true);
               board.setIconifiable(true);
               board.setDefaultCloseOperation(TicTacToe.DISPOSE_ON_CLOSE);
               if (!board.isVisible())
                   board.setVisible(true);

               desktopPane.add(board);
               add(desktopPane);
           }
       });

   }

   public static void main(String[] args) {
       BoardBuilder builderBoard = new BoardBuilder();
       builderBoard.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
       builderBoard.setSize(640, 480);
       builderBoard.setVisible(true);

    }

   }

to call the child window place this code block in a button Click event Listener

SwingUtilities.invokeLater(new Runnable() {
               public void run() {
               board = new TicTacToe("TicToc Toe", true);
               board.setSize(250, 250);
               board.setClosable(true);
               board.setIconifiable(true);
               board.setDefaultCloseOperation(TicTacToe.DISPOSE_ON_CLOSE);
               if (!board.isVisible())
                   board.setVisible(true);

               desktopPane.add(board);
               add(desktopPane);
           }
       });

For future info check out this link docs.oracle-InternalFrames