HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"An AWT Tip Window Control"
Volume: 5 Issue: 2, p.40

	

Listing 1: 

import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 

public class TipWindow extends Window 
{ 
 private String message; 
 private int tipYCoord; 
 private final int slack = 4; 

 public TipWindow(Frame parent, String 
 message, int xCoord, int yCoord) 
 { 
  super(parent); 

  /* Set the message to display */ 
  this.message = message; 

  /* Set the tip window's background color */ 
  setBackground(new Color(255,255,220)); 

  /* Set the tip window's font */ 
  Font f = new 
  Font("Arial",Font.PLAIN,12); 
  setFont(f); 

  /* Retrieve the font's metrics */ 
  FontMetrics fm = getFontMetrics(f); 
  tipYCoord = fm.getAscent() + slack/2; 

  /* Set the tip window's size based on 
     the font metrics */ 
  setSize(fm.stringWidth(message) + 
  slack,fm.getHeight() + slack); 

  /* Set the tip window's location */ 
  setLocation(xCoord,yCoord); 

  /* Display the tip window */ 
  setVisible(true); 
 } 

 public void paint(Graphics g) 
 { 
  /* Draw the tip window's border */ 
  g.drawRect(0,0,getSize().width - 
  1,getSize().height - 1); 

  /* Display the message */ 
  g.drawString(message,slack/2,tipYCoord); 
 } 
} 

Listing 2: 

class TipWindowMouseAdapter extends MouseAdapter 
{ 
 private TipWindow tp; 
 private Frame parent; 
 private ResourceBundle rb; 

 public TipWindowMouseAdapter(Frame par- 
 ent, String propFile) 
 { 
  this.parent = parent; 
  try {rb =  ResourceBundle.get- 
  Bundle(propFile);} 
  catch(MissingResourceException mre){} 
 } 

 public void mouseEntered(MouseEvent evt) 
 { 
  Component comp = evt.getComponent(); 
  Point p = comp.getLocationOnScreen(); 

  try 
  { 
   if (rb != null) 
    tp = new TipWindow(parent,rb.get- 
    String(comp.getName()),p.x + 
    evt.getX(),p.y + evt.getY() + 20); 
  } 
  catch(MissingResourceException mre){} 
 } 

 public void mouseExited(MouseEvent evt) 
 { 
  if (tp != null) 
  { 
   tp.dispose(); 
   tp = null; 
  } 
 } 
} 

Listing 3: 

import java.awt.event.*; 
import java.awt.*; 

public class TipWindowTest { 

 public static void main(String args[]) { 
  new TipWindowTestFrame(); 
 } 
} 

class TipWindowTestFrame extends Frame { 
 Label lblUserId = new Label("User  Id:"); 
 Label lblPassword = new Label("Password:"); 
 TextField tfUserid = new TextField(10); 
 TextField tfPassword = new TextField(10); 
 Button ok = new Button("OK"); 
 Button cancel = new Button("Cancel"); 

 TipWindowTestFrame() { 
  super(); 

  /* Set the layout */ 
  setLayout(new GridLayout(3,2,20,20)); 

  /* Add components */ 
  add(lblUserId); 
  add(tfUserid); 
  add(lblPassword); 
  add(tfPassword); 
  add(ok); 
  add(cancel); 

  MouseListener ml = new TipWindow- 
  MouseAdapter(this,"helptext"); 

  /* Get the frame's components */ 
  Component[] comps = getComponents(); 

  /* Cycle through adding mouse listen- 
   er and naming components */ 
  for (int i = 0; i < comps.length; i++) 
  { 
   comps[i].addMouseListener(ml); 
   comps[i].setName("" + i); 
  } 

  /* Add the window listener */ 
  addWindowListener(new WindowAdapter() { 
   public void windowClosing(WindowEvent 
                           evt) { 
    dispose(); System.exit(0);}}); 

  /* Size the frame */ 
  pack(); 

  /* Center the frame */ 
  Dimension screenDim = Toolkit.getDe- 
  faultToolkit().getScreenSize(); 
  Rectangle frameDim = getBounds(); 
  setLocation((screenDim.width - frameD- 
  im.width) / 2,(screenDim.height - 
  frameDim.height) / 2); 

  /* Show the frame */ 
  setVisible(true); 
 } 
} 



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.