MultiLine ToolTip Example : Tooltip « Swing JFC « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Swing JFC » Tooltip 




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(300100);
    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(00, 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 = ((JToolTipc).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(Stringe.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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.