Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 21, 1997

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

Question:
I am attempting to implement an applet that needs multiple frames (windows) that all belong or work under a single frame — sort of like an MDI application. However, each of the windows will differ from each other in size, shape, components, etc. An example might be Symantec'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't work within a single parent frame, and other application windows can get intertwined within my applet window. I also don't want to make each child window modal.

Answer:
If I understand your problem, I think you need to create your own class of "frames" extending the FramedPanel class defined below. Unlike frames, panels are confined within their parent applet or frame. You can dynamically create, resize, move and destroy panels, and because panels are containers, 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, so PanFrame needs to be an extension of Applet.) I realize I got a little carried away with handling mouse events. I suppose you would need to add your own mouse handlers inside your extension of FramedPanel to intercept mouse events and make them 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(Color.black);
      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);
        p.show();
         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 (e.id == 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();
   }

}
 
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date