<?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>cnicollonline &#187; School</title>
	<atom:link href="http://cnicollonline.com/category/school/feed/" rel="self" type="application/rss+xml" />
	<link>http://cnicollonline.com</link>
	<description>&#34;I was just guessing at numbers and figures, pulling the puzzles apart&#34;</description>
	<lastBuildDate>Mon, 23 Aug 2010 19:16:07 +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>Creating a Java UI Part 1</title>
		<link>http://cnicollonline.com/programming/creating-a-java-ui/#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</link>
		<comments>http://cnicollonline.com/programming/creating-a-java-ui/#comments</comments>
		<pubDate>Tue, 12 May 2009 05:47:40 +0000</pubDate>
		<dc:creator>Cosizzle</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Notes]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[School]]></category>
		<category><![CDATA[Action Event]]></category>
		<category><![CDATA[awt]]></category>
		<category><![CDATA[Java Libraries]]></category>
		<category><![CDATA[Menu Bar]]></category>
		<category><![CDATA[Swing]]></category>

		<guid isPermaLink="false">http://cnicollonline.com/?p=121</guid>
		<description><![CDATA[Creating a basic Java UI can be a little overwhelming, but once you understand what&#8217;s going on there is not a lot to it. This post is about the Java User Interface and components within it. Today i&#8217;ll be&#160;covering: Creating a JFrame Configuring the&#160;window Adding a menu bar Adding&#160;mnemonics Adding key&#160;strokes Adding a popup &#8216;about&#8217;&#160;window [...]]]></description>
			<content:encoded><![CDATA[<p><!--<br />
- WORDPRESS TEMPLATE FOR CREATING OR POSTING TO MY BLOG.<br />
- COMMON TAGS TO NOTE:<br />
-<br />
<h1>MAIN&nbsp;TITLE</h1>
<p>-
<ul>
<li>LIST&nbsp;ITEM</li>
</ul>
<p>-
<ol>
<li>ORDER LIST&nbsp;ITEM</li>
</ol>
<p>--></p>
<p>Creating a basic Java UI can be a little overwhelming, but once you understand what&#8217;s going on there is not a lot to it. This post is about the Java User Interface and components within it. Today i&#8217;ll be&nbsp;covering:</p>
<ul>
<li>Creating a JFrame
<ul>
<li>Configuring the&nbsp;window</li>
</ul>
</li>
<li>Adding a menu bar
<ul>
<li>Adding&nbsp;mnemonics</li>
<li>Adding key&nbsp;strokes</li>
<li>Adding a popup &#8216;about&#8217;&nbsp;window</li>
</ul>
</li>
<p>	<!--</p>
<li>Layout Management
<ul>
<li>Creating&nbsp;tabs</li>
<li>Adding content to each&nbsp;tab</li>
</ul>
</li>
<p>    -->
</ul>
<p><span id="more-121"></span></p>
<h2>File&nbsp;Setup</h2>
<p>The following Java libraries that are used for this section&nbsp;are:</p>
<ul>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html" target="_blank">JFrame</a></li>
</ul>
<p>To start the project create the project folder with a bin and src folder. Once setup create the default package which will be used to compile and run the application. In the package create <em>MainDisplay.java</em> This will be the class which runs the&nbsp;application.</p>
<pre class="brush: java;">
/**
 * Project: cnicoll_javaUI
 * File: MainDisplay.java
 * Date: May 6, 2009
 * Time: 11:26:07 AM
 */ 

package cnicoll;

import javax.swing.JFrame;

import cnicoll.ui.MainFrame;

/**
 * @author cNicoll
 *
 */
public class MainDisplay {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
</pre>
<h2>Building the MainFrame&nbsp;(JFrame)</h2>
<p>Now that we have the basic structure, create a new extended package that will contain the ui. Within this package create a new class called MainFrame.java and have it extend the JFrame class. MainFrame is a class the will be, well, the main frame of the application. It will be a container which will hold all our application elements and&nbsp;components.</p>
<pre class="brush: java;">
/**
 * Project: cnicoll_javaUI
 * File: MainFrame.java
 * Date: May 6, 2009
 * Time: 11:46:14 AM
 */ 

package cnicoll.ui;

import javax.swing.JFrame;

/**
 * @author cNicoll
 *
 */
public class MainFrame extends JFrame {
	// Instance Variables

	/*
	 * Class Constructor
	 */
	public MainFrame() {
		// Window Name
		super(&quot;My Demo Application&quot;);
	}
}
</pre>
<p>Not much here is going&nbsp;on.</p>
<pre class="brush: java; toolbar: false;">super(&quot;My Demo Application&quot;);</pre>
<p>Gives a title to the window. Lets add some more properties to the mainframe, by adding the following below the super call. For further explanation read the Java&nbsp;comments.</p>
<pre class="brush: java;">
// call the method and change the width and height.
setSize(700, 600);
// exit the application on the window close
setDefaultCloseOperation(JFrame.EXITONCLOSE);
// disable window resizing
setResizable(false);
// centers window
setLocationRelativeTo(null);
</pre>
<p>Next go back to the <em>MainDisplay.java</em> class and create an object that will create the MainFrame when the application is run. <strong>NOTE: Also import the class ui package</strong>. Your <em>MainDisplay.java</em> should now look like&nbsp;this:</p>
<pre class="brush: java;">
/**
 * Project: cnicoll_javaUI
 * File: MainDisplay.java
 * Date: May 6, 2009
 * Time: 11:26:07 AM
 */ 

package cnicoll;

import javax.swing.JFrame;

import cnicoll.ui.MainFrame;

/**
 * @author cNicoll
 *
 */
public class MainDisplay {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame _frame = new MainFrame();
		// show the frame
		_frame.setVisible(true);

	}

}
</pre>
<p>Run the <em>MainDisplay.java</em> class and you should&nbsp;see:</p>
<div class="tutorial_image"><img src="http://farm4.static.flickr.com/3373/3507634409_3e375f0553.jpg" border="0" alt="050609_12185" width="500" height="429" /></div>
<p>Thats about it when it comes to setting up the main frame. Of course a lot more needs to be done which we will get to, but in order to create a simple&nbsp;JFrame.</p>
<ol>
<li>Set up your application working&nbsp;directory</li>
<li>Create a new package with a single class that will hold the main string&nbsp;method</li>
<li>Create a new package that will be used for your ui. In this package create a new class called&nbsp;MainFrame.ui</li>
<li>Extend the JFrame class and assign the window&nbsp;properties</li>
<li style="list-style: none">
<pre class="brush: java;">
	public MainFrame() {
		// Window Name
		super(&quot;My Demo Application&quot;);
		// call the method and change the width and height.
		setSize(700, 600);
		// exit the application on the window close
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// disable window resizing
		setResizable(false);
		// centers window
		setLocationRelativeTo(null);
	}
	</pre>
</li>
<li>Create the object within your MainDisplay&nbsp;class</li>
</ol>
<h4>At this&nbsp;point:</h4>
<ul>
<li><em>MainDisplay.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
	/**
 * Project: cnicoll_javaUI
 * File: MainDisplay.java
 * Date: May 6, 2009
 * Time: 11:26:07 AM
 */ 

package cnicoll;

import javax.swing.JFrame;

import cnicoll.ui.MainFrame;

/**
 * @author cNicoll, A00572953
 *
 */
public class MainDisplay {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame _frame = new MainFrame();
		// show the frame
		_frame.setVisible(true);

	}

}
	</pre>
</li>
<li><em>MainFrame.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
/**
 * Project: cnicoll_javaUI
 * File: MainFrame.java
 * Date: May 6, 2009
 * Time: 11:46:14 AM
 */ 

package cnicoll.ui;

import javax.swing.JFrame;

/**
 * @author cNicoll
 *
 */
public class MainFrame extends JFrame {
	// Instance Variables

	/*
	 * Class Constructor
	 */
	public MainFrame() {
		// Window Name
		super(&quot;My Demo Application&quot;);
		// call the method and change the width and height.
		setSize(700, 600);
		// exit the application on the window close
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// disable window resizing
		setResizable(false);
		// centers window
		setLocationRelativeTo(null);
	}
}
</pre>
</li>
</ul>
<h2>Adding a menu&nbsp;bar</h2>
<p>The following Java libraries that are used for this section&nbsp;are:</p>
<ul>
<li><a href="java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionEvent.html#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" target="_blank">ActionEvent</a></li>
<li><a href="java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionListener.html#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" target="_blank">ActionListener</a></li>
<li><a href="java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" target="_blank">KeyEvent</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html" target="_blank">JFrame</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JMenu.html" target="_blank">JMenu</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JMenuBar.html" target="_blank">JMenuBar</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JMenuItem.html" target="_blank">JMenuItem</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/KeyStroke.html" target="_blank">KeyStroke</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/SwingUtilities.html" target="_blank">SwingUtilities</a></li>
<li><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/UIManager.html" target="_blank">UIManager</a></li>
</ul>
<p>A menu bar almost every application will use, and with every menu bar there are certain expectations to be met such as names, mnemonics, keystrokes and the display. To start create a new java class within your ui package and give it the name of <em>MainMenuBar.java .</em> Similar to what we did to the MainFrame we are going to extend this class out to <span style="text-decoration: line-through;">JFrame</span> JMenuBar. Your class should now look like&nbsp;this:</p>
<pre class="brush: java;">
/**
 * Project: cnicoll_javaUI
 * File: MainMenuBar.java
 * Date: May 6, 2009
 * Time: 2:52:21 PM
 */ 

package cnicoll.ui;

import javax.swing.JMenuBar;

/**
 * @author cNicoll
 *
 */
public class MainMenuBar extends JMenuBar {
	// Instance Variables

	/*
	 * Class Constructor
	 */
	public void MainMenuBar() {

	}
}
</pre>
<p>Next define the instance variables that will be used within the menu&nbsp;bar.</p>
<pre class="brush: java;">
// Instance Variables
private JMenu _file;
private JMenu _lookAndFeel;
private JMenu _help;
</pre>
<p>Also, create the menu item object within the&nbsp;constructor</p>
<pre class="brush: java;">
public void MainMenuBar() {
	// File Menu
	_file = new JMenu(&quot;File&quot;);
	add(_file);

	// Look and Feel Menu
	_lookAndFeel = new JMenu(&quot;Look &amp; Feel&quot;);
	add(_lookAndFeel);

	// Help Menu
	_help = new JMenu(&quot;Help&quot;);
	add(_help);
}
</pre>
<p>Now, that creates the menu bar, and adds menu items to it - however we have to set the menu bar to our main frame. To do&nbsp;this:</p>
<ol>
<li>Modify your instance Variables and add a frame object&nbsp;reference</li>
<li style="list-style: none">
<pre class="brush: java;">
	// Instance Variables

	// Adding the frame variable
	private final JFrame _frame;
	// What we had before
	private JMenu _file;
	private JMenu _lookAndFeel;
	private JMenu _help;
	</pre>
</li>
<li>Create a object and a value for the menu at the top of the constructor <strong>NOTE: You will need to import the JFrame&nbsp;class</strong></li>
<li style="list-style: none">
<pre class="brush: java; toolbar: false;">
	public MainMenuBar(JFrame frame) {
		this._frame = frame;
		//...
</pre>
</li>
<li>Set the menu at the bottom of the&nbsp;constructor</li>
<li style="list-style: none">
<pre class="brush: java; toolbar: false;">
		//...
		frame.setJMenuBar(this);
	}
	</pre>
</li>
<li>within the constructor of <em>MainFrame.java</em> add the following code at the end. This will create the menubar object within the MainFrame&nbsp;class</li>
<li style="list-style: none">
<pre class="brush: java; toolbar: false;">
	//...
	// add the menu bar
	menubar = new MainMenuBar(this);
	</pre>
</li>
</ol>
<p>Go ahead and compile and test. When you it runs, your MainFrame window should now look like&nbsp;this:</p>
<div class="tutorial_image"><img src="http://farm4.static.flickr.com/3655/3511351464_5a28e4c5fd.jpg?=0" border="0" alt="050609_12185" width="500" height="429" /></div>
<h4>At this&nbsp;point:</h4>
<ul>
<li><em>MainDisplay.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
/**
 * Project: cnicoll_javaUI
 * File: MainDisplay.java
 * Date: May 6, 2009
 * Time: 11:26:07 AM
 */ 

package cnicoll;

import javax.swing.JFrame;

import cnicoll.ui.MainFrame;

/**
 * @author cNicoll
 *
 */
public class MainDisplay {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame _frame = new MainFrame();
		// show the frame
		_frame.setVisible(true);

	}

}
</pre>
</li>
<li><em>MainFrame.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
/**
* Project: cnicoll_javaUI
* File: MainFrame.java
* Date: May 6, 2009
* Time: 11:46:14 AM
*/

package cnicoll.ui;

import javax.swing.JFrame;

/**
* @author cNicoll
*
*/
public class MainFrame extends JFrame {
// Instance Variables

/*
* Class Constructor
*/
public MainFrame() {
// Window Name
super(&quot;My Demo Application&quot;);
// call the method and change the width and height.
setSize(700, 600);
// exit the application on the window close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// disable window resizing
setResizable(false);
// centers window
setLocationRelativeTo(null);
}
}
</pre>
</li>
<li><em>MainMenuBar.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
/**
 * Project: cnicoll_javaUI
 * File: MainMenuBar.java
 * Date: May 6, 2009
 * Time: 2:52:21 PM
 */ 

package cnicoll.ui;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**
 * @author cNicoll
 *
 */
public class MainMenuBar extends JMenuBar {
	// Instance Variables
	private final JFrame _frame;
	private JMenu _file;
	private JMenu _lookAndFeel;
	private JMenu _help;

	/*
	 * Class Constructor
	 */
	public MainMenuBar(JFrame frame) {
		// Create the frame object
		this._frame = frame;

		// File Menu
		_file = new JMenu(&quot;File&quot;);
		add(_file);

		// Look and Feel Menu
		_lookAndFeel = new JMenu(&quot;Look &amp; Feel&quot;);
		add(_lookAndFeel);

		// Help Menu
		_help = new JMenu(&quot;Help&quot;);
		add(_help);

		// Set the menu bar
		frame.setJMenuBar(this);
	}

}
</pre>
</li>
</ul>
<h3>Adding menu items and key strokes to the&nbsp;menubar</h3>
<p>We have our frame, we have a menu bar within the frame, and it looks pretty snazzy, but&#8230; It doesnt do a whole lot. In this section I&#8217;ll be adding menu items to the menus as well as adding mnemonics and&nbsp;keystrokes.</p>
<p>Open <em>MainMenuBar.java</em> and add the following instance&nbsp;variables:</p>
<pre class="brush: java; toolbar: false;">
//...
private JMenuItem _exit;
private JMenuItem _about;
</pre>
<p>And within the class constructor of <em>MainMenuBar.java</em> add the following to the pre-existing&nbsp;code.</p>
<pre class="brush: java;">
// File Menu
_file = new JMenu(&quot;File&quot;);
_file.setMnemonic('F');
add(_file);
_exit = new JMenuItem(&quot;Exit&quot;, 'x');
_file.add(_exit);
</pre>
<pre class="brush: java;">
// Help Menu
_help = new JMenu(&quot;Help&quot;);
_help.setMnemonic('H');
add(_help);
_about = new JMenuItem(&quot;About&quot;, 'A');
_about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
_help.add(_about);
</pre>
<p>Go ahead and test this, you will now see drop downs within your&nbsp;menus.</p>
<p>Now for the big bit of code which will add an ActionListeners to tell &#8216;exit&#8217; to quit the program, also create a popup for the about window. Add this to <em>MainFrame.java</em>. The reason I put this within the MainFrame class is because of the MVC design&nbsp;pattern.</p>
<pre class="brush: java;">
menubar.exit.addActionListener(new ActionListener() {

	public void actionPerformed(ActionEvent e) {
		System.exit(0);
	}
});

menubar.about.addActionListener(new ActionListener() {

	public void actionPerformed(ActionEvent e) {
		JOptionPane.showMessageDialog(MainFrame.this, &quot;My own pop up window\nBy cNicoll&quot;, &quot;My Pop Up Window&quot;,
				JOptionPane.INFORMATION_MESSAGE);
	}
});
</pre>
<p>Finally i&#8217;ll add the Java GUI&#8217;s to the &#8216;Look and Feel&#8217; menu. I&#8217;m not going to into detail of how it works. Basically it Java has pre-built in GUI&#8217;s, this will display them and allow for you to change&nbsp;them.</p>
<p>First off, add the following to the look and feel&nbsp;menu.</p>
<pre class="brush: java;">
// add the look and feel GUIs
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
for (UIManager.LookAndFeelInfo info : infos) {
	makeMenuItem(info.getName(), info.getClassName());
}
</pre>
<p>Next add a new method within the same&nbsp;class.</p>
<pre class="brush: java;">
/**
 * Makes a button to change the pluggable look and feel.
 *
 * @param name
 *            the button name
 * @param plafName
 *            the name of the look and feel class
 */
private void makeMenuItem(String name, final String plafName) {
	// add button to panel

	JMenuItem item = new JMenuItem(name);
	lookAndFeel.add(item);

	// set button action
	item.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent event) {
			// button action: switch to the new look and feel
			try {
				UIManager.setLookAndFeel(plafName);
				SwingUtilities.updateComponentTreeUI(MainMenuBar.this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	});
}
</pre>
<p>After you compile and run the application you should have something looking like&nbsp;this.</p>
<div class="tutorial_image"><img src="http://farm4.static.flickr.com/3347/3511705320_57e28e1bd5.jpg?v=0" width="500" height="429" /></div>
<h4>At this&nbsp;point:</h4>
<ul>
<li><em>MainDisplay.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
/**
 * Project: cnicoll_javaUI
 * File: MainDisplay.java
 * Date: May 6, 2009
 * Time: 11:26:07 AM
 */ 

package cnicoll;

import javax.swing.JFrame;

import cnicoll.ui.MainFrame;

/**
 * @author cNicoll
 *
 */
public class MainDisplay {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame _frame = new MainFrame();
		// show the frame
		_frame.setVisible(true);

	}

}
	</pre>
</li>
<li><em>MainFrame.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
	/**
 * Project: cnicoll_javaUI
 * File: MainFrame.java
 * Date: May 6, 2009
 * Time: 11:46:14 AM
 */ 

package cnicoll.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * @author cNicoll
 *
 */
public class MainFrame extends JFrame {
	// Instance Variables
	private final MainMenuBar menubar;

	/*
	 * Class Constructor
	 */
	public MainFrame() {
		// Window Name
		super(&quot;My Demo Application&quot;);
		// call the method and change the width and height.
		setSize(700, 600);
		// exit the application on the window close
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// disable window resizing
		setResizable(false);
		// centers window
		setLocationRelativeTo(null);

		// add the menu bar
		menubar = new MainMenuBar(this);

		menubar._exit.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});

		menubar._about.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(MainFrame.this, &quot;My own pop up window\nBy cNicoll&quot;, &quot;My Pop Up Window&quot;,
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
	}
}
</pre>
</li>
<li><em>MainMenuBar.java</em> should look&nbsp;like:</li>
<li style="list-style: none">
<pre class="brush: java; collapse: true; light: false; toolbar: true;">
	/**
 * Project: cnicoll_javaUI
 * File: MainMenuBar.java
 * Date: May 6, 2009
 * Time: 2:52:21 PM
 */ 

