Hiển thị ảnh trong tooltip với Java Swing


Trong bài viết này sẽ chỉ cho bạn cách làm cho nội dung trong tooltip sẽ hiển thị thêm ảnh minh họa. Ứng dụng đơn giản của chúng ta sẽ sử dụng tooltip cho một Button. Khi bạn di chuyển con trỏ chuột quanh nút thì nội dung tooltip sẽ hiển thị kèm theo...

Trong bài viết này sẽ chỉ cho bạn cách làm cho nội dung trong tooltip sẽ hiển thị thêm ảnh minh họa. Ứng dụng đơn giản của chúng ta sẽ sử dụng tooltip cho một Button. Khi bạn di chuyển con trỏ  chuột quanh nút thì nội dung tooltip sẽ hiển thị kèm theo một ảnh minh họa chẳng hạn. Với ứng dụng này thì bạn có thể dễ dàng gắn được một hình ảnh vào trong nội dung của tooltip được.

Trong chương trình này, chúng ta sử dụng các thẻ HTML để hiển thị tooltip với hình ảnh đi kèm. Các thẻ HTML này được hiểu bằng cách sử dụng class HTML từ gói thư việnjavax.swing.text.html.* của Java Swing. Thực ra chúng ta sẽ đưa thẻ vào luôn trong ứng dụng để cho ảnh được hiển thị cùng với nội dung của tooltip.

view plainprint?
  1. import javax.swing.*;  

  2. import javax.swing.text.html.*;  

  3. import java.awt.*;  

  4. import java.awt.event.*;  

  5. public class ImageToolTip{  

  6.   public static void main(String[] args){  

  7.     ToolTipManager.sharedInstance().setInitialDelay(0);     //làm tooltip hiển thị ngay lập tức  

  8.     ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);//làm tooltip không tự mất đi khi chưa dời chuột khỏi đối tượng  

  9.     JFrame frame = new JFrame("Image Tool Tip Frame");  

  10.     JComponent button = new JButton("Cut");  

  11.     String tooltiptext = "" + "This is the " + ""  

  12.  + " tool tip text." + "</html>";  

  13.     button.setToolTipText(tooltiptext);  

  14.     JPanel panel = new JPanel();  

  15.     panel.add(button);  

  16.     frame.add(panel, BorderLayout.CENTER);  

  17.     frame.setSize(400, 400);  

  18.     frame.setVisible(true);  

  19.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

  20.   }  

  21. }   

How to Use Tool Tips

Creating a tool tip for any JComponent object is easy. Use the setToolTipText method to set up a tool tip for the component. For example, to add tool tips to three buttons, you add only three lines of code:
b1.setToolTipText("Click this button to disable the middle button.");
b2.setToolTipText("This middle button does not react when you click it.");
b3.setToolTipText("Click this button to enable the middle button.");
When the user of the program pauses with the cursor over any of the program's buttons, the tool tip for the button comes up. You can see this by running the ButtonDemo example, which is explained in How to Use Buttons, Check Boxes, and Radio Buttons. Here is a picture of the tool tip that appears when the cursor pauses over the left button in the ButtonDemo example.

ButtonDemo showing a tool tip.For components such as tabbed panes that have multiple parts, it often makes sense to vary the tool tip text to reflect the part of the component under the cursor. For example, a tabbed pane might use this feature to explain what will happen when you click the tab under the cursor. When you implement a tabbed pane, you can specify the tab-specific tool tip text in an argument passed to the addTab or setToolTipTextAt method.

Even in components that have no API for setting part-specific tool tip text, you can generally do the job yourself. If the component supports renderers, then you can set the tool tip text on a custom renderer. The table and tree sections provide examples of tool tip text determined by a custom renderer. An alternative that works for all JComponents is creating a subclass of the component and overriding its getToolTipText(MouseEvent) method.

The Tool Tip API

Most of the API you need in order to set up tool tips belongs to the JComponent class, and thus is inherited by most Swing components. More tool tip API can be found in individual classes such as JTabbedPane. In general, those APIs are sufficient for specifying and displaying tool tips; you usually do not need to deal directly with the implementing classes JToolTip and ToolTipManager.

Post a Comment

Mới hơn Cũ hơn