<?xml version="1.0" encoding="utf-8"?>
			
			<rss version="2.0">
			<channel>
			<title>Trond Ulseth&apos;s Blog - Java for Newbies</title>
			<link>http://trond.ulseth.no/index.cfm</link>
			<description>Trond Ulseth</description>
			<language>en-us</language>
			<pubDate>Wed, 02 Jan 2008 20:15:23 -0600</pubDate>
			<lastBuildDate>Wed, 18 May 2005 22:58:00 -0600</lastBuildDate>
			<generator>BlogCFC</generator>
			<docs>http://blogs.law.harvard.edu/tech/rss</docs>
			<managingEditor>trond@ulseth.no</managingEditor>
			<webMaster>trond@ulseth.no</webMaster>
			
			
			
			
			
			<item>
				<title>Java for Newbies - part 2</title>
				<link>http://trond.ulseth.no/index.cfm?mode=entry&amp;entry=F1D9C439-A5AD-6841-651B62A4012F88E3</link>
				<description>
				
				&lt;p&gt;&lt;strong&gt;Hey GUIs, look here!&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The last time we took a look at how to create a window with JFrame, and some text in the window with JLabel. Now let&apos;s explore some more GUI elements, and how to lay them out within the window.&lt;/p&gt;
&lt;p&gt;First let&apos;s do some modifications to our code from last time, to prepare ourself for adding more elements into the mix.&lt;/p&gt;
&lt;p&gt;Directly below the &lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;import.javax.swing.*;&lt;/div&gt;
&lt;p&gt;add &lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;import java.awt.*;&lt;/div&gt;
&lt;p&gt;This imports a class library that we&apos;ll need for some of our new code.&lt;/p&gt;
&lt;p&gt;Then locate the following line and delete it&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;frame.getContentPane().add(label);&lt;/div&gt;
&lt;p&gt;Now we are going to throw a button in there, by introducing the following code below the lane where we create our label.&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;//Let&apos;s throw a button in there as well&lt;br/&gt;
JButton button = new JButton(&amp;quot;Click me for luck!&amp;quot;);&lt;/div&gt;
&lt;p&gt;Now the quick ones might guess that by deleting the code that added the label to the frame, and not introducing any such code will make our label and button not to apear. You are right. We are going to introduce the JPanel. We will use the JPanel for aranging our elements in the frame, but also create some padding by implementing an &amp;quot;empty&amp;quot; border.&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;JPanel pane = new JPanel(new GridLayout(0, 1));&lt;br/&gt;
&amp;nbsp;pane.add(label);&lt;br/&gt;
&amp;nbsp;pane.add(button);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; pane.setBorder(BorderFactory.createEmptyBorder(&lt;br/&gt;
&amp;nbsp;&amp;nbsp;20, //top&lt;br/&gt;
&amp;nbsp;&amp;nbsp;30, //left&lt;br/&gt;
&amp;nbsp;&amp;nbsp;10, //bottom&lt;br/&gt;
&amp;nbsp;&amp;nbsp;30) //right&lt;br/&gt;
&amp;nbsp;);&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&lt;br/&gt;
frame.getContentPane().add(pane);&lt;/div&gt;
&lt;p&gt;You see we add the label and the button to the JPanel, and then we use code similar to the one that used to put the label into the frame to put the JPanel into the frame. There&apos;s also a setBorder() method at play, that I don&apos;t think need further explanation.&lt;/p&gt;
&lt;p&gt;If your code is like &lt;a href=&quot;/UserFiles/File/j3.txt&quot; target=&quot;_blank&quot;&gt;mine&lt;/a&gt;, you should get the following when hitting the &amp;quot;Run&amp;quot; button.&lt;/p&gt;
&lt;p&gt;&lt;img height=&quot;116&quot; alt=&quot;j3&quot; src=&quot;/UserFiles/Image/j3.gif&quot; width=&quot;209&quot;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Text input&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let&apos;s quickly look at a couple of text field examples. After all there&apos;s not that many applications which don&apos;t accept any user input.&lt;/p&gt;
&lt;p&gt;Bellow the button creation put the following code&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;//Not a propper application without a text field&lt;br/&gt;
JTextField textField = new JTextField(5);&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&lt;br/&gt;
//While we&apos;re at text fields let&apos;s make one that only accepts a decimal formated number&lt;br/&gt;
JFormattedTextField textField2 = new JFormattedTextField(&lt;br/&gt;
&amp;nbsp;new java.text.DecimalFormat(&amp;quot;##0.0#&amp;quot;)&lt;br/&gt;
);&lt;/div&gt;
&lt;p&gt;The first one should need no further explanation. The second one, the formatted text field, you use when you want to controll a legal set of characters that can be entered into the text field. I wont go in to the different formating methods and the different formatting mask characters at the moment.&lt;/p&gt;
&lt;p&gt;I&apos;ll let you figure out how to add the text fields to the JPanel your self. But a click on the &amp;quot;Run&amp;quot; button should result in this (I&apos;ve added some input in the text fields here):&lt;/p&gt;
&lt;p&gt;&lt;img height=&quot;168&quot; alt=&quot;j4&quot; src=&quot;/UserFiles/Image/j4.gif&quot; width=&quot;209&quot;/&gt;&lt;/p&gt;
&lt;p&gt;I intented to go further, but time flys, and it&apos;s already time for bed :) So more GUI magic at a later time (very soon I promise).&lt;/p&gt;
				
				</description>
				
				<category>Java for Newbies</category>
				
				<pubDate>Wed, 18 May 2005 22:58:00 -0600</pubDate>
				<guid>http://trond.ulseth.no/index.cfm?mode=entry&amp;entry=F1D9C439-A5AD-6841-651B62A4012F88E3</guid>
				
				<enclosure url="http://trond.ulseth.no/enclosures/0" length="0" type=""/>
				
			</item>
			
		 	
			
			
			<item>
				<title>Java for Newbies - part 1</title>
				<link>http://trond.ulseth.no/index.cfm?mode=entry&amp;entry=D33C4BE4-E2B4-E594-CB6F48E638BA9BEF</link>
				<description>
				
				&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Hello, and welcome to my Java for Newbies series. I will follow the same approach writing this series as I do with my Mach-II for Newbies series. For you not familiar with it, the approach is this:&lt;/p&gt;
