© Ioannis Kostaras, Constantin Drabo, Josh Juneau, Sven Reimers, Mario Schröder, Geertjan Wielenga 2020
I. Kostaras et al.Pro Apache NetBeanshttps://doi.org/10.1007/978-1-4842-5370-0_2

2. Getting Started with NetBeans

Ioannis Kostaras1 , Constantin Drabo2, Josh Juneau3, Sven Reimers4, Mario Schröder5 and Geertjan Wielenga6
(1)
The Hague, South Holland, The Netherlands
(2)
Ouagadougou, Burkina Faso
(3)
Chicago, IL, USA
(4)
Salem, Germany
(5)
Berlin, Germany
(6)
Amsterdam, The Netherlands
 

When you want to quickly develop an application, you can use Apache NetBeans IDE to simplify your tasks. In this chapter, you will discover the layout of the editor along with some tips that will help you to have some control over your code and speed up development.

Default Layout and Purpose

Each window in the IDE appears as a tab in the pane or position where it resides. Additionally, the IDE’s window system enables you to arrange windows anywhere in the IDE by dragging and dropping to any of the window positions shown in the layout of Figure 2-1.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig1_HTML.jpg
Figure 2-1

Apache NetBeans Default Layout Windows positions

The following sections cover some of the specific windows that you will see for each of the positions in Figure 2-1. For example, the Explorer window position can show projects, or files, or services, all depending upon the context of what you are doing at a given time.

Explorer Window

The Explorer window position is used by all windows that provide access to user objects. These windows include the projects window, files window, services window, and the tasks window.

Projects Window

The Projects Window (Ctrl+1 or Cmd+1) displays project sources. It displays the logical view of one or more projects. Each project or view is represented by a node, with a specific icon depending on the project type (e.g., a Java application node is presented differently from a JavaFX application node) as shown in Figure 2-2.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig2_HTML.jpg
Figure 2-2

Projects Window with different types of projects

Right-clicking on each node invokes a pop-up menu listing commands that you can run on that node. The pop-up menu provides project commands to compile, build, run, etc. You can also execute the main file of a Java project by selecting the project and pressing the F6 shortcut key.

Files Window

The Files Window (Ctrl+2 or Cmd+2) displays the directory-based view of a project, including files and folders that are not displayed in the Projects window. Right-clicking on each node invokes a menu that has commands to create new files or directories, and also copy, cut, and paste actions. Figure 2-3 shows an example of a project structure.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig3_HTML.jpg
Figure 2-3

Directory-based structure of a project in Files Window

Services Window

The Services Window (Ctrl+5 or Cmd+5), shown in Figure 2-4, displays the logical view of runtime resources such as servers, databases, web services, Docker containers, issue trackers, and the tasks repository, which are registered with Apache NetBeans IDE.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig4_HTML.jpg
Figure 2-4

Services Window

Tasks Window

The Tasks Window (Ctrl+Shift+6 or Cmd+Shift+6) is conceived for the inventory of tasks registered in a task’s repository such as JIRA, Bugzilla, and so forth. Figure 2-5 shows an example of a Tasks Window.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig5_HTML.jpg
Figure 2-5

Tasks Window

Navigator Window

The Navigator Window (Ctrl+7 or Cmd+7) provides a compact view of the currently selected file and simplifies navigation between different parts of the file. For a Java file, the window displays members (constructors, properties, and attributes) and Beans Patterns as shown in Figure 2-6.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig6_HTML.jpg
Figure 2-6

Navigator Window

The Navigator Window also displays Maven goals in the event the project is a Maven-based application. Figure 2-7 shows an example, a list of Maven goals for a Maven application. In the case of Maven, you are able to execute a goal by double-clicking its node.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig7_HTML.jpg
Figure 2-7

Maven goals displayed in a Navigator Window

Editor Window

Document windows are normally displayed in the Editor Window position. With Document windows you are able to edit Java program files and other source files. You are also able to execute commands from the Java Platform Shell, because the Java 9 shell’s command-line interface has been also integrated within a Document Window.

Source Editor

The Source Editor is the part of the IDE layout that is used the most. It is the place to write your source code or design graphic user interfaces for Java or other types of application.

The Java Source Editor is comprised of tabs: the Source view (where the user writes code), the Design view (where the user designs forms), and the History view (displays the modification history of the file). All three tabs are shown in Figure 2-8, and you can see in the figure that the user is editing some Java source code.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig8_HTML.jpg
Figure 2-8

Source Editor for a Java File

The Source Editor helps you achieve coding tasks more quickly. thanks to many interesting features such as code completion, code templates, refactoring, and syntax coloring.

The Source Editor is not limited to just Java code. It can be used for several other language file types. The Source Editor offers features that apply to the specific type of file the user is working on (for example, Java, JSP, HTML, etc.).