package cnicoll.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 * @author cNicoll
 *
 */
public class MainMenuBar extends JMenuBar {
	// Instance Variables
	private final JFrame _frame;
	private JMenu _file;
	JMenuItem _exit;
	private JMenu _lookAndFeel;
	private JMenu _help;
	JMenuItem _about;

	/*
	 * Class Constructor
	 */
	public MainMenuBar(JFrame frame) {
		// Create the frame object
		this._frame = frame;

		// File Menu
		_file = new JMenu(&quot;File&quot;);
		_file.setMnemonic('F');
		add(_file);
		_exit = new JMenuItem(&quot;Exit&quot;, 'x');
		_file.add(_exit);

		// Look and Feel Menu
		_lookAndFeel = new JMenu(&quot;Look &amp; Feel&quot;);
		add(_lookAndFeel);
		// add the look and feel GUIs
		UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
		for (UIManager.LookAndFeelInfo info : infos) {
			makeMenuItem(info.getName(), info.getClassName());
		}

		// Help Menu
		_help = new JMenu(&quot;Help&quot;);
		_help.setMnemonic('H');
		add(_help);
		_about = new JMenuItem(&quot;About&quot;, 'A');
		_about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
		_help.add(_about);

		// Set the menu bar
		frame.setJMenuBar(this);
	}

	/**
	 * Makes a button to change the pluggable look and feel.
	 *
	 * @param name
	 *            the button name
	 * @param plafName
	 *            the name of the look and feel class
	 */
	private void makeMenuItem(String name, final String plafName) {
		// add button to panel

		JMenuItem item = new JMenuItem(name);
		_lookAndFeel.add(item);

		// set button action
		item.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent event) {
				// button action: switch to the new look and feel
				try {
					UIManager.setLookAndFeel(plafName);
					SwingUtilities.updateComponentTreeUI(MainMenuBar.this);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
	</pre>
</li>
</ul>
<h2>Conclusion</h2>
<p>It seems like a lot but in the end its really not much, theres a lot more we can do - but all the hard work is done. In my next entry I plan on picking up from this and adding tabs to it. To download this project please <a href="http://www.cnicollonline.com/wp-articles/051209_120017_Creating_a_Java_UI/cnicoll_javaUI.zip" target="_self">click&nbsp;here</a>.</p>
<hr />
]]></content:encoded>
			<wfw:commentRss>http://cnicollonline.com/programming/creating-a-java-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

