<?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>Conor's Blog &#187; jar</title>
	<atom:link href="http://blog.macaoidh.name/tag/jar/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.macaoidh.name</link>
	<description>PHP, Music, Linux</description>
	<lastBuildDate>Mon, 05 Jul 2010 13:25:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java Visual Hello World Popup</title>
		<link>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/</link>
		<comments>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 13:07:19 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[College]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[add text to jframe]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jbutton]]></category>
		<category><![CDATA[jframe]]></category>
		<category><![CDATA[jpanel]]></category>
		<category><![CDATA[popup]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[visual]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=261</guid>
		<description><![CDATA[I spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It&#8217;s quite simple really once you get around the strict OOP and different syntax&#8230; This is how I did it: First you need to import some of the java [...]]]></description>
			<content:encoded><![CDATA[<p>I spent yesterday going a bit deeper into Java with one of my lecturers. I decided to try and write a more complicated hello world app. It&#8217;s quite simple really once you get around the strict OOP and different syntax&#8230; This is how I did it:</p>
<p>First you need to import some of the java libraries that we will be using:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
</pre>
<p>Then add the class, Popup. Make sure to name the file Popup.java</p>
<pre class="brush: java">
public class Popup{
private JFrame frame=new JFrame("Hello!");
private JPanel panel=new JPanel();
private JButton button=new JButton("Say Hello");
private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
private ButtonListener buttonListener=new ButtonListener();
</pre>
<p>What that actually means is create a new instance of JFrame, JPanel, JButton and JLabel and store them in the frame,panel, button and label variables. As for the ButtonListener, we will deal with that later. Now we will deal with the launchFrame function which does what it says on the tin &#8211; launches the frame! Basically it just connects all of the pieces of information stored into the variables above and displays them together.</p>
<pre class="brush: java">
public void launchFrame(){
int height=400, width=600;
frame.setSize(width,height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
</pre>
<p>Above we create two new integers which store the width and height of our frame. The next line sets the variables to the frame size. The final line tells the Java application to close when the window, or JFrame, is closed. Now that we have the configuration out of the way we can move on to adding the variables to the frame.</p>
<pre class="brush: java">
frame.getContentPane().add(label,BorderLayout.CENTER);
panel.add(button);
frame.getContentPane().add(panel,BorderLayout.WEST);
button.addActionListener(buttonListener);
frame.setVisible(true);
}
</pre>
<p>The code above adds the label to the frame, then adds the button to the panel and then the panel to the frame. Once again disregard the button.addAction.. line for now. The final line simply sets the frame to visible so we can see it.</p>
<p>Now that we have the frame settled, we need to add our essential main function to start up the execution:</p>
<pre class="brush: java">
public static void main(String args[]){
Popup popup=new Popup();
popup.launchFrame();
}
}
</pre>
<p>This code basically creates an instance of our class, Popup and then executes the function launchFrame which we have created in that class.</p>
<p>That&#8217;s the guts of it, but now we need to talk about these lines:</p>
<pre class="brush: java">
private ButtonListener buttonListener=new ButtonListener();</pre>
<pre class="brush: java">
 public void launchFrame(){
....
button.addActionListener(buttonListener);
</pre>
<p>Basically we have previously created a button, but these lines define what happens when the button is pressed. The second line of code tells the machine to listen out for the clicking of the button, and when that happens to execute the variable buttonListener. We have created the variable buttonListener, which creates a new instance of the ButtonListener class which we have yet to create.</p>
<p>That is the next step, to create that class. Create a new file called ButtonListener.java and add this code:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;</pre>
<pre class="brush: java">
public class ButtonListener implements ActionListener{
</pre>
<p>The first four lines import the libraries that we are using. The final line is important. It states that our class, ButtonListener is implementing the ActionListener class. Below is the content of our new class:</p>
<pre class="brush: java">
private JFrame pressed=new JFrame("Hello World!");</pre>
<pre class="brush: java">
public void actionPerformed(ActionEvent actionEvent){
</pre>
<p>First, we create a new JFrame for our popup just like we did in the other file. The function actionPerformed is  a function from the ActionListener class that we are implementing. The parameters for the function are predefined so we need them. The rest of the code is very similar to the previous code so I won&#8217;t explain it in depth. Basically it just configures the new frame, adds the label and then displays it:</p>
<pre class="brush: java">
int height=160, width=200;
pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
pressed.setSize(width,height);
pressed.setVisible(true);
}</pre>
<p>Save that file and then compile both files by executing:</p>
<pre class="brush: bash">
javac ButtonListener.java Popup.java
</pre>
<p>Now if you run the program you should see a nice visual dialogue:</p>
<pre class="brush: bash">
java Popup
</pre>
<p>You can download a jar archive of the program <a href="http://files.macaoidh.name/Download/Java-Popup-Hello-World">here</a>.</p>
<p>The full code is below. Popup.java:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class Popup{
        private JFrame frame=new JFrame("Hello!");
        private JPanel panel=new JPanel();
        private JButton button=new JButton("Say Hello");
        private JLabel label=new JLabel("Click the button to say hello...",SwingConstants.CENTER);
        private ButtonListener buttonListener=new ButtonListener();

        public void launchFrame(){
                int height=400, width=600;
                frame.setSize(width,height);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().add(label,BorderLayout.CENTER);
                panel.add(button);
                frame.getContentPane().add(panel,BorderLayout.WEST);
                button.addActionListener(buttonListener);
                frame.setVisible(true);
        }

        public static void main(String args[]){
                Popup popup=new Popup();
                popup.launchFrame();
        }
}
</pre>
<p>ButtonListener.java:</p>
<pre class="brush: java">
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonListener implements ActionListener{
        private JFrame pressed=new JFrame("Hello World!");

        public void actionPerformed(ActionEvent actionEvent){
                int height=160, width=200;
                pressed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JLabel pressedtext=new JLabel("Hello World!",SwingConstants.CENTER);
                pressed.getContentPane().add(pressedtext,BorderLayout.CENTER);
                pressed.setSize(width,height);
                pressed.setVisible(true);
        }

}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/11/15/java-visual-hello-world-popup/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating A Jar Archive</title>
		<link>http://blog.macaoidh.name/2009/04/27/creating-a-jar-archive/</link>
		<comments>http://blog.macaoidh.name/2009/04/27/creating-a-jar-archive/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 21:42:21 +0000</pubDate>
		<dc:creator>Conor</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[comporession]]></category>
		<category><![CDATA[compress a java file]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jar cmf]]></category>
		<category><![CDATA[manifest]]></category>
		<category><![CDATA[MF]]></category>
		<category><![CDATA[seal]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://blog.macaoidh.name/?p=198</guid>
		<description><![CDATA[I made the HelloWorld class a few days ago in this post. I am getting a bit ahead of myself here but what if you have an application with many files? Do you let the user handle all those files yourself? No! The Java Archiving Format .jar is the solution. With it you can create [...]]]></description>
			<content:encoded><![CDATA[<p>I made the HelloWorld class a few days ago in <a title="Hello World - Java" href="http://blog.macaoidh.name/2009/04/26/hello-world-java/">this post</a>. I am getting a bit ahead of myself here but what if you have an application with many files? Do you let the user handle all those files yourself? No! The Java Archiving Format .jar is the solution. With it you can create an entire application and bring it down to one file. Then you can run the whole application from that file! This can be done with the option of compression aswell.</p>
<p>Anyway even though it seems useless to create a jar archive of one file I am going to create one for the HelloWorld.class. First I need to create a file in which to store the manifest. This file will contain crucial information about the application and how it is run. I am going to do the bare minimum at the moment but there are endless possibilities with this file! This is what I put in HelloWorld.mf:</p>
<pre>Main-Class: HelloWorld
Name: class/
Sealed: true</pre>
<p>The main class tells the application which class to load first. Name and sealed tell the application which folders to seal. You might want to seal a package to ensure  version consistency among the classes in your software.</p>
<p>I now have two files; HelloWorld.class and HelloWorld.MF. Now I must compress them both into a jar archive and run the application. To create the jar archive execute:</p>
<pre>jar cmf HelloWorld.mf HelloWorld.jar HelloWorld.class</pre>
<p>Basically this says create a new jar archive including the manifest and the class file and locate it in a new file called HelloWorld.jar. Now the application is executable. To run it simply type:</p>
<pre>java -jar HelloWorld.jar</pre>
<p>You should see the same output from the original application:</p>
<pre>[conor@host java]$ java -jar HelloWorld.jar
Hello World!</pre>
<p>Simple as that!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.macaoidh.name/2009/04/27/creating-a-jar-archive/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
