MultiLine ToolTip Example : Tooltip « Swing JFC « Java





MultiLine ToolTip Example

MultiLine ToolTip Example
   
// Example from http://www.crionics.com/products/opensource/faq/swing_ex/SwingExamples.html
/* (swing1.1beta3) */


import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalToolTipUI;

/**
 * @version 1.0 11/09/98
 */
public class MultiLineToolTipExample extends JFrame {
  public MultiLineToolTipExample() {
    super("Multi-Line ToolTip Example");
    JButton button = new JButton("Hello, world") {
      public JToolTip createToolTip() {
        MultiLineToolTip tip = new MultiLineToolTip();
        tip.setComponent(this);
        return tip;
      }
    };
    button.setToolTipText("Hello\nworld");
    getContentPane().add(button);
  }

  public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {}
  
    MultiLineToolTipExample f = new MultiLineToolTipExample();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.setSize(300, 100);
    f.show();
  }
}

class MultiLineToolTip extends JToolTip {
  public MultiLineToolTip() {
    setUI(new MultiLineToolTipUI());
  }
}

class MultiLineToolTipUI extends MetalToolTipUI {
  private String[] strs;

  private int maxWidth = 0;

  public void paint(Graphics g, JComponent c) {
    FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(
        g.getFont());
    Dimension size = c.getSize();
    g.setColor(c.getBackground());
    g.fillRect(0, 0, size.width, size.height);
    g.setColor(c.getForeground());
    if (strs != null) {
      for (int i = 0; i < strs.length; i++) {
        g.drawString(strs[i], 3, (metrics.getHeight()) * (i + 1));
      }
    }
  }

  public Dimension getPreferredSize(JComponent c) {
    FontMetrics metrics = Toolkit.getDefaultToolkit().getFontMetrics(
        c.getFont());
    String tipText = ((JToolTip) c).getTipText();
    if (tipText == null) {
      tipText = "";
    }
    BufferedReader br = new BufferedReader(new StringReader(tipText));
    String line;
    int maxWidth = 0;
    Vector v = new Vector();
    try {
      while ((line = br.readLine()) != null) {
        int width = SwingUtilities.computeStringWidth(metrics, line);
        maxWidth = (maxWidth < width) ? width : maxWidth;
        v.addElement(line);
      }
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    int lines = v.size();
    if (lines < 1) {
      strs = null;
      lines = 1;
    } else {
      strs = new String[lines];
      int i = 0;
      for (Enumeration e = v.elements(); e.hasMoreElements(); i++) {
        strs[i] = (String) e.nextElement();
      }
    }
    int height = metrics.getHeight() * lines;
    this.maxWidth = maxWidth;
    return new Dimension(maxWidth + 6, height + 4);
  }
}


           
         
    
    
  










Related examples in the same category

1.Tooltip SampleTooltip Sample
2.Html tooltipsHtml tooltips
3.Working with Tooltip TextWorking with Tooltip Text
4.Colored ToolTip ExampleColored ToolTip Example
5.Disable / enable application tool tips
6.Making Tool Tips Remain Visible
7.Enabling and Disabling Tool Tips
8.Making Tool Tips Appear Immediately
9.Use the default tool tip location
10.Make a tool tips appear immediately
11.Showing an Image in a Tool Tip with HTML
12.Setting a Tool Tip
13.Setting the Location of a Tool Tip
14.By default, the lines are left justified. Center the lines.
15.Set the location of the tool tip such that its nw corner coincides with the bottom center of the button
16.Showing an Image in a ToolTip
17.Modify the behaviour of the default JToolTip
18.Italicize the second line
19.Changing ToolTip background color for Swing Applications
20.ToolTip Location ExampleToolTip Location Example
21.Set a tool tip for swing component
22.JToolTip With Icon