<?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>javajiggle.com &#187; Swing</title>
	<atom:link href="http://javajiggle.com/category/swing/feed/" rel="self" type="application/rss+xml" />
	<link>http://javajiggle.com</link>
	<description>Java Jiggle. See what's shaking.</description>
	<lastBuildDate>Sat, 14 Mar 2009 17:40:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Firefox buttons in your JTabbedPane. (Example)</title>
		<link>http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/</link>
		<comments>http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/#comments</comments>
		<pubDate>Tue, 18 Dec 2007 02:09:35 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/</guid>
		<description><![CDATA[For my last post I showed how easy it is to add components to the tabs in JTabbedPane. Someone replied that the example was a bit primitive and asked me how to add buttons that look similar to the Firefox tab buttons. Below is an example that  uses Firefox buttons.
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">For my last post I showed how easy it is to add components to the tabs in JTabbedPane.<span> </span>Someone replied that the example was a bit primitive and asked me how to add buttons that look similar to the Firefox tab buttons.<span> </span>Below is an example that  uses Firefox buttons.</p>
<p><em>import java.awt.*;<br />
import java.awt.event.*;<br />
import java.net.*;</em></p>
<p><em>import javax.swing.*;</em></p>
<p><em>public class JtabbedCloseExample extends JFrame {</em></p>
<blockquote><p><em>JTabbedPane jtabbedPane;</em></p>
<p><em>class TabButton extends JPanel implements ActionListener {</em></p>
<blockquote><p><em> ImageIcon reg = null;<br />
ImageIcon over = null;public TabButton( String label ){</em></p>
<blockquote><p><em> super(new FlowLayout());add( new JLabel( label ));<br />
try{</em></p>
<blockquote><p><em> // load firefox buttons<br />
reg = new ImageIcon(new URL(&#8220;http://javajiggle.com/images/regular.JPG&#8221;));<br />
over = new ImageIcon(new URL(&#8220;http://javajiggle.com/images/hoverOver.JPG&#8221;));</em></p></blockquote>
<p><em>} catch( Exception e ){</em></p>
<blockquote><p><em> e.printStackTrace();</em></p></blockquote>
<p><em>}<br />
setOpaque(false);<br />
final TabButton self = this;<br />
final JButton button = new JButton(reg);<br />
button.setOpaque(false);<br />
button.setRolloverIcon(over);<br />
button.setPressedIcon(over);<br />
button.setBorderPainted( false );<br />
button.setContentAreaFilled(false);<br />
button.addActionListener( this );<br />
add( button );</em></p></blockquote>
<p><em>}</em></p>
<p><em>public void actionPerformed(ActionEvent ae ){</em></p>
<blockquote><p><em> // close the tab which was clicked<br />
jtabbedPane.remove(jtabbedPane.indexOfTabComponent( this ) );</em></p></blockquote>
<p><em>}</em></p></blockquote>
<p><em>}</em></p>
<p><em>public JtabbedCloseExample() {</em></p>
<blockquote><p><em>jtabbedPane = new JTabbedPane();<br />
jtabbedPane.add(new JTextArea(&#8220;Tab 1 text area.  Using Firefox buttons!&#8221;));<br />
jtabbedPane.add(new JTextArea(&#8220;Tab 2 text area.  Using Firefox buttons!&#8221;));<br />
jtabbedPane.add(new JTextArea(&#8220;Tab 3 text area.  Using Firefox buttons!&#8221;));<br />
jtabbedPane.setTabComponentAt( 0, new TabButton(&#8220;Tab 1&#8243;) );<br />
jtabbedPane.setTabComponentAt( 1, new TabButton(&#8220;Tab 2&#8243;) );<br />
jtabbedPane.setTabComponentAt( 2, new TabButton(&#8220;Tab 3&#8243;) );<br />
getContentPane().add( jtabbedPane );<br />
setSize(new Dimension(300, 200));<br />
setVisible(true);</em></p></blockquote>
<p><em>}</em></p>
<p><em>public static void main ( String [] args ){</em></p>
<blockquote><p><em> new JtabbedCloseExample();</em></p></blockquote>
<p><em>}</em></p></blockquote>
<p><em>}</em></p>
<p><img src="http://javajiggle.com/images/firefoxButtons.PNG" alt="JTabbedPane using firefox buttons." width="300" height="200" /></p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2007/12/18/firefox-buttons-in-your-jtabbedpane-example/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JDK 6 Rules!  Adding components to the tabs on JTabbedPane is now a breeze. Check it out!</title>
		<link>http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/</link>
		<comments>http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 02:36:44 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/</guid>
		<description><![CDATA[As you already know, JTabbedPane provides a convenient way for components such as JPanels to share the same real estate on the screen. It also allows a user to select a desired panel by clicking on a tab. If you&#8217;ve ever used a Firefox browser than you truly appreciate tabbed panes. Firefox displays each web [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><img src="https://duke.dev.java.net/images/comfyChair/ComfyChairRadSmall.jpg" alt="JDK 6 Rules!" width="270" height="270" align="left" />As you already know, JTabbedPane provides a convenient way for components such as JPanels to share the same real estate on the screen.<span> </span>It also allows a user to select a desired panel by clicking on a tab.<span> </span>If you&#8217;ve ever used a Firefox browser than you truly appreciate tabbed panes.<span> </span>Firefox displays each web page in a separate tab, allowing a user to switch between pages by clicking on a corresponding tab.</p>
<p class="MsoNormal">Up until JDK 1.6, we had limited control of what was rendered on a tab itself.<span> </span>In particular we were limited to the label, the icon and a tooltip.<span> </span>In other words we could not place a button on a tab itself as is done in a Firefox browser.<span> </span><span> </span>Many improvised by manually drawing components and simulating their behavior.<span> </span>Although this solution worked great, the code was way too complicated for a simple task.  With JDK 6 came three new methods which completely changed the behavior of a tabbed pane.   You can now set componets on a tab itself.  In addition, swing will render these components for you.   All possible with three methods below:</p>
<p class="MsoNormal">
<p class="MsoNormal"><strong>public void setTabComponentAt(int index, Component component)</strong> – allows you to add a component such as a button to a tab.<span> </span>The rendering and the event handling is done for you by swing.<span> </span></p>
<p class="MsoNormal">
<p class="MsoNormal"><strong>public Component getTabComponentAt(int index)</strong> – gets a component for a corresponding index that was set by a setTabComponentAt(..) method above.<span> </span></p>
<p class="MsoNormal">
<p class="MsoNormal"><strong>public int indexOfTabComponent(Component tabComponent)</strong> – reverse lookup, that returns an index of a tab that corresponds to the tab component set by a setTabComponentAt(..) method above.<span> </span>This is particularly handy if you are using a button to remove a corresponding tab from a tabbed pane.</p>
<p class="MsoNormal">
<p class="MsoNormal">Below is a simple example that illustrates how to implement a Firefox style close button on the individual tab.<span> </span>Keep in mind this example won’t work on versions prior to JDK 1.6!</p>
<pre></pre>
<blockquote>
<p class="MsoNormal"><strong>import java.awt.*;</strong></p>
<p class="MsoNormal"><strong>import java.awt.event.*;</strong></p>
<p class="MsoNormal"><strong> </strong></p>
<p class="MsoNormal"><strong>import javax.swing.*;</strong></p>
<p class="MsoNormal"><strong> </strong></p>
<p class="MsoNormal"><strong>public class JtabbedCloseExample extends JFrame {</strong></p>
<p class="MsoNormal"><strong><span> </span></strong></p>
<blockquote>
<p class="MsoNormal"><strong><span> </span>JTabbedPane jtabbedPane;</strong></p>
<p class="MsoNormal"><strong><span> </span>class TabButton extends JPanel implements ActionListener {</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span> </span>public TabButton( String label ){</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span> </span><span> </span>super(new FlowLayout());</strong></p>
<p class="MsoNormal"><strong><span> </span>add( new JLabel( label ));</strong></p>
<p class="MsoNormal"><strong><span> </span>JButton button = new JButton(&#8220;X&#8221;);</strong></p>
<p class="MsoNormal"><strong><span> </span>button.addActionListener( this );</strong></p>
<p class="MsoNormal"><strong><span> </span>button.setMargin(new Insets(0,0,0,0));</strong></p>
<p class="MsoNormal"><strong><span> </span>add( button );<span> </span></strong></p>
</blockquote>
</blockquote>
<p class="MsoNormal"><strong><span> </span><span> </span> }</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span> </span>public void actionPerformed(ActionEvent ae ){</strong></p>
<blockquote>
<p class="MsoNormal"><strong><span> </span>// close the tab which was clicked<br />
jtabbedPane.remove(jtabbedPane.indexOfTabComponent( this ) );</strong></p></blockquote>
<p class="MsoNormal"><strong><span> </span>}</strong></p>
</blockquote>
<p class="MsoNormal"><strong><span> </span>}</strong></p>
<p class="MsoNormal"><strong><span> </span></strong></p>
<p class="MsoNormal"><strong><span> </span></strong></p>
<p class="MsoNormal"><strong><span> </span>public JtabbedCloseExample() {</strong></p>
<p class="MsoNormal"><strong><span> </span></strong></p>
<blockquote>
<p class="MsoNormal"><strong><span> </span><span> </span>jtabbedPane = new JTabbedPane();<span> </span></strong></p>
<p class="MsoNormal"><strong><span> </span>jtabbedPane.add(new JTextArea(&#8220;Tab 1 text area&#8221;));<span> </span></strong></p>
<p class="MsoNormal"><strong><span> </span>jtabbedPane.add(new JTextArea(&#8220;Tab 2 text area&#8221;));</strong></p>
<p class="MsoNormal"><strong><span> </span>jtabbedPane.add(new JTextArea(&#8220;Tab 3 text area&#8221;));</strong></p>
<p class="MsoNormal"><strong><span> </span>jtabbedPane.setTabComponentAt( 0, new TabButton(&#8220;Tab 1&#8243;) );</strong></p>
<p class="MsoNormal"><strong><span> </span>jtabbedPane.setTabComponentAt( 1, new TabButton(&#8220;Tab 2&#8243;) );</strong></p>
<p class="MsoNormal"><strong><span> </span>jtabbedPane.setTabComponentAt( 2, new TabButton(&#8220;Tab 3&#8243;) );</strong></p>
<p class="MsoNormal"><strong><span> </span>getContentPane().add( jtabbedPane );</strong></p>
<p class="MsoNormal"><strong><span> </span>setSize(new Dimension(300, 200));</strong></p>
<p class="MsoNormal"><strong><span> </span>setVisible(true);</strong></p>
</blockquote>
<p class="MsoNormal"><strong><span> </span>}</strong></p>
<p class="MsoNormal"><strong> </strong></p>
<p class="MsoNormal"><strong><span> </span>public static void main ( String [] args ){</strong></p>
<p class="MsoNormal"><strong><span> </span> new JtabbedCloseExample();</strong></p>
<p class="MsoNormal"><strong><span> </span>}</strong></p>
</blockquote>
<p class="MsoNormal"><strong>}</strong></p>
<p class="MsoNormal"><img src="file:///C:/DOCUME%7E1/P/LOCALS%7E1/Temp/moz-screenshot-1.jpg" alt="" /> <img src="http://javajiggle.com/images/jtabbedpane.JPG" alt="" width="303" height="204" align="left" /></p>
</blockquote>
<blockquote><p>This is not the best looking window.   However, it clearly illustrates the easy with which you can add your custom components to the tabs!</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2007/11/16/jdk-6-rules-adding-components-to-the-tabs-is-a-breeze-check-it-out/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
