[Swing getting started tutorial] Step by Step netbeans (3): customizes jtooltip that can display multiple lines

Source: Internet
Author: User
Tags netbeans

The labels of most components in netbeans are displayed in a single line. You just need to use settooltiptext (string tip) to set the labels for buttons and other prompts. However, if you want to set a multi-row label prompt, there will be a multi-row label prompt on netbeans, as shown in:

Some people on the Internet say that settooltiptext ("<HTML> AAAA/BR bbbbb ToolTip
The displayed components can be rewritten.JComponent
OfcreateToolTip
Method, and use a subclass of this class.

Method: First, define a multilinetooltip that inherits the jtooltip. Rewrite the createconltip () method for the component that needs to implement multiline prompts. When settooltiptext (string tip) is used) you can (use '/N' to separate multiple rows). Check the effect first:

Implementation of multilinw.ltip

Here we need to make the L & F of jtooltip, put the lines to be displayed in the string array, calculate the width and height of the display panel, traverse the array and draw the content to the Panel:

Package COM. monitor1394.netbeans. component; <br/> Import Java. AWT. dimension; <br/> Import Java. AWT. fontmetrics; <br/> Import Java. AWT. graphics; <br/> Import Java. AWT. toolkit; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. stringreader; <br/> Import Java. util. enumeration; <br/> Import Java. util. vector; <br/> Import javax. swing. jcomponent; <br/> Import javax. swing. JT Ooltip; <br/> Import javax. swing. swingutilities; <br/> Import javax. swing. plaf. metal. metaltooltipui; <br/>/** <br/> * multi-row tooltip <br/> * @ author Monitor <br/> * created on, 22:12:06 <br/> */<br/> public class multilinw.ltip extends jtooltip {<br/> Public multilinw.ltip () {<br/> setui (New multilinw.ltipui ()); <br/>}< br/> private class multilinw.ltipui extends metaltoo Ltipui {<br/> private string [] STRs; <br/> private int maxwidth = 0; <br/> @ override <br/> Public void paint (Graphics G, jcomponent c) {<br/> fontmetrics metrics = C. getfontmetrics (G. getfont (); <br/> dimension size = C. getsize (); <br/> G. setcolor (C. getbackground (); <br/> G. fillrect (0, 0, size. width, size. height); <br/> G. setcolor (C. getforeground (); <br/> If (STRs! = NULL) {<br/> for (INT I = 0; I <STRs. length; I ++) {<br/> G. drawstring (STRs [I], 3, (metrics. getheight () * (I + 1 )); <br/>}< br/> @ override <br/> public dimension getpreferredsize (jcomponent C) {<br/> fontmetrics metrics = toolkit. getdefatooltoolkit (). getfontmetrics (C. getfont (); <br/> string tiptext = (jtooltip) c ). gettiptext (); <br/> If (tiptext = NULL) {<br/> tiptext =" "; <Br/>}< br/> bufferedreader BR = new bufferedreader (New stringreader (tiptext); <br/> string line; <br/> int maxwidth = 0; <br/> vector v = new vector (); <br/> try {<br/> while (line = BR. readline ())! = NULL) {<br/> int width = swingutilities. computestringwidth (metrics, line); <br/> maxwidth = (maxwidth <width )? Width: maxwidth; <br/> v. addelement (line); <br/>}< br/>} catch (ioexception ex) {<br/> ex. printstacktrace (); <br/>}< br/> int lines = v. size (); <br/> If (lines <1) {<br/> STRs = NULL; <br/> lines = 1; <br/>} else {<br/> STRs = new string [Lines]; <br/> int I = 0; <br/> for (enumeration E = v. elements (); E. hasmoreelements (); I ++) {<br/> STRs [I] = (string) E. nextelement (); <br/>}< br/> int Height = metrics. getheight () * lines; <br/> This. maxwidth = maxwidth; <br/> return new dimension (maxwidth + 6, height + 4); <br/>}< br/>

The tabbutton class needs to override the createconltip () class:

@ Override <br/> Public jtooltip createconltip () {<br/> multilineconltip tip = new multilineconltip (); <br/> return tip; <br/>}

Set the content to be displayed:

Settooltiptext ("hold down SHIFT and click to close all documents/n hold down ALT and click to close other documents ");

Close the job.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.