Section 6.7. Putting Your IDE to Work

   

6.7 Putting Your IDE to Work

Please do not allow something that has been mentioned in this chapter as a major feature of one IDE to sway you entirely toward that product. Almost all of the features mentioned here are shared among the different IDEs . Sometimes they are easier to use, more prominent, or more powerful. They all have debuggers . They all have inspectors. They all have wizards. In the end, choosing an IDE will be entirely a matter of personal preferencewhat others around you are using, how serious you think you'll need to be, and what your budget is.

In the next couple of sections, we will take a quick look at some common things you need to do to work in these various IDEs, such as setting up a project, using the built-in templates to reduce the amount of code writing you need to do, and working with the interactive debugger.

6.7.1 Getting Started with a Project in IntelliJ IDEA

Once you have downloaded and unpacked the compressed IDEA file, you'll want to create a folder to extract it to (strangely, the decompressor won't create one for you). Call it IntelliJ_IDEA . Go into the <IDEA_HOME>/bin folder to find the executable idea.bat file. Double clicking here will launch the program. The first time you run the program, it will ask for your username, which you are assigned when you purchase IDEA. Enter this and the key number, and you will be prompted to set up a project.

Now let's create a project that we can use to store the programs we'll write in the remainder of this book.

  1. Enter a name for the project. Type JavaForCF .

  2. The default location for the project will be the name of the project as an .ipr file. For instance, if you've installed IDEA in a folder under your root directory on Windows, this will be C:\Intellij_IDEA\Java-ForCF.ipr .

  3. You can change the JDK you want to use with IDEA in the JDK field. Older versions of IDEA will not work well with JDK 1.4; you can specify another JDK here if you wish.

  4. Select the compiler output path . When you write source files, the compiler can place them in a different directory from the source files. This is recommended so that when you deploy your application you don't send the source with them. It also helps keep things organized. Choose C:\IntelliJ_IDEA\JavaForCF , then we'll make a separate package as we move through each chapter. You'll want to make sure you package your files, which we'll start doing in the next chapter, so that your classes are organized in that directory.

  5. Next, we specify the project path. This is where the working files for the project will be. In general, you'll want to store your source files somewhere other than under the JDK directory. For now, we want to have the source files next to the class files. The project path by default will have the path name of the directory into which you installed IDEA. If you created a folder called IntelliJ_IDEA in the Windows root, the default value is C:\IntelliJ_IDEA . Let's specify a subfolder for this project. Click the ellipses ( ... ) button next to the Project Path field to get a file browser. Find your <IDEA_HOME> folder, right click on it, and choose New Folder. Type JavaForCF , then click OK.

  6. The next screen informs us of the locations for source paths for this project. The JDK is included here, as is the directory we just created with a subdirectory called src for "source". Click Next, and allow IntelliJ to create the src folder if it asks to. So, you'll write all of your source files for Java classes in <IDEA_HOME>\JavaForCF\src . For me, that is C:\IntelliJ_IDEA\JavaForCF\src . Click Next.

  7. This step asks you to specify the class path entries where other Java class files required for your project can be found. When you write an extensive Java application, you might require, for instance, a particular XML parser or a tag library created by a third party. These are usually packaged into .jar (Java archive) files, which use the same compression algorithm as .zip files, and are then distributed. You can place such .jar files directly under your JDK. This is an easy way to add extensions to your projects. You don't have to modify classpath settings, and they can be used by any application you make. This is the reason that you see all these .jar files in this step of the project setup wizard under <JAVA_HOME>/lib/ext/ . Right now, we don't have any other .jar s to add, so just click Finish. IntelliJ will create the project and show you a tip of the day.

Now your project is set up in IntelliJ, and you can start working. Click the Project tab, which will reveal your project directory structure. Let's do a test file quickly to make sure that all of the paths are correctly resolving and that you can compile and view the results of your work.

You should see your project tree in the left-hand side of the screen in the Project tab. Right click on the folder in which you want to place your class file or create a new folder.

Let's make a folder called chp6 by right-clicking on the <IDEA_HOME>\JavaForCF\src folder. Choose New > Folder from the context menu and name it chp6 . Then, right click on your new chp6 folder. Choose New > Class. You will be prompted for the class name. Type TryIdea and click OK. Something like the following code is created for you:

 /*   * Created by IntelliJ IDEA.  * User: eben  * Time: 11:33:04 PM  * To change template for new class use  * Code Style  Class Templates options (Tools  IDE Options).  */ package chp6; public class TryIdea { } 

Notice that the package name has automatically been created for you. Now you can enter the data for your class.

Inside the class curly braces, write a main method, type System.out , and wait one second. You will see a menu pop up with all of the available methods of the out object. This is similar to the Tag Insight function you're used to in ColdFusion Studio and HomeSite. Choose println() and type a message to print. Now compile your test program by clicking the icons with the 0s and 1s in it. This will compile all modified files in a project. If you are not sure what a button does, mouse over it for a moment, and you'll see a tip in the bottom left-hand corner of the screen. IDEA will compile your class and display any errors in a bottom window.

To run your project, click the green arrow. If you have not yet compiled your class, this will do it for you.

Note

You may have difficulty once you have multiple, unrelated example files set up in the same project. That is because the main method is the entry point for a Java programno matter if it is one file or one thousand files. The project will (rightly) assume that the files in packages under the same directory should all belong to one application.


You should be asked to create a default application configuration. Just click the + button and choose the file in which the main method resides. Right now, this is chp6.TryIdea . You can specify additional parameters for the Virtual Machine here, and in the Program Parameters you can supply any arguments to send to the main method. After you have specified the file containing the main method, click Run, and you will see the output of the program at the bottom of the screen.

My program just prints Hello, sweetheart! , so my output looks like this:

[View full width]
 
[View full width]
C:\jdk1.4\bin\javaw.exe-classpath C:\jdk1.4\jre\lib\rt.jar;C:\jdk1.4\jre\lib\ext\dnsns. graphics/ccc.gif jar;C:\jdk1.4\jre\lib\ext\ldapsec.jar;C:\jdk1.4\jre\lib\ext\localedata.jar;C:\jdk1.4\jre\ graphics/ccc.gif lib\ext\sunjce_provider.jar;C:\IntelliJ_IDEA\JavaForCF chp6.TryIdea Hello, sweetheart! Process terminated with exit code 0

If you see something similar, you're good to go. If you get errors, check your program syntax. Check that you have correctly set up the project and that you have specified the correct JDK. Modify any project-specific settings, including the JDK being used, in File > Project Properties.

6.7.2 Setting Up a Project in JBuilder

In order to work in JBuilder, you need to create a project. In this section, we will see how to do this.

To create a new project, choose File > New Project from the main menu. You will begin a wizard. Type JavaForCF for the project name. Choose the home directory to be <JBUILDER_HOME>/JavaForCF . Click Next.

You can use the JDK that ships with JBuilder or specify a newer JDK to use. The output paths should be populated more or less like this:

 JDK: java 1.3.1 -b24  Output Path: C:/JBuilder6/JavaForCF/classes Backup Path: C:/JBuilder6/JavaForCF/bak Working Directory: C:/JBuilder6/JavaForCF 

Click Next to get to the final step, which allows you to specify general project settings. You can add settings such as the name you would like to appear in the JavaDoc comments generated at the top of the source code. Click Finish and you're off.

To test if everything is pointing in the right direction, let's write a quick test file. But this time, instead of writing a little Hello World file like we did with IDEA, let's use the Design View feature of JBuilder to generate a little GUI interface without writing any code.

6.7.3 Creating a File in JBuilder

With the JavaForCF project open , click the white paper icon to create a new file. The Object Gallery will appear, which allows you to choose among different files you can create. You are pretty much limited to class files and applets when using the Personal Edition. Choose Class File, and a wizard appears.

Choose a package for the class. Because we have not created any subfolders , we should specify javaforcf.chp6 as the package. The default (project) package is javaforcf . Adding the chp6 package in our code will automatically create this folder for you when you compile the class.

In the Class Name field, type TryJBuilder .

A Base Class of java.lang.Object is fine.

In the options area, choose the options you want for this class. I will leave all of the default options, such as Public, Generate main Method, Generate Header Comments, and so on, checked. Click OK and the class code will be started for you. JBuilder 6 creates this code:

 package javaforcf;  /**  * <p>Title: </p>  * <p>Description: </p>  * <p>Copyright: Copyright (c) 2002</p>  * <p>Company: </p>  * @author E Hewitt  * @version 1.0  */ public class TryJBuilder {   public TryJBuilder() {   }   public static void main(String[] args) {     TryJBuilder tryJBuilder1 = new TryJBuilder();   } } 

As you can see, this is rather more code generated than what IDEA made for us, which may or may not be useful, depending on what you're doing. Now we have this basis for a class.

6.7.4 Creating a New Application

JBuilder makes it easy to start creating applications that consist of GUI components . An application needs a window in which to reside, and it can be complicated code to write, but it's rather standard stuff once you've done it a couple of times. So let's start by having JBuilder do it for us first, then we'll know how it's done.

To create a new application, click File > New and choose the Application icon. This starts the New Application Wizard.

First we are asked to enter the application class details. Once we have completed the wizard, the application will consist of the main application class and a Frame class. Then we'll use the visual designer to add elements to it.

Enter javaforcf.chp6 in the Package Name field.

Next, enter TryJBuilder for the Class field value. Click Next.

In the last step, the wizard asks us for details regarding the Frame class, as shown in Figure 6.6. We can leave the class name Frame1 and enter My GUI App in the Title field. In the options area, you'll see several check boxes asking you what components you would like JBuilder to generate the code for. Let's choose all of them: Generate Menu Bar, Generate Toolbar, Generate Status Bar, and Generate About Dialog. Center Frame On Screen is already checked. Click Finish.

Figure 6.6. The JBuilder Application Wizard.

graphics/06fig06.jpg

After a quick moment, the IDE will return to source editor mode, with the Structure Pane showing all of the fields and methods it created, as shown in Figure 6.7. Click on an item in the Structure Pane, and the relevant code will be highlighted in the editor. You'll notice too that a few icon .gif files were automatically written for you and used in the program.

Figure 6.7. JBuilder editor after the code has been generated.

graphics/06fig07.jpg

Here is the code that JBuilder generated in the two-step wizard. This not only illustrates how much tedious work JBuilder can take care of for you, but it shows you the amount and nature of code that goes into creating a GUI application in Java. You can use this as a basic framework for creating user interfaces in Java programs.

6.7.5 Frame1.java

[View full width]
 
