Creating Multiple Frames (Windows) that Work Under a Single Frame

Creating Multiple Frames (Windows) that Work Under a Single Frame

I am attempting to implement an applet that needs multipleframes (windows) that all belong or work under a single frame ? sort oflike an MDI application. However, each of the windows will differfrom each other in size, shape, components, etc. An example might beSymantec’s Cafe IDE for Java with the different windows for the Project,Output and Editor, all working under a single frame.

Is there a way to do this in Java?

Currently, while I can easily create popup windows or frames, they don’twork within a single parent frame, and other application windows can getintertwined within my applet window. I also don’t want to make each childwindow modal.

If I understand your problem, I think you need to create your own class of”frames” extending the FramedPanel class defined below. Unlikeframes, panels are confined within their parent applet or frame. You candynamically create, resize, move and destroy panels, and because panels arecontainers, you can fill them up with components.

My PanFrame applet allows users to create, destroy, move,and reshape FramedPanels with the mouse. (Unfortunately,mouse events aren’t reported to frames on my Mac, soPanFrame needs to be an extension of Applet.)I realize I got a little carried away with handling mouse events. Isuppose you would need to add your own mouse handlers inside yourextension of FramedPanel to intercept mouse events and makethem do something useful in the context of your panels.

*/import java.awt.*;import java.applet.*;class FramedPanel extends Panel {   // I stole this code from the Java Tutorial!   //Ensure that no Component is placed on top of the frame.   //The inset values were determined by trial and error.   public Insets insets() {      return new Insets(4,4,5,5);   }   //Draw the frame at this Panel’s edges.   public void paint(Graphics g) {      Dimension d = size();      g.setColor(;      g.draw3DRect(0, 0, d.width – 1, d.height – 1, true);      g.draw3DRect(3,3, d.width – 7, d.height – 7, false);   }}public class PanFrame extends Applet {  private FramedPanel[] panels;  // all panels in the applet  private int MAX_PANELS = 10,              PANEL_COUNT = 0,              current = -1;  private boolean MOVING = false;  // = moving or resizing a panel  public boolean mouseDown(Event e, int x, int y) {     //setCursor(Frame.CROSSHAIR_CURSOR); a frame method 🙁     current = findPanel(x, y);  // = panel selected     if (current < 0)        addPanel(x, y);   // no panel selected, so create one     else if (e.clickCount >= 2)        removePanel(current);  // double click, so remove selected panel      return true;  }  // move or reshape selected panel  public boolean mouseDrag(Event e, int x, int y) {     if (current >= 0)        if (MOVING)           panels[current].move(x, y);  // move selected panel        else {           Point p = panels[current].location();           panels[current].reshape(p.x, p.y, x, y);  // reshape selected panel        }     // repaint();     return true;  }  // deselect selected panel  public boolean mouseUp(Event e, int x, int y) {        //setCursor(Frame.DEFAULT_CURSOR);        current = -1;        return true;  }  // create a new panel  private void addPanel(int x, int y) {     if ( PANEL_COUNT < MAX_PANELS) {        FramedPanel p = new FramedPanel();        p.move(x, y);        p.resize(100, 100);        add(p);;         panels[PANEL_COUNT++] = p;         repaint();      }   }   // remove selected panel   private void removePanel(int c) {      if (0 <= c && c < PANEL_COUNT) {         panels[c].hide();         PANEL_COUNT = PANEL_COUNT - 1;         // move panels in array down         for(int i = c; i <= PANEL_COUNT; i++)            panels[i + 1] = panels[i];         repaint();      }   }   private int findPanel(int x, int y) {      // panels[i] selected?      for(int i = 0; i < PANEL_COUNT; i++) {         Point p = panels[i].location();         Dimension d = panels[i].size();         if ((p.x <= x && x <= p.x + d.width)            && (p.y <= y && y <= p.y + d.height)) {            if ((x > p.x + d.width – 7) && (y > p.y + d.height – 7))               MOVING = false;   // near border            else               MOVING = true;  // near center            return i;         } // if      } // for      return -1;  // no panel selected    } // findPanel   public boolean handleEvent(Event e) {      if ( == Event.WINDOW_DESTROY) System.exit(0);      return super.handleEvent(e);   }   public void paint(Graphics g) {      for(int i = 0; i < PANEL_COUNT; i++)         panels[i].repaint();   }   public void init() {      //setTitle("Paneled Frame");      setLayout(new FlowLayout());      MAX_PANELS = 10;      panels = new FramedPanel[MAX_PANELS];      resize(400, 300);      show();   }} 

See also  Why ChatGPT Is So Important Today

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist