home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Micrsoft / SAMPLES / VJ6SAMPL.EXE / BindPubs / CaptionedEdit.java < prev    next >
Text File  |  1998-02-24  |  2KB  |  64 lines

  1.  
  2.  
  3. /*  Copyright (C) Microsoft Corporation, 1996-1998.  All rights reserved.
  4.  
  5.   This source code is intended only as a supplement to Microsoft
  6.   Visual J++ 6.0.  See this product's on-line documentation for detailed   
  7. information regarding Microsoft code samples.
  8.  
  9. */
  10.  
  11. import wfc.ui.*;
  12. import wfc.core.*;
  13. /**CaptionedEdit is an edit box with a label underneath
  14.  */
  15. class CaptionedEdit extends Edit{
  16.     private String caption;
  17.     private Label label = new Label();
  18.     /**default constructor sets up callbacks for label management
  19.      */
  20.     public CaptionedEdit(){
  21.         //define a size for the label
  22.         label.setSize(60, 20);
  23.         Label label = this.label;
  24.         label.setAlignment(HorizontalAlignment.CENTER);
  25.         //set the label to move with the edit
  26.         addOnMove(new EventHandler(this.moveCaption));
  27.         //set the label's size by the edit's
  28.         addOnResize(new EventHandler(this.resizeCaption));
  29.     }
  30.     /**public void setCaption(String caption) sets the caption of the edit box
  31.      */
  32.     public void setCaption(String caption){
  33.         //retrieve the current location of the edit
  34.         Point location = getLocation();
  35.         //place a label with the caption below it -- if there's a parent, add the label to it
  36.         label.setText(caption);
  37.         label.setLocation(location.x, location.y + getSize().y + 2);
  38.         Control parent = getParent();
  39.         if(parent!=null)
  40.             parent.add(label);
  41.         setLocation(getLocation());
  42.     }
  43.     /**public String getCaption() returns the edit's caption
  44.      */
  45.     public String getCaption(){
  46.         return caption;
  47.     }
  48.     /**private void moveCaption(Object sender, Event event) moves the caption with the edit
  49.      */
  50.     private void moveCaption(Object sender, Event event) {
  51.         Point location = getLocation();
  52.         label.setLocation(location.x, location.y + getSize().y + 2);
  53.         Control parent = getParent();
  54.         //if the control got a parent since the last time it was located, add the caption to the parent
  55.         if(parent!=null && !(parent.contains(label)))
  56.             parent.add(label);
  57.     }
  58.     /**private void resizeCaption(Object sender, Event event) ensures the label fits under the caption
  59.      */
  60.     private void resizeCaption(Object sender, Event event) {
  61.         label.setSize(getSize().x, label.getSize().y);
  62.     }
  63.     
  64. }