&lt;p&gt;I write as i learn and learn as I write.&lt;/p&gt;
&lt;p&gt;This means that I have no Java experience on my back. I just document my own learning journey so that (hopefully) helping others going the same path.&lt;/p&gt;
&lt;p&gt;However I hope some of the more Java savvy people out there will help me out on the road. I will have questions, and I will probably get lost at some time.&lt;/p&gt;
&lt;p&gt;My first personal goal is to be able to make simple desktop applications. Like a password generator, a simple address directory and such. So I will pretty much jump into Swing (user interface) development, instead of the typical faceless console approach most tutorials out there take.&lt;/p&gt;
&lt;p&gt;I&apos;m using Eclipse for my development, and will write how I&apos;m doing things in that environment.&lt;/p&gt;
&lt;more&gt;&lt;/more&gt;
&lt;p&gt;&lt;strong&gt;Preparing.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In Eclipse, go to &lt;em&gt;File &amp;gt; New &amp;gt; Project&lt;/em&gt;. In the New Project pop-up you&apos;ll now be asked to select a wizard. Select the &amp;quot;Java Project&amp;quot; wizard. In the first step enter the project name &amp;quot;JavaNewbie&amp;quot;. Under Contents keep &amp;quot;Create new project in workspace&amp;quot; selected, and under Project layout keep &amp;quot;Use project folder as root for sources and class files&amp;quot; selected. Click &amp;quot;Finish&amp;quot;. I get a box saying that &amp;quot;The created projects appears to be 5.0, but the project compliance is not yet set to 5.0. Blah, blah....&amp;quot; I don&apos;t know (yet) what this means, so I&apos;ll just keep the default answer and click &amp;quot;Yes&amp;quot;.&lt;/p&gt;
&lt;p&gt;In the Package Explorer you will now have your new project. Right-click the project name and choose &lt;em&gt;New &amp;gt; Class&lt;/em&gt;. In the New Java Class box fill in the name &amp;quot;FirstJava&amp;quot;. In the top you&apos;ll get a warning saying &amp;quot;The use of the default package is discouraged&amp;quot;, but I&apos;m not sure what that means either, so I just click &amp;quot;Finish&amp;quot;.&lt;/p&gt;
&lt;p&gt;You will now see the code, looking like this:&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;public class FirstJava {&lt;br/&gt;
&amp;nbsp;/**&lt;br/&gt;
&amp;nbsp; * @param args&lt;br/&gt;
&amp;nbsp; */&lt;br/&gt;
&amp;nbsp;public static void main(String[] args) {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;// TODO Auto-generated method stub&lt;br/&gt;
&amp;nbsp;}&lt;br/&gt;
}&lt;/div&gt;
&lt;p&gt;To make sure things are working change the code into this:&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;public class FirstJava {&lt;br/&gt;
&amp;nbsp;public static void main(String[] args) {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;System.out.println(&amp;quot;Hello World&amp;quot;);&lt;br/&gt;
&amp;nbsp;}&lt;br/&gt;
}&lt;/div&gt;
&lt;p&gt;Save, and click the &amp;quot;Run&amp;quot; button. You should now get a &amp;quot;Run&amp;quot; dialouge box. You might have to click the &amp;quot;New&amp;quot; button. There&apos;s a lot of stuff here. Just be sure that the Name and Project is &amp;quot;JavaNewbie&amp;quot; and Main class is &amp;quot;FirstJava&amp;quot;. In the Console part at the bottom of the Eclipse workspace you should now see the &amp;quot;Hello World&amp;quot; output.&lt;/p&gt;
&lt;p&gt;If to compare this with a ColdFusion cfc, the &amp;quot;public class FirstJava&amp;quot; is can roughly be translated into &amp;lt;cfcomponent displayname=&amp;quot;FirstJava&amp;quot;&amp;gt; and &amp;quot;public static void main(String[] args)&amp;quot; can roughly be translated to &amp;lt;cffunction name=&amp;quot;main&amp;quot; access=&amp;quot;public&amp;quot; returntype=&amp;quot;void&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating a GUI&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We need to import the main Swing package so at the very top put this line:&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;import javax.swing.*;&lt;/div&gt;
&lt;p&gt;Now we will make a function for creating and showing our very first GUI. Put the following above the main function.&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;private static void createAndShowGUI() {&lt;br/&gt;
&lt;br/&gt;
}&lt;/div&gt;
&lt;p&gt;Now within the braces of our new function we will put some lines:&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;//Create and set up the window.&lt;br/&gt;
JFrame frame = new JFrame(&amp;quot;My first swing&amp;quot;);&lt;br/&gt;
&lt;br/&gt;
//Add the ubiquitous &amp;quot;Hello World&amp;quot; label.&lt;br/&gt;
JLabel label = new JLabel(&amp;quot;Hello World&amp;quot;);&lt;br/&gt;
frame.getContentPane().add(label);&lt;br/&gt;
&lt;br/&gt;
//Display the window.&lt;br/&gt;
frame.pack();&lt;br/&gt;
frame.setVisible(true);&lt;font color=&quot;#808080&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;/font&gt;&lt;/div&gt;
&lt;p&gt;And last change change the&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;System.out.println(&lt;b&gt;&lt;i&gt;&amp;quot;&lt;/i&gt;&lt;/b&gt;Hello World&lt;b&gt;&lt;i&gt;&amp;quot;&lt;/i&gt;&lt;/b&gt;);&lt;/div&gt;
&lt;p&gt;for this ones&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;//creating and showing this application&apos;s GUI.&lt;br/&gt;
javax.swing.SwingUtilities.invokeLater(new Runnable() {&lt;br/&gt;
&amp;nbsp;public void run() {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;createAndShowGUI();&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br/&gt;
});&lt;/div&gt;
&lt;p&gt;Ok, what happens here is that we create a top-level container as an instance of the class JFrame. In this case a frame is a window. Applications with GUI&apos;s would use at least one frame.&lt;/p&gt;
&lt;p&gt;We then add a lable initiating an instance of the class JLabel - and then call a function for adding it to our frame.&lt;/p&gt;
&lt;p&gt;The frame.pack() and frame.setVisisble() functions finalize the code to set up and show the frame/window.&lt;/p&gt;
&lt;p&gt;The last piece of code I found provided as a recomended &amp;quot;use as-is&amp;quot; code for calling the window creating function. We don&apos;t need to understand it, just to know that it ensures that we don&apos;t have a thread-safety problem.&lt;/p&gt;
&lt;p&gt;The final code should look like this:&lt;/p&gt;
&lt;div class=&quot;code&quot;&gt;import javax.swing.*;&lt;br/&gt;
&lt;br/&gt;
public class FirstJava {&lt;br/&gt;
&amp;nbsp;&lt;br/&gt;
&amp;nbsp;private static void createAndShowGUI() {&lt;br/&gt;
&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Create and set up the window.&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; JFrame frame = new JFrame(&amp;quot;My first swing&amp;quot;);&lt;br/&gt;
&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Add the ubiquitous &amp;quot;Hello World&amp;quot; label.&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; JLabel label = new JLabel(&amp;quot;Hello World&amp;quot;);&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frame.getContentPane().add(label);&lt;br/&gt;
&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //Display the window.&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frame.pack();&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; frame.setVisible(true);&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br/&gt;
&amp;nbsp;&lt;br/&gt;
&amp;nbsp;public static void main(String[] args) {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;javax.swing.SwingUtilities.invokeLater(new Runnable() {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void run() {&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; createAndShowGUI();&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br/&gt;
}&lt;font color=&quot;#808080&quot;&gt;&lt;i&gt;&lt;/i&gt;&lt;/font&gt;&lt;/div&gt;
&lt;p&gt;Try to run it now. You should get a window popping up looking like this:&lt;/p&gt;
&lt;p&gt;&lt;img height=&quot;50&quot; alt=&quot;&quot; src=&quot;/UserFiles/Image/j1.gif&quot; width=&quot;123&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Resizing it slightly to see all text like this:&lt;/p&gt;
&lt;p&gt;&lt;img height=&quot;107&quot; alt=&quot;&quot; src=&quot;/UserFiles/Image/j2.gif&quot; width=&quot;252&quot;/&gt;&lt;/p&gt;
				
				</description>
				
				<category>Java for Newbies</category>
				
				<pubDate>Fri, 13 May 2005 00:17:00 -0600</pubDate>
				<guid>http://trond.ulseth.no/index.cfm?mode=entry&amp;entry=D33C4BE4-E2B4-E594-CB6F48E638BA9BEF</guid>
				
				<enclosure url="http://trond.ulseth.no/enclosures/0" length="0" type=""/>
				
			</item>
			
		 	
			</channel></rss>
<SCRIPT language="Javascript">
<!--

// FILE ARCHIVED ON 20080103021532 AND RETRIEVED FROM THE
// INTERNET ARCHIVE ON 20100801044949.
// JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
// ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
// SECTION 108(a)(3)).

   var sWayBackCGI = "http://web.archive.org/web/20080103021532/";

   function xResolveUrl(url) {
      var image = new Image();
      image.src = url;
      return image.src;
   }
   function xLateUrl(aCollection, sProp) {
      var i = 0;
      for(i = 0; i < aCollection.length; i++) {
         var url = aCollection[i][sProp];         if (typeof(url) == "string") { 
          if (url.indexOf("mailto:") == -1 &&
             url.indexOf("javascript:") == -1
             && url.length > 0) {
            if(url.indexOf("http") != 0) {
                url = xResolveUrl(url);
            }
            url = url.replace('.wstub.archive.org','');
            aCollection[i][sProp] = sWayBackCGI + url;
         }
         }
      }
   }

   xLateUrl(document.getElementsByTagName("IMG"),"src");
   xLateUrl(document.getElementsByTagName("A"),"href");
   xLateUrl(document.getElementsByTagName("AREA"),"href");
   xLateUrl(document.getElementsByTagName("OBJECT"),"codebase");
   xLateUrl(document.getElementsByTagName("OBJECT"),"data");
   xLateUrl(document.getElementsByTagName("APPLET"),"codebase");
   xLateUrl(document.getElementsByTagName("APPLET"),"archive");
   xLateUrl(document.getElementsByTagName("EMBED"),"src");
   xLateUrl(document.getElementsByTagName("BODY"),"background");
   xLateUrl(document.getElementsByTagName("TD"),"background");
   xLateUrl(document.getElementsByTagName("INPUT"),"src");
   var forms = document.getElementsByTagName("FORM");
   if (forms) {
       var j = 0;
       for (j = 0; j < forms.length; j++) {
              f = forms[j];
              if (typeof(f.action)  == "string") {
                 if(typeof(f.method)  == "string") {
                     if(typeof(f.method) != "post") {
                        f.action = sWayBackCGI + f.action;
                     }
                  }
              }
        }
    }


//-->
</SCRIPT>