[View full width]
package javaforcf.chp6; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: </p> * @author E Hewitt * @version 1.0 */ public class Frame1 extends JFrame { JPanel contentPane; JMenuBar jMenuBar1 = new JMenuBar(); JMenu jMenuFile = new JMenu(); JMenuItem jMenuFileExit = new JMenuItem(); JMenu jMenuHelp = new JMenu(); JMenuItem jMenuHelpAbout = new JMenuItem(); JToolBar jToolBar = new JToolBar(); JButton jButton1 = new JButton(); JButton jButton2 = new JButton(); JButton jButton3 = new JButton(); ImageIcon image1; ImageIcon image2; ImageIcon image3; JLabel statusBar = new JLabel(); BorderLayout borderLayout1 = new BorderLayout(); //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { image1 = new ImageIcon(javaforcf.chp6.Frame1.class.getResource("openFile. gif")); image2 = new ImageIcon(javaforcf.chp6.Frame1.class.getResource("closeFile. gif")); image3 = new ImageIcon(javaforcf.chp6.Frame1.class.getResource("help.gif")); // setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your graphics/ccc.gif Icon]"))); contentPane = (JPanel) this.getContentPane(); contentPane.setLayout(borderLayout1); this.setSize(new Dimension(400, 300)); this.setTitle("My GUI App"); statusBar.setText(" "); jMenuFile.setText("File"); jMenuFileExit.setText("Exit"); jMenuFileExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuFileExit_actionPerformed(e); } }); jMenuHelp.setText("Help"); jMenuHelpAbout.setText("About"); jMenuHelpAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jMenuHelpAbout_actionPerformed(e); } }); jButton1.setIcon(image1); jButton1.setToolTipText("Open File"); jButton2.setIcon(image2); jButton2.setToolTipText("Close File"); jButton3.setIcon(image3); jButton3.setToolTipText("Help"); jToolBar.add(jButton1); jToolBar.add(jButton2); jToolBar.add(jButton3); jMenuFile.add(jMenuFileExit); jMenuHelp.add(jMenuHelpAbout); jMenuBar1.add(jMenuFile); jMenuBar1.add(jMenuHelp); this.setJMenuBar(jMenuBar1); contentPane.add(jToolBar, BorderLayout.NORTH); contentPane.add(statusBar, BorderLayout.SOUTH); } //File Exit action performed public void jMenuFileExit_actionPerformed(ActionEvent e) { System.exit(0); } //Help About action performed public void jMenuHelpAbout_actionPerformed(ActionEvent e) { Frame1_AboutBox dlg = new Frame1_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - graphics/ccc.gif dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show(); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { jMenuFileExit_actionPerformed(null); } } }

The code and comments are all JBuilder's. Now try running the program to see how it works. To run the program, click the green arrow with the tool tip that reads Run Project. The running program is shown in Figure 6.8.

Figure 6.8. Result of executing the Frame1 application.

graphics/06fig08.gif

You'll notice that a menu bar is generated that holds two options: File and Help. Each of these holds one option, respectively: Exit, which closes the application, and About, which displays information from the comments.

Then there are three buttons with tool tips that explain what each does. Clicking on these buttons has no effect at this pointyou have to add code to make them do something.

You will notice too that the window has the look and feel of whatever platform you're on. This figure was generated on a Windows XP machine, so yours may have a different look to it.

As an exercise, try changing some of the text on the JButtons and run the program again.

Choose File > Exit. The application window will close and the program will stop running.

6.7.6 Using Design View in JBuilder

At the bottom of your open source file you should see a few tabs: Source, Design, Bean, Doc, and History. These are the View tabs. Choose the Design tab.

At the top of the editor now are new toolbars that will allow us to easily add components (such as buttons) to our program. Many of these, such as the XML, EJB, and Database, tabs are reserved for the Enterprise version of the product. We will use the Swing tab to test it.

The Java Swing library is relatively new, in some ways replacing the older AWT (Abstract Windowing Toolkit). It comprises a good deal of GUI components such as buttons, text fields, and labels. We will discuss Swing in a later chapter so you can write it by hand. For now, we will just add a simple button to our program to see how Design View works and what kind of code it generates.

Note

You must have defined a frame (window) before you can really use Design View to much advantage. You can add a button to your program with no frame defined, and it will compile and run without error, but you won't see anything. Don't worry about frames and buttons at this pointwe're just demonstrating certain aspects of JBuilder right now, with the side benefit of getting a sneak preview at more exciting aspects of Java programming.


Choose the first button in the Swing menu, which is labeled OK. Holding your mouse over the button will reveal a tool tip stating that this component is of type javax.swing.JButton .

Note

javax means Java extensions. Swing components were originally made available as an optional extension to the regular Java libraries. They are now in the core libraries.


Click the JButton button and move your mouse into the main frame. The mouse will become a crosshair that you can use to draw the button.

Immediately to the right of the Help button that JBuilder made for us, draw a small square.

The Property Inspector should be on the right-hand side of your screen. We will use this to edit certain properties.

First, in the Name field, type msg and hit Enter. This will be our Message button. In the Property Inspector, scroll down to the Text field and type message .

Next, in the Title field, type A Button Program and hit Enter. This will change the relevant source code line to this.setTitle("A Button Program"); .

Click the Events tab in the Property Inspector. We want to make something happen when the button is clicked. Right now, the button will appear, but nothing will happen when it is clicked. Edit the mouseClicked field, and a default event that should fire when the mouse is clicked will be named msg_mouseClicked . Hit Enter.

Now we'll turn to the source code by clicking the Source tab. The skeleton for the method has been added, but there is no functionality yet. So find the place in the code where the Property Inspector added your mouse event method. It will look like this:

 void msg_mouseClicked(MouseEvent e) { } 

Inside the msg_mouseClicked method we will write the code for what should happen when the button is clicked. Type the following code so that your method looks like this:

 void msg_mouseClicked(MouseEvent e) {     Graphics g = getGraphics();     g.drawString("Hello, sweetheart!", 150, 150);     g.dispose(); } 

Now run your program again. Running the program saves the file, compiles it, and executes the class. When you click the button, a message will appear, as shown in Figure 6.9. Choose File > Exit to end the program.

Figure 6.9. The final button program once the button has been clicked.

graphics/06fig09.gif

6.7.7 IDE Debugging in Forte

Forte, like other the other editors discussed here, has a debugger that works quite well. You are likely familiar with this process from using ColdFusion Studio's debugger. You will be relieved to find that the terminology is the same and that debugging Java programs is a very similar process.

You can inspect variables , set breakpoints, and step through your program. To set a breakpoint in Forte, go to Debug > Add Breakpoint. To set a variable value for a watch, go to Debug > Add Watch. Then type the name of the variable you want to see, and it shows up in the watch window. To step through an application, go to Debug > Trace Over or Debug > Trace Into. The Forte debugger is shown in Figure 6.10.

Figure 6.10. The Forte debugger in action.

graphics/06fig10.jpg


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net