<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>worth a read &#187; JLabel</title>
	<atom:link href="http://www.wortharead.com/tag/jlabel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wortharead.com</link>
	<description>thoughts on PHP, Linux and other boring things</description>
	<lastBuildDate>Mon, 30 Mar 2009 09:45:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Wrapping text in a JLabel</title>
		<link>http://www.wortharead.com/2009/03/29/wrapping-text-in-a-jlabel/</link>
		<comments>http://www.wortharead.com/2009/03/29/wrapping-text-in-a-jlabel/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 22:11:26 +0000</pubDate>
		<dc:creator>Duncan</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JLabel]]></category>
		<category><![CDATA[Swing]]></category>
		<category><![CDATA[wrap]]></category>

		<guid isPermaLink="false">http://www.wortharead.com/?p=83</guid>
		<description><![CDATA[The Java Swing framework does not seem to supply a component designed to display static text in fluid manner. JLabels are great for displaying a line of text, or a carefully constructed multi-line statement (with suitable line breaks), but provide no support for wrapping.  JTextAreas can probably be customized to the point where they [...]]]></description>
			<content:encoded><![CDATA[<p>The Java Swing framework does not seem to supply a component designed to display static text in fluid manner. <code>JLabel</code>s are great for displaying a line of text, or a carefully constructed multi-line statement (with suitable line breaks), but provide no support for wrapping.  <code>JTextArea</code>s can probably be customized to the point where they appear to look like static labels, but who has the time?</p>
<p>The <code>WrappableJLabel</code> class is an extension to the regular <code>JLabel</code> that provides support for basic wrapping.  When called with the default constructor, the component will expand to fill all available space in its parent container, wrapping if necessary.  An optional width argument can be passed in the constructor, which will be used as the preferred width of the component.</p>
<p>Download <a href='http://www.wortharead.com/wp-content/uploads/2009/03/wrappablejlabel.java'>WrappableJLabel.java</a> or view the <a href='http://www.wortharead.com/wp-content/uploads/2009/03/wrappablejlabel.html'>JavaDoc</a>.</p>
<h3>Example usage</h3>
<p>The following brief example creates a <code>WrappableJLabel</code> that will expand to fill the surrounding fixed-sized <code>JDialog</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Creates a JDialog with a fixed size and places
   a WrappableJLabel inside. */</span>
&nbsp;
<span style="color: #003399;">String</span> text <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur &quot;</span> <span style="color: #339933;">+</span>
  <span style="color: #0000ff;">&quot;adipiscing elit. Vivamus quis magna. Ut ante ligula, &quot;</span> <span style="color: #339933;">+</span>
  <span style="color: #666666; font-style: italic;">/* HTML line breaks are allowed */</span>
  <span style="color: #0000ff;">&quot;pulvinar sed, egestas in, tempor vel, dui.&lt;br&gt;&quot;</span> <span style="color: #339933;">+</span>  
  <span style="color: #0000ff;">&quot;Cras pellentesque sagittis sem. Maecenas venenatis &quot;</span> <span style="color: #339933;">+</span>
  <span style="color: #0000ff;">&quot;odio nec tellus. Nunc a sapien. Nunc quam turpis, &quot;</span> <span style="color: #339933;">+</span>
  <span style="color: #0000ff;">&quot;malesuada in, pellentesque non, fringilla ut, magna.&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">JDialog</span> dialog <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JDialog</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dialog.<span style="color: #006633;">setDefaultCloseOperation</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">WindowConstants</span>.
    <span style="color: #006633;">DISPOSE_ON_CLOSE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dialog.<span style="color: #006633;">setPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Dimension</span><span style="color: #009900;">&#40;</span>200, 300<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dialog.<span style="color: #006633;">setLayout</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BorderLayout</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
&nbsp;
WrappableJLabel wrapLabel <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WrappableJLabel<span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
dialog.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>wrapLabel, <span style="color: #003399;">BorderLayout</span>.<span style="color: #006633;">PAGE_START</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
dialog.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JButton</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test button&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #003399;">BorderLayout</span>.<span style="color: #006633;">PAGE_END</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dialog.<span style="color: #006633;">pack</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
dialog.<span style="color: #006633;">setLocationRelativeTo</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
dialog.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Comments, suggestions for improvements and bug reports welcome.  Due to levels of spam, you&#8217;ll need to register to leave a comment &#8211; apologies.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.wortharead.com/2009/03/29/wrapping-text-in-a-jlabel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
