Multi-View Applications

 

class TableFrame extends JInternalFrame {
   private static int openFrameCount = 0;
   public TableFrame(MyTableModel model) {
        super("Document #" + (++openFrameCount),
              true, //resizable
              true, //closable
              true, //maximizable
              true);//iconifiable

        TableGUI view = new TableGUI(model);
        setContentPane(view);

        //...Create the GUI and put it in the window...

        //...Then set the window size or call pack...
        setSize(300,300);
   }
}

public class ParentAppFrame extends JFrame implements Runnable, ActionListener {

   private static final long serialVersionUID = 1L;
   private JDesktopPane desktop;
   private MyTableModel model;

   public ParentAppFrame() {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setDefaultLookAndFeelDecorated(true);
      desktop = new JDesktopPane(); //a specialized layered pane
      model = new MyTableModel();
        createFrame(); //create first "window"
        setContentPane(desktop);
        setJMenuBar(createMenuBar());

        //Make dragging a little faster but perhaps uglier.
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
   }

   public void actionPerformed(ActionEvent e) {
        if ("new".equals(e.getActionCommand())) { //new
            createFrame();
        } else { //quit
            System.exit(1);
        }
    }

    //Create a new internal frame.
    protected void createFrame() {
        TableFrame frame = new TableFrame(model);
        frame.setVisible(true); //necessary as of 1.3
        desktop.add(frame);

        try {
            frame.setSelected(true);
        } catch (java.beans.PropertyVetoException e) {}

    }


    protected JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        //Set up the lone menu.
        JMenu menu = new JMenu("Document");
        menu.setMnemonic(KeyEvent.VK_D);
        menuBar.add(menu);

        //Set up the first menu item.
        JMenuItem menuItem = new JMenuItem("New");
        menuItem.setMnemonic(KeyEvent.VK_N);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_N, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("new");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        //Set up the second menu item.
        menuItem = new JMenuItem("Quit");
        menuItem.setMnemonic(KeyEvent.VK_Q);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(
                KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        menuItem.setActionCommand("quit");
        menuItem.addActionListener(this);
        menu.add(menuItem);

        return menuBar;
    }

   public void run() {
      pack();
      setVisible(true);
   }

   public void display() {
      SwingUtilities.invokeLater(this);
   }

   public static void main(String[] args) {
      ParentAppFrame app = new ParentAppFrame();
      app.setTitle("Test AppFrame");
      app.display();
   }


}