<?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>All Free Tech &#187; Swing</title>
	<atom:link href="http://www.allfreetech.com/tag/swing/feed" rel="self" type="application/rss+xml" />
	<link>http://www.allfreetech.com</link>
	<description>For developers</description>
	<lastBuildDate>Tue, 01 Feb 2011 13:45:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Swinging and Tweeting with Java and NetBeans</title>
		<link>http://www.allfreetech.com/ebook/java/swinging-and-tweeting-with-java-and-netbeans-129.html</link>
		<comments>http://www.allfreetech.com/ebook/java/swinging-and-tweeting-with-java-and-netbeans-129.html#comments</comments>
		<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[NetBeans]]></category>
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://www.allfreetech.com/?p=129</guid>
		<description><![CDATA[In this article by Alfonso Romero, we will discuss how to go about building a custom application for Twitter using java and NetBeans, instead of using the standard web interace. We will learn how login into Twitter using our application and also see the last tweet. Due to the fact that writing a Java app [...]]]></description>
			<content:encoded><![CDATA[<p>In this article by <b>Alfonso Romero</b>, we will discuss how to go about building a custom application for Twitter using java and NetBeans, instead of using the standard web interace. We will learn how login into Twitter using our application and also see the last tweet.</p>
<p>Due to the fact that writing a Java app to control your Twitter account is quite a long process and requires several features, I intend to divide this article in several sections, so you can see in extreme detail all the bells and whistles involved in writing Java applications.</p>
<h1>Downloading and installing NetBeans for your developing platform</h1>
<p>To download NetBeans, open a web browser window and go to the <a href="http://www.netbeans.org" target="_blank">NetBeans</a> website. Then click on the <span style="font-style: italic;">Download</span> button and select the <span style="font-style: italic;">All IDE download</span> bundle. After downloading NetBeans, install it with the default options.</p>
<h1>Creating your SwingAndTweet project</h1>
<ol>
<li>Open NetBeans and select <b>File </b><b>|</b><b> New Project</b> to open the <b>New Project</b> dialog. Now select <b>Java</b> from the <b>Categories</b> panel and<b> Java Application</b> from the <b>Projects</b> panel. Click on <b>Next</b> to continue.</li>
<li>The <b>New Java Application</b> dialog will show up next. Type <i>SwingAndTweet</i> in the <i>Project Name</i> field, mark the <i>Use Dedicated Folder for Storing Libraries</i> option, deselect the <i>Create Main Class</i> box (we&rsquo;ll deal with that later), make sure the <b>Set as Main Project</b> box is enabled and click on<b> Next</b> to continue:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-1.png" /></p>
</li>
<li>NetBeans will create the <i>SwingAndTweet</i> project and will show it under the <i>Projects</i> tab, in the NetBeans main window. Right click on the project&rsquo;s name and select <i>JFrame Form</i>&#8230; in the pop-up menu:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-2.png" /></p>
</li>
<li>The <b>New JFrame Form</b> window will appear next. Type <i>SwingAndTweetUI</i> in the <i>Class Name</i> field, type <i style="font-weight: bold;">swingandtweet</i> in the <i>Package</i> field and click on <i>Finish</i> to continue:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-3.png" /></p>
</li>
<li>NetBeans will open the <i style="font-weight: bold;">SwingAndTweetUI</i> frame in the center panel of the main screen. Now you&rsquo;re ready to assemble your Tweeter Java application!</li>
<li>Now let me explain a little bit about what we did in the previous exercise: First, we created a new Java application called <span style="font-weight: bold;">SwingAndTweet</span>. Then we created a Swing <b>JFrame</b> component and we named it <i>SwingAndTweetUI</i>, because this is going to act as the foundation, where we&rsquo;re going to put all the other Swing components required to interact with Twitter. Now I&rsquo;m going to show you how to download and integrate the <i>Twitter4J API</i> to your <i>SwingAndTweet</i>Java application.</li>
</ol>
<h1>Downloading and integrating the Twitter4J API into your NetBeans environment</h1>
<p>For us to be able to use the powerful classes and methods from the <i>Twitter4J API</i>, we need to tell NetBeans where to find them and integrate them into our Java applications.</p>
<ol>
<li>Open a web browser window, go to <a href="http://repo1.maven.org/maven2/net/homeip/yusuke/twitter4j/" target="_blank">http://repo1.maven.org/maven2/net/homeip/yusuke/twitter4j/</a> and search for the latest <i>twitter4j.2.X.X.jar</i> file, or download the most recent version at the time of this writing from here:<a href="http://repo1.maven.org/maven2/net/homeip/yusuke/twitter4j/2.0.9/twitter4j-2.0.9.jar" target="_blank">http://repo1.maven.org/maven2/net/homeip/yusuke/twitter4j/2.0.9/twitter4j-2.0.9.jar.</a></li>
<li>Once you download it in your computer, go to NetBeans, right-click on the <i>SwingAndTweet</i> project and select <i>Properties</i> from the context menu. Once at the project properties screen, select the <i>Libraries</i> category under the <i>Categories</i> panel, click on the <i>Add JAR/Folder</i>&#8230; button at the middle-right part of the screen to open the <i>Add JAR/Folder</i> dialog, navigate to the directory where you downloaded the <i>twitter4j-2.X.X.jar</i> file and double click on it to add it to your project&rsquo;s library path:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-4.png" /></p>
</li>
<li>Click on <span style="font-style: italic;">OK</span> to close the <i>Project Properties </i>dialog and return to the NetBeans main screen.</li>
</ol>
<p>Ok, you have integrated the <i>Twitter4J API</i> to your <i>SwingAndTweet</i> application. Now, let&rsquo;s see how to log into your Twitter account from our Java application&#8230;</p>
<h1>Logging into Twitter from Java and seeing your last Tweet</h1>
<p>In the following exercise, I&rsquo;ll show you how easy it is to start communicating with Twitter from a Java application, thanks to the Twitter class from the Twitter4J API. You&lsquo;ll also learn how to check your last tweet through your Java application.</p>
<p>Let&rsquo;s see how to log into a Twitter account:</p>
<ol>
<li>Go to the <i>Palette</i> window and locate the <i>JLabel</i> component under the <i>Swing Controls</i> section; then drag and drop it into the <i>TweetAndSwing</i> <i>JFrame</i> component:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-5.png" /></p>
</li>
<li>Now drag a <i>Button</i> and a <i>Text Editor</i>, too. Once you have the three controls inside the <i>SwingAndTweetUI JFrame</i> control, arrange them as shown below:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-6.png" /></p>
</li>
<li>The next step is to change their names and captions, to make our application look more professional. Right click on the <i>JLabel1</i> control, select <i>Edit</i> from the context menu, type <i>My Last Tweet</i> and hit <b>Enter</b>. Do the same procedure with the other two controls: erase the text in the <b>jTextField1</b> control and type <i>Login</i> in the <i>jButton1</i> control.</li>
<li>Rearrange the <i>jLabel1</i> and <i>jTextField1</i> controls, and drag one of the ends of <i>jTextField1</i> to increase its length all you can. Once done, your application will look like this:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-7.png" /></p>
</li>
<li>
<ol>
<li value="5">And now, let&rsquo;s inject some life to our application! Double click on the <i>JButton1</i> control to open your application&rsquo;s code window. You&rsquo;ll be inside a java method called <b>jButton1ActionPerformed</b>. This method will execute every time you click on the <i>Login</i> button, and this is where we&rsquo;re going to put all the code for logging into your Twitter account.</li>
<li>Delete the <i>// TODO add your handling code here</i>: line and type the following code inside the <b>JButton1ActionPerformed</b> method:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-8.png" /></p>
<p>
				Remember to replace <i>username</i> and <i>password</i> with your real Twitter username and password.</li>
<li>If you look closely at the line numbers, you&lsquo;ll notice there are five error icons on lines 82, 84, 85,&nbsp; 88 and 89. That&rsquo;s because we need to add some import lines at the beginning of your code, to indicate NetBeans where to find the <i>Twitter</i> and <i>JOptionPane</i> classes, and the <i>TwitterException</i>.</li>
<li>Scroll up until you locate the package swingandtweet; line; then add the following lines:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-9.png" /></p>
</li>
<li>Now all the errors will disappear from your code. To see your Java application in action, press <b>F6</b> or select <i>Run&nbsp; Run </i><i>|</i><i> Main Project</i> from the NetBeans main menu. The Run Project window will pop up, asking you to select the main class for your project. The <i>swingandtweet.SwingAndTweetUI</i> class will already be selected, so just click on <span style="font-style: italic;">OK</span> to continue.</li>
<li>Your <i>SwingAndTweetUI</i> application window will appear next, showing the three controls you created. Click on the<span style="font-style: italic;"> Login</span> button and wait for the <i>SwingAndTweet</i> application to validate your Twitter username and password. If they&rsquo;re correct, the following dialog will pop up:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-10.png" /></p>
</li>
<li>Click on <b>OK</b> to return to your <i>SwingAndTweet</i> application. Now you will see your last tweet on the textbox control:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-11.png" /></p>
</li>
<li>If you want to be really sure it&rsquo;s working, go to your Twitter account and update your status through the Web interface; for example, type <i>Testing my Java app</i>. Then return to your <i>SwingAndTweet</i> application and click on the <i>Login</i> button again to see your last tweet. The textbox control will now reflect your latest tweet:
<p style="text-align: center;"><img src="http://www.packtpub.com/files/images/java-netbeans-12.png" /></p>
</li>
<li>As you can see, your <i>SwingAndTweet</i> Java application can now communicate with your Twitter account! Click on the<i> <span style="font-weight: bold;">X</span></i><span style="font-weight: bold;"> </span>button to close the window and exit your <i>SwingAndTweet</i> application.</li>
</ol>
<h1>Examining the code</h1>
<p>Ok, I know this is a very simple example, but nevertheless we need to look at the code you wrote, because it will be the clue to add more features to your new Java app, like seeing your followers, the people you follow, the latest tweets from you and the people you follow, and what&rsquo;s more important, you&rsquo;ll be able to update your status without having to log into the Web interface anymore!</p>
<p>And know let&rsquo;s talk about the code. Below you&rsquo;ll find the complete code for the <b>jButton1ActionPerformed</b> method, so you don&rsquo;t have to go back and forth between this article and your code:<span style="font-family: monospace;"><br />
			</span></p>
<pre style="margin-left: 40px;">try {
           Twitter twitter = new Twitter(&quot;alromeromx&quot;, &quot;Nina1976742&quot;);
           twitter.verifyCredentials();
           JOptionPane.showMessageDialog(null, &quot;You&#39;re logged in!&quot;);
           java.util.List&lt;Status&gt; statusList = twitter.getUserTimeline();
           String s = String.valueOf(statusList.get(0).getText());
           jTextField1.setText(s);
        } catch (TwitterException e) {

	JOptionPane.showMessageDialog (null, &quot;Login failed&quot;);

	}
</pre>
<p>The first thing to notice is that all the code is surrounded by a <b>try-catch</b> statement. For those of you who haven&rsquo;t worked with <b>try-catch</b>, just keep in mind that the <b>try</b> block contains the code that will execute until an error shows up; in this case, if the <b>SwingAndTweet</b> application can log into your Twitter account, the try block will execute completely, and the catch block will not execute.</p>
<p>But if an error occurs when trying to log into your Twitter account, the try block will stop executing after the <b>twitter.verifyCredentials()</b> statement and will jump to the <b>JOptionPane.showMessageDialog (null, &quot;Login failed&quot;);</b> statement inside the catch block. The result will be a dialog box indicating the login process failed, and then the application will return to the main screen, without updating your Twitter status.</p>
<p>Inside the try block, the first line creates a <i>Twitter</i> object named <i>twitter</i> (yes, I know I could&rsquo;ve thought of a more creative name) with your <b>username</b> and your <b>password</b> as parameters. The next line:</p>
<pre style="margin-left: 40px;">twitter.verifyCredentials();
</pre>
<p>is the one in charge of verifying that you can log into your Twitter account. As I explained before, if an error occurs when trying to log in, an exception will be thrown up and the following lines of code will not execute, because the execution will jump to the line inside the catch block.</p>
<p>Now, if the login process is successful, the execution will continue with:&nbsp;</p>
<pre style="margin-left: 40px;">JOptionPane.showMessageDialog(null, &quot;You&#39;re logged in!&quot;);
</pre>
<p>This line is the one that shows the dialog window indicating you could log in successfully to your Twitter account. Then, when you press the OK button on this dialog, it will close and you&rsquo;ll return to the main screen of your <b>SwingAndTweet</b> application, and the next line of code will execute:</p>
<pre style="margin-left: 40px;">
<style type="text/css">
	<!--
		@page { margin: 0.79in }
		P { margin-bottom: 0.08in }
	-->
	</style>

java.util.List&lt;Status&gt; statusList = twitter.getUserTimeline();
</pre>
<p>In this line we create a List object called statusList, and we assign to this object the result returned by the getUserTimeline method of the twitter object. This method returns all the information regarding your 20 most recent Tweets.</p>
<p>On the next line, we create a <b>String</b> object called <b>s</b>, and we assign to it all the information regarding the text of your first Tweet only, through the <b>statusList.get(0).getText()</b> method. But first we need to use the <b>String.valueOf</b> method to convert the <b>Object</b> value returned by the <b>statusList.get(0).getText()</b> method into a <b>String</b> value that <b>s</b> can receive.</p>
<p>The last line:</p>
<div style="margin-left: 40px;">
<pre>jTextField1.setText(s);
</pre>
</div>
<p>assigns the value of the <b>s</b> string to the textbox control named <b>jTextField1</b>, so you can see your latest tweet in the <b>SwingAndTweet</b> main window.</p>
<h1>Summary</h1>
<p>In this article, we discussed how to go about building a custom application for Twitter using java and NetBeans.</p>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.allfreetech.com/ebook/java/swinging-and-tweeting-with-java-and-netbeans-129.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