Figure 2-9 shows the Source Editor being used to edit an HTML file.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig9_HTML.jpg
Figure 2-9

HTML code source in HTML Editor

Java Platform Shells

The Java Platform Shell (see Figure 2-10) is an interesting use case of the Source Editor window. The Java Platform is the place where you can write code snippets and interact with the JShell interpreter. You can edit source code but also execute that source code directly from within the editor without the need to create a Java file with a main() method.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig10_HTML.jpg
Figure 2-10

Java Shell Editor

Output Window

The Output Window position (Ctrl+4 or Cmd+4) can dock multi-tabbed windows to display messages from the Apache NetBeans IDE. The Output Window is displayed automatically after an execution succeeds (Figure 2-11), when compilation errors have occurred, when you debug your application, or in other cases where output needs to be presented to you.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig11_HTML.jpg
Figure 2-11

Output Window after an execution succeeds

When errors occurred during compilation, the Output Window displays the errors with hyperlinks to their location in the source code. Figure 2-12 shows a typical error listing.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig12_HTML.jpg
Figure 2-12

Output Window when an error occurred

When a program requires user input, a new tab appears in the Output Window.

Terminal Window

Apache NetBeans IDE offers a Terminal Window, which is also displayed in the Output Window position, which allows you to interact with the operating system. Click on the menu Window ➤ IDE Tools ➤ Terminal to open this window. You can run operating system commands and scripts as shown in Figure 2-13.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig13_HTML.jpg
Figure 2-13

Executing commands from the Terminal Window

Palette Window

The Palette Window (Ctrl+Shift+8 or Cmd+Shift+8) contains all the components that help in Rapid-Application Development (RAD). The behavior of the Palette is associated with the editor, and the list of components differs from one type of editor to another. Figure 2-14 shows the contents of the Palette when the Design view is dealing with a Java form (left) and an html form (right)
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig14_HTML.jpg
Figure 2-14

Palette Window for Java form and html form

Properties Window

Elements (nodes) in Projects, Files, Services, Documents, or Navigator windows have properties that can be viewed in the Properties window as shown in Figure 2-15. These elements can be Java classes, Swing components, and so on.

You can click a property to modify its value field. Note that not all properties can be modified (for example, File Size, Modification Time, etc.). A value field can be an input box, or it can be a list of values.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig15_HTML.jpg
Figure 2-15

An example of a Property sheet

Design a Project

Apache NetBeans offers you the ability to design an application using one of the popular build automation tools: Ant or Maven or Gradle. The following sections show examples of each of these cases.

Set Up an Ant-Based Project

By default, the Apache NetBeans IDE uses Ant as a build automation tool. Following are the steps to create a new project using Ant:
  1. 1.

    To create a project, choose File ➤ New Project (Ctrl+Shift+N) or click on the New Project toolbar icon (a folder with a green plus sign at top left). The New Project wizard appears as shown in Figure 2-16.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig16_HTML.jpg
Figure 2-16

New Project wizard

  1. 2.

    Select the Java with Ant category. A number of project templates is displayed on the right side such as Java Application, Java Modular Project, etc. Select a project template; choose Java Application if you are following along with the current example. Then click Next.

     
  2. 3.

    The next step is shown in Figure 2-17. Here you can provide the Project Name, the directory where the application will be stored (Project Location), as well as the class that will contain the main() method. Fill in the fields as shown in Figure 2-17. Then click Finish.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig17_HTML.jpg
Figure 2-17

Provide the project name and location in step 2 of the wizard

You have created a new Java project. The Java Application template sets up a basic Java project and includes a main class as shown in Figure 2-18.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig18_HTML.jpg
Figure 2-18

A new Java Application created

The new application contains the Ant build file. You can display that build file through the Files tab. When selecting the Ant manifest, the Navigator Window displays the build targets as shown in Figure 2-19.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig19_HTML.jpg
Figure 2-19

Ant build file and targets

Set Up a Maven-Based Project

Maven is a very popular build automation tool that dominates the Java ecosystem. Apache NetBeans supports Maven naturally (i.e., integrated inside the IDE, not as a plugin like in other IDEs). You can set up a new project using Maven by performing the following steps:
  1. 1.

    Create a Maven project by choosing File ➤ New Project (Ctrl+Shift+N), or by clicking the New Project toolbar icon (../images/479166_1_En_2_Chapter/479166_1_En_2_Figa_HTML.jpg). The New Project wizard opens as shown in Figure 2-20.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig20_HTML.jpg
Figure 2-20

New Project wizard

  1. 2.

    Select the Java with Maven category to display the available project templates (see Figure 2-20). Choose Java Application for the current example, and click Next.

     
  2. 3.

    In the next step (see Figure 2-21), you can provide the project name and location as well as the Maven coordinates (groupid, version, and package). When you are done, click Finish.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig21_HTML.jpg
