<?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>Axino.net &#187; JTable</title>
	<atom:link href="http://www.axino.net/tag/jtable/feed" rel="self" type="application/rss+xml" />
	<link>http://www.axino.net</link>
	<description>The other side of atom.</description>
	<lastBuildDate>Sat, 14 Aug 2010 23:11:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Addind content in JTable</title>
		<link>http://www.axino.net/tutorial/2009/11/addind-content-in-jtable</link>
		<comments>http://www.axino.net/tutorial/2009/11/addind-content-in-jtable#comments</comments>
		<pubDate>Tue, 03 Nov 2009 05:49:00 +0000</pubDate>
		<dc:creator>Arxleol</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[JTable]]></category>

		<guid isPermaLink="false">http://www.axino.net/?p=582</guid>
		<description><![CDATA[Java tutorial on this subject is complex and long opposite to other java tutorials. And as I have problem figuring this out, let me share with you probably the simplest way of inputting content into JTable. First thing you need is DefaultTableModel. We will initialize one object named mod. DefaultTableModel mod = new DefaultTableModel&#40;&#41;; Next step is [...]]]></description>
			<content:encoded><![CDATA[<p>Java tutorial on this subject is complex and long opposite to other java tutorials. And as I have problem figuring this out, let me share with you probably the simplest way of inputting content into JTable.</p>
<p><span id="more-582"></span>First thing you need is <strong>DefaultTableModel</strong>. We will initialize one object named <strong>mod</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">DefaultTableModel</span> mod <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">DefaultTableModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next step is entering column names. As usual you want to represent some sort of data in table. Now let&#8217;s say we want to enter some student data in the table.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">mod.<span style="color: #006633;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mod.<span style="color: #006633;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Surname&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mod.<span style="color: #006633;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Student ID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mod.<span style="color: #006633;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Dorm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
mod.<span style="color: #006633;">addColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Room number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So we will have five columns named <strong>Name, Surname, Student ID, Dorm, Room number</strong>. Now we need to enter data in rows of table. We will do this by initializing <strong>Object array</strong>. I strongly suggest that the size of this array is as the number of columns therefore 5.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> data <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now we need to fill out this array with data.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #339933;">;</span>
data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Doe&quot;</span><span style="color: #339933;">;</span>
data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;666&quot;</span><span style="color: #339933;">;</span>
data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;F&quot;</span><span style="color: #339933;">;</span>
data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;203&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now as we this array filled out next step is to add it in the row of table.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">mod.<span style="color: #006633;">addRow</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you want to add more rows then I suggest you wrap previous two code blocks into some loop. As shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">do</span><span style="color: #009900;">&#123;</span>
     data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;John&quot;</span><span style="color: #339933;">;</span>
     data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Doe&quot;</span><span style="color: #339933;">;</span>
     data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;666&quot;</span><span style="color: #339933;">;</span>
     data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;F&quot;</span><span style="color: #339933;">;</span>
     data<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;203&quot;</span><span style="color: #339933;">;</span>
     mod.<span style="color: #006633;">addRow</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Technically this code will add five same rows. But if you read data from somewhere else then you will be able to enter different data. Point here was to show how you may enter several rows one behind the other.</p>
<p>Final thing we need to do is to set our model (<strong>mod</strong>) as default model for table and data will appear in it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">jTable1.<span style="color: #006633;">setModel</span><span style="color: #009900;">&#40;</span>mod<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, sometimes you might not obtain desired result so you should just refresh table and your content will appear. <strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.axino.net/tutorial/2009/10/connect-to-mysql-database-in-java" rel="bookmark" title="Wednesday 21.10.2009">Connect to mysql database in java</a></li>
<li><a href="http://www.axino.net/tutorial/2009/12/hackthissite-org-extbasic-1-over-and-out" rel="bookmark" title="Wednesday 23.12.2009">hackthissite.org extbasic 1 :: Over and out?</a></li>
<li><a href="http://www.axino.net/tutorial/2009/11/basic-web-hacking-8-simple-sql" rel="bookmark" title="Monday 16.11.2009">Basic web hacking 8 :: simple SQL</a></li>
<li><a href="http://www.axino.net/programming/java-programming/2010/04/java-glassfish-and-ubuntu-server" rel="bookmark" title="Friday 30.04.2010">java glassfish and ubuntu server</a></li>
<li><a href="http://www.axino.net/tutorial/2009/06/how-to-obtain-get-parameters-from-url-with-javascript" rel="bookmark" title="Tuesday 09.06.2009">How to obtain get parameters from URL with javascript</a></li>
</ul>
<p><!-- Similar Posts took 5.578 ms --></p>
 <img src="http://www.axino.net/wordpress/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=582" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://www.axino.net/tutorial/2009/11/addind-content-in-jtable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
