<?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>Along Came Betty &#187; swing</title>
	<atom:link href="http://blog.darevay.com/category/swing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.darevay.com</link>
	<description>You know, software and some other stuff like maybe guitar or something</description>
	<lastBuildDate>Tue, 24 Nov 2009 03:21:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Use enum to define JTable columns</title>
		<link>http://blog.darevay.com/2008/11/use-enum-to-define-jtable-columns/</link>
		<comments>http://blog.darevay.com/2008/11/use-enum-to-define-jtable-columns/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 04:39:21 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[swing]]></category>
		<category><![CDATA[java swing]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=70</guid>
		<description><![CDATA[Last week while tediously defining another Swing TableModel, I had a little epiphany. Typically, I&#8217;d define column headers, types, etc with a list of integer constants, and some arrays:

public class MyTableModel extends AbstractTableModel
{
    private final int NAME_COLUMN = 0;
    private final int VALUE_COLUMN = 1;

    private [...]]]></description>
			<content:encoded><![CDATA[<p>Last week while tediously defining another Swing <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/TableModel.html">TableModel</a>, I had a little epiphany. Typically, I&#8217;d define column headers, types, etc with a list of integer constants, and some arrays:</p>
<pre name="code" class="java">
public class MyTableModel extends AbstractTableModel
{
    private final int NAME_COLUMN = 0;
    private final int VALUE_COLUMN = 1;

    private final String NAMES[] = { "Name", "Value" };
    private final Class CLASSES[] = { String.class, Double.class };

    . . .

   public String getColumnName(int columnIndex)
   {
      return NAMES[columnIndex];
   }

   . . .
}
</pre>
<p>This code is pretty tedious to maintain. In particular, switching column order involves a bunch of changes that are easy to get wrong. How about this instead&#8230; use an <strong>enum</strong> to define the columns!!</p>
<pre name="code" class="java">
public class MyTableModel extends AbstractTableModel
{
    private static enum Columns
    {
        Name(String.class), Value(Double.class);

        final Class<?> klass;

        Columns(Class<?> klass)
        {
             this.klass = klass;
        }
    }

    . . .

   public int getColumnCount() { return Columns.values().length; }

   public Class<?> getColumnClass(int columnIndex)
   {
      return Columns.values()[columnIndex].klass;
   }

   public String getColumnName(int columnIndex)
   {
      return Columns.values()[columnIndex].toString();
   }

   . . .
}
</pre>
<p>Now rearranging column order just works. Furthermore, you can add whatever column-specific functionality you like as methods on the enum. I think this approach can be generified with an interface for the enum to implement and a new abstract table model base class that can handle all the boilerplate above (<code>getColumnCount()</code>, <code>getColumnName()</code>, etc). When I get around to trying it out, I&#8217;ll post an update.</p>
<p>Late breaking: Of course, a quick search reveals <a href="http://www.basilv.com/psd/blog/2006/advanced-uses-of-java-5-enums">I&#8217;m not the first person to think of this</a>. Typical.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2008/11/use-enum-to-define-jtable-columns/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