Figure 2-21

Maven-based project name, location, groupid, and version

The new project is created with the Maven’s configuration file pom.xml. The Navigator Window in Figure 2-22 displays the goals (each set of predefined commands).
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig22_HTML.jpg
Figure 2-22

A new Maven-based project

Set Up a Gradle-Based Project

Gradle is the new player on the game. It was created to ease some of the hassles of Maven, like, for example, the long pom files. Similar to the above, to create a new Gradle-based project, create a new project and select the Java with Gradle category as shown in Figure 2-23 (NetBeans might activate the feature if it is the first time that it is being used).
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig23_HTML.jpg
Figure 2-23

A new Gradle-based project

Pressing Next, complete the various fields like project name and location; and the optional group, version, description, etc., of the second step of the wizard and click Finish in order to see your new Gradle project created.

Speed Up Your Application Coding

The Apache NetBeans IDE has plenty of rich features that allow you to speed up your coding tasks. The IDE has default features such as code completion, syntax highlighting, code templates, and so on that you can use and modify by changing various settings.

Intelligent Code Completion

Code completion is the most exciting and most-used feature of the IDE. It is specific to the Source Editor, and it is available for the majority of the supported languages (Java, HTML, PHP, and so on).

There are two ways to open the code completion pop-up dialog box (shown in Figure 2-24): by pressing Ctrl+Space or by typing the dot (.) character inside the editor.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig24_HTML.jpg
Figure 2-24

Code completion in the Java editors

The code completion feature is smarter than it has never been. For example, if you want to call a variable, a class, or a method, the auto pop-up code completion dialog will list all the elements defined and help you to easily choose the one you need by typing the first character of the element’s name.

There is also camel case completion. Instead of typing all the characters, you can type just the capital letters of the word you’re interested in. Then press Ctrl+Space and the camel case completion feature will complete the full name of the element.

You also have access to Javadoc for an element. Put the cursor on an element name and press Ctrl+Space as shown in Figure 2-25. You’ll see a dialog open with the Javadoc for the element you’ve selected.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig25_HTML.jpg
Figure 2-25

Javadoc pop-up window

The IDE also provides keyword completion. In this case the editor checks the context and suggests the most relevant keywords. Figure 2-26 shows an example, and you can see how the editor sees that you are specifying a class definition and offers you the opportunity to either extend another class, or to implement an interface.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig26_HTML.jpg
Figure 2-26

Keyword completion

Hints

Hints are excellent guides when typing code. Hints provide suggestions on how to fix errors and complete whatever statement or expression you are typing. The editor displays small light bulbs in the IDE’s left-hand margin for the lines of code for which hints are available (see Figure 2-27). Click on a bulb, and one or more hints appear.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig27_HTML.jpg
Figure 2-27

Hint to solve surrounding statement issue

Refactoring

Sometimes you want to change the name of a specific element (class, variable, method) in a project, but this can have a domino effect as it might break dependencies, that is, other elements that depend on that name. Instead of modifying each file that uses that name, you can use the refactoring feature. Refactoring an element will replace the old name of each occurrence by the new name.

Say, for example, that you would like to rename a class from Animals to Animal. Here are the steps to getting the IDE to do the work for you:
  1. 1.

    Put the cursor on the class name in the Source Editor and press Ctrl-R, or right-click and select Refactoring ➤ Rename from the pop-up menu (see Figure 2-28).

     
  2. 2.

    Check Apply Rename on Comments, if you wish that the modification will be applied also to occurrences in comments.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig28_HTML.jpg
Figure 2-28

Rename class refactor window

  1. 3.

    Type the new name of the class and press Preview to preview all the occurrences or Refactor to apply the rename without preview. The IDE will present you with both versions of your source file, as shown in Figure 2-29.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig29_HTML.jpg
Figure 2-29

Preview Refactoring occurrences

  1. 4.

    Does everything look good? Then press Do Refactoring to apply the modifications. Otherwise press Cancel.

     

Comparing Files

The editor offers a “diff” feature to deal with the problem of finding differences between two files. For example, say that you have two Animal class definitions, each one in a separate package. Do the following to see whether those definitions are the same or different:
  1. 1.

    Open both class files in the Source editor.

     
  2. 2.

    Right-click on one of the source files in the Projects window and select Tools ➤ Diff to… A dialog box appears displaying a list with the files that open in the Editor.

     
  3. 3.

    Select the second file from the Files Open in Editor list to display the results of the diff operation. Figure 2-30 shows how green backgrounds are used to indicate lines of code that are present in one file, but missing from the other.

     
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig30_HTML.jpg
Figure 2-30

Differences between two files

Generating Code

You can generate pieces of code when coding a Java application. The IDE offers some alternatives: by using the Code Completion feature, or from the Code Generation pop-up menu, or by using the Refactoring features . The code completion feature can be used to add code snippets in a Java class body. For example, the feature can help to generate the accessors in a class for which you have defined properties that you wish to make available to other classes.

Figure 2-31 shows how to create a setter function in the class Animal for the name attribute. If the pop-up menu doesn’t appear, type Ctrl+space.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig31_HTML.jpg
Figure 2-31

Generate code snippets with code completion

Use the Code Generation pop-up menu by clicking inside the editor and pressing Alt-Insert (or choosing Source ➤ Insert code). It helps, for example, to generate a constructor as shown in Figure 2-32.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig32_HTML.jpg
Figure 2-32

Code Generation pop-up menu

Thanks to this feature you can easily generate the body of class with extreme speed. It generates getters and setters, toString(), equals() and hashCode() methods, overrides methods from the superclasses, etc., in only a few clicks.

The option Add property creates with one action both a field and its accessors. Below is a sample of inputs for the Add Property dialog box (Figure 2-33).
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig33_HTML.jpg
Figure 2-33

Add Property dialog box

The second alternative to generate accessors is by choosing Refactor ➤ Encapsulate. The IDE displays the Encapsulate Fields dialog box (see Figure 2-34).
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig34_HTML.jpg
Figure 2-34

Encapsulate Fields dialog box

Select one or more fields or select all the fields by pressing the Select All button. Then press Refactor to encapsulate the fields (i.e., generate the accessors with the visibility you selected).

Using Code Templates

The IDE helps with predefined pieces of code provided through code templates. Code templates are abbreviations of long code snippets.

To access a code template, type the abbreviation and hit Ctrl+Space. Doing so will expand the full code as shown in Figure 2-35. There you can see that I typed the keyword while. Then when I pressed Ctrl+Space, NetBeans suggested several templates with code snippets beginning with that same keyword.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig35_HTML.jpg
Figure 2-35

Code template for while

There are predefined sets of abbreviations with templates you can use, or you may customize the existing ones or add your own by displaying the Options dialog box (see Figure 2-36) via Tools ➤ Options ➤ Editor ➤ Code Templates (NetBeans ➤ Preferences ➤ Editor ➤ Code Templates on Mac).
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig36_HTML.jpg
Figure 2-36

Code Templates window

Splitting Windows

It often happens that we want to look at different source files simultaneously. You can easily look at two files by splitting the Source Editor. Having at least two source files open, click on the tab of a file and then drag it to the far left, far right, or bottom of the Source Editor window. Release the mouse button when the red outline that appeared around the tab when you started dragging changes to a rectangle indicating the placement of the split window. Figure 2-37 shows the result.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig37_HTML.jpg
Figure 2-37

View of two files simultaneously

Splitting windows is applicable to a single source file, too. Below there are four ways to split a source file into two views:
  • On the right side of each editor window, above the vertical scrollbar, there is a small plus icon that you can drag to split the window horizontally or vertically.

  • Use the menu action Window ➤ Configure Window ➤ Split Document ➤ Horizontally or Vertically.

  • Right-click the file’s tab and select Clone.

  • Right-click the file’s tab and select Split ➤ Vertically or Split ➤ Horizontally.

Figure 2-38 shows the same source file being edited in two views. The advantage of such an edit is that you can have one view positioned on, say, a class definition in order to remind yourself of what the class is about while you are writing code later in the document that refers to the class. That is one of many reasons why you might want to view the same file in two or more tabs.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig38_HTML.jpg
Figure 2-38

A single file split in two views

Enriching Your IDE

You can change the default setting of the IDE and also enrich it with additional features through plugins. To install a plugin, open the Plugins dialog box (Tools ➤ Plugins) as shown in Figure 2-39. Then choose which of the plugins that interest you and press Install (installation requires an Internet connection but you may also install downloaded plugins with the Downloaded tab).
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig39_HTML.jpg
Figure 2-39

Plugins dialog box

The Options dialog box (Tools ➤ Options or NetBeans ➤ Preferences on Mac) is a multi-tabbed window that enables you to change any of the IDE’s configuration settings. The Options dialog box is the place, for example, to change the editor’s fonts and colors, the Java coding general configuration, the code completion behaviors, etc. Figure 2-40 shows an example of an editor formatting configuration.
../images/479166_1_En_2_Chapter/479166_1_En_2_Fig40_HTML.jpg
Figure 2-40

Options dialog box

Conclusion

This chapter gave an introduction to the main features of the Apache NetBeans IDE. We saw the IDE’s layout with the default windows positions (Editor, Explorer, Navigator, Output, Properties, Palette); we saw how to create a Java Application project using Ant, Maven or Gradle, as well as tips to make you more productive. You are now equipped with knowledge that is good enough to further explore Apache NetBeans in the next chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.51.241