Working with projects
Apache NetBeans 12.0. 12.0 is primarily a consolidation of the minor releases 11.1, 11.2, and 11.3. There is as little innovation as possible in 12.0 and as much qualitative solidifying of existing features and functionality. The program through which qualitative consolidation takes place for 12.0 is NetCAT 12.0. Apache NetBeans is free and open source and is governed by the Apache Software Foundation. Fast & Smart Editing. Apache NetBeans is much more than a text editor. Java Netbeans free download - Java Runtime Environment (JRE), DJ Java Decompiler, Java Runtime Environment (JRE) (64-Bit), and many more programs. NetBeans is used professionally by various developers on different tasks, but some developers and projects require alternatives. For example, many people choose Eclipse, which is the main competitor of NetBeans, due to its speed and editing support.
How do I open a NetBeans project in IntelliJ IDEA?
Use File | New | Project from Existing Sources and select your NetBeans project directory.
When the Import Project wizard opens, select the Create project from existing sources option and then follow the instructions of the wizard.
IntelliJ IDEA will add the necessary definition files (the .idea directory) to your project directory. The NetBeans .nbproject directory and build.xml will remain untouched, and you'll be able to use IntelliJ IDEA along with NetBeans.
During the import IntelliJ IDEA will fix missing libraries, add facets for different Web frameworks and create a run configuration.
If you are using Maven with NetBeans, and you want to import a Maven project into IntelliJ IDEA, select File | Open and then select your project's pom.xml. You'll still need to configure a run configuration, however, all project dependencies will get resolved.
What's the difference between projects and modules?
IntelliJ IDEA creates a project for an entire code base and a module for each of its individual components. So, IntelliJ IDEA module is more like a NetBeans project.
The following table maps the most important NetBeans concepts to IntelliJ IDEA ones.
NetBeans | IntelliJ IDEA |
---|---|
Project | Module |
Global library | Global library |
Project library | Module library |
Project dependency | Module dependency |
Is there a directory-based project format in IntelliJ IDEA?
Yes, there is a .idea directory where project definition XML files are stored. For more information, see Projects.
How do I change the JDK for my project?
Open the Project Structure dialog (File | Project Structure or Ctrl+Alt+Shift+S ).
Under Platform Settings, select SDKs.
Click , select JDK and specify the JDK installation directory.
Click Apply.
Under Project Settings, select Project.
Under Project SDK, select the JDK from the list.
Click OK.
For more information, see SDKs.
How do I add a library to my project?
Open the Project Structure dialog (File | Project Structure or Ctrl+Alt+Shift+S ).
Under Project Settings, select Libraries.
Click , select Java and specify the library location.
Select the modules in which this library will be used.
Click OK.
For more information, see Working with libraries.
How do I configure a Web framework for my project?
In IntelliJ IDEA Ultimate (there's no corresponding functionality in the Community Edition):
Open the Project tool window (for example View | Tool Windows | Project ).
Right-click the necessary module and select Add Framework Support. (Framework support is enabled at a module level.)
In the Add Frameworks Support dialog that opens, select the frameworks to be supported, specify the associated settings and click OK.
For more information, see Add frameworks (facets) and for example Jakarta Server Faces (JSF).
The Run button is disabled. How do I run my application?
The Run button is disabled because there are no run configurations in your project.
If you have a Java class with a main()
method, open the corresponding file in the editor, right-click the editing area and select Run '<FileName>.main()'. As a result, the necessary run configuration will be created automatically and then executed.
You can create run configurations yourself: in the main menu, select Run | Edit Configurations |. Click to add a new configuration and choose how you want to run your application.
How do I generate an Ant build script for my project?
Select Build | Generate Ant Build. For more information, see Generate Ant Build file.
How can I open several projects in IntelliJ IDEA simultaneously?
It's possible to work with multiple projects simultaneously using IntelliJ IDEA. To achieve this, you only need to open a project, while another one is already opened, and choose Add to currently opened projects.
How do I close a project?
Select File | Close Project. You can also use File | Exit to close all open projects and quit IntelliJ IDEA.
Where is the Options dialog?
In IntelliJ IDEA, the Settings dialog is used for similar purposes. To open this dialog, press Ctrl+Alt+S.
There is also the Project Structure dialog (Ctrl+Alt+Shift+S) which lets you manage JDKs, libraries, module dependencies, and so on.
For more information, see Settings / Preferences dialog and Project Structure dialog.
How do I start with VCS integration?
The most popular Version Control Systems including Git, Subversion, Mercurial, Perforce, and more are supported by IntelliJ IDEA. VCS integration for your project can be configured in on the Version Control page of the Settings /Preferences dialog. See Version control fore details.
Working with the code editor
Can I use the NetBeans key bindings in IntelliJ IDEA?
Yes, you can.
In the Settings/Preferences dialog Ctrl+Alt+S, select Keymap under Appearance and Behavior.
In the right-hand part of the dialog, next to Keymaps, select NetBeans 6.5 from the list.
How does code completion in IntelliJ IDEA work?
The code completion suggestion list appears automatically after you type one or two letters. To narrow down this list, use:
Ctrl+Space. The list is reduced to keywords and the names of classes, methods, and fields available in the current context. Note that the list changes when you press Ctrl+Space for the second or third time.
Ctrl+Shift+Space. Only the types appropriate for the current context are shown.
For more information, see Code completion.
Is local history in IntelliJ IDEA any different from that in NetBeans?
Local history in IntelliJ IDEA, generally, is more detailed. Whatever you do with a directory, file, class, method or field, or a code block is reflected in your local history. The local history also includes VCS operations.
For more information, see Local History.
Are there any special code analysis features in IntelliJ IDEA?
IntelliJ IDEA can analyze dependencies, data flows and stack traces, find duplicates and evaluate code quality. Just have a look at the options in the Analyze menu.
Can I enable 'mark occurrences' in IntelliJ IDEA?
You can. The corresponding option in IntelliJ IDEA is called Highlight usages of element at caret. This option is enabled by default.
Just in case:
Open the Settings dialog Ctrl+Alt+S.
In the Editor category, select General.
In the right-hand part of the dialog, under Highlight on Caret Movement, select the Highlight usages of element at caret checkbox.
Click OK.
Can I enable 'compile on save' in IntelliJ IDEA?
You can.
To enable automatic compilation on every save (or autosave), turn on the Build project automatically option in the Settings dialog:
Open the Settings dialog Ctrl+Alt+S.
In the Build, Execution, Deployment category, select Compiler.
In the right-hand part of the dialog, select the Build project automatically checkbox.
Click OK.
Note that by default, IntelliJ IDEA saves changed files automatically, so you don't need to use Ctrl+S as frequently as in other IDEs.
Can I enable 'deploy on save' in IntelliJ IDEA?
There is no such option in IntelliJ IDEA settings, however, you can get similar result by choosing an appropriate application update option in the corresponding run configuration.
For more information, see Update applications on application servers.
(The corresponding functionality is available only in IntelliJ IDEA Ultimate. The Community Edition doesn't provide integration with application servers.)
Using plugins
Can I use NetBeans plugins in IntelliJ IDEA?
Unfortunately not. However, a lot of functionality implemented as plugins for NetBeans is available in IntelliJ IDEA 'out of the box'. Besides, there's a lot of plugins for IntelliJ IDEA, so you can always find an IntelliJ IDEA plugin with the functionality similar to that of your favorite NetBeans plugin.
How do I find the plugin that I need?
All the functions related to working with plugins are on the Plugins page of the Settings dialog Ctrl+Alt+S. You can look for, download, install and update the plugins as well as enable and disable them.
For more information, see Plugins and Manage plugins.
How do I install the plugin that I have available on my computer?
Open the Settings dialog Ctrl+Alt+S.
In the left-hand pane, select Plugins.
In the lower part of the Plugins page, click Install plugin from disk.
In the dialog that opens, select the plugin file (normally, a JAR or ZIP).
Click OK.
If asked, restart IntelliJ IDEA.
I'd like to write a plugin for IntelliJ IDEA. Are there any instructions?
Yes, have a look at:
IntelliJ Platform SDK Developer Guide.
Information for Plugin Developers on the IntelliJ IDEA Plugins page
Is it possible to build NetBeans RCP applications with IntelliJ IDEA?
It is possible, however you won't get the same kind of support you would in the case of NetBeans (wizards, menu actions, and so on). Have a look at Using IntelliJ IDEA for NetBeans Platform Development.
Configuring PHP development environment
What configuration is needed before start?
A lot of IntelliJ IDEA features are available without any configuration right after you launch it. Still, to take full advantage of running your PHP application, you need to configure a PHP interpreter and a server.
If you plan to launch the application locally, you need a PHP engine installed and registered in IntelliJ IDEA, as well as a Web server installed, configured, and integrated with IntelliJ IDEA. You can install these components separately or use an AMP package. For more details about initial environment configuration, refer to Configure PHP development environment.
If you are going to run and debug an application directly on a remote host, the only thing you need is register access to this host in IntelliJ IDEA to enable synchronization.
How do I start with deployment to a remote host?
If you've checked out your project from the remote host, the deployment server is already configured. Otherwise, you will need to get it configured (it can be FTP/SFTP/FTPS server or mounted/local folder) on the Deployment page of the Settings/Preferences dialog. The Remote host tool window is available on the right-hand side of the IntelliJ IDEA window, which can be handy for browsing through your remote server and performing various actions.
See Deploy your application for details.
How do I start debugging?
IntelliJ IDEA comes with support for both Xdebug and Zend Debugger for debugging and profiling. There is a zero-configuration debugging workflow available, which means that to start debugging you only need to:
Click Start Listening for PHP Debugging Connections on the toolbar of the IDE.
Place a breakpoint in code by clicking in the editor gutter next to the line.
Start debugging in the browser using a plugin or browser bookmarklets.
NetBeans (@ http://netbeans.org) is an open-source Integrated Development Environment (IDE). NetBeans began in 1996 as a Java IDE student project at Charles University in Prague. Sun Microsystems acquired NetBeans in 1999. In 2010, Oracle acquired Sun (and thus NetBeans).
Compared with its rival Eclipse (http://www.elicpse.org) (both are open-source, so I don't know what are they competing for?), NetBeans provides seamless support for Java AWT/Swing, Java ME mobility pack, Java EE, and bundled with an excellent profiler for performance tuning.
NOTE (2018 Feb 01): NetBeans 8.2 might not work with JDK 9 at this moment. Stick to JDK 8 now, if you want to use NetBeans.
How to Install NetBeans 8.2
How to Install NetBeans on Windows
Step 0: Install JDK
To use NetBeans for Java programming, you need to first install Java Development Kit (JDK). See 'JDK - How to Install'.
Step 1: Download
Download 'NetBeans IDE' installer from http://netbeans.org/downloads/index.html. There are many 'bundles' available. For beginners, choose the 1st entry 'Java SE' (e.g., 'netbeans-8.2-javase-windows.exe
' 95MB).
Step 2: Run the Installer
Run the downloaded installer.
How to Install NetBeans on Mac OS X
To use NetBeans for Java programming, you need to first install JDK. Read 'How to install JDK on Mac'.
To install NetBeans:
- Download NetBeans from http://netbeans.org/downloads/. Set 'Platform' to 'Mac OS X'. There are many 'bundles' available. For beginners, choose 'Java SE' (e.g., '
netbeans-8.2-javase-macosx.dmg
' 116MB). - Double-click the download Disk Image (DMG) file.
- Double-click the '
NetBeans 8.x.mpkg
', and follow the instructions to install NetBeans. NetBeans will be installed under '/Applications/NetBeans
'. - Eject the Disk Image ('
.dmg
').
You can launch NetBeans from the 'Applications'.
Notes: To uninstall NetBeans, drag the '/Applications/NetBeans
' folder to trash.
How to Install NetBeans on Ubuntu Linux
To use NetBeans for Java programming, you need to first install JDK. Read 'How to install JDK on Ubuntu'.
To install NetBeans:
- Download NetBeans from http://netbeans.org/downloads/. Choose platform 'Linux (x86/x64)' ⇒ 'Java SE'. You shall receive a
sh
file (e.g., 'netbeans-7.x-ml-javase-linux.sh
') in '~/Downloads
'. - Set the downloaded
sh
file to executable and run thesh
file. Open a Terminal: Follow the instructions to install NetBeans.
To start NetBeans, run the script 'netbeans
' in the NetBeans' bin
directory:
Writing a Hello-world Java Program in NetBeans
Step 0: Launch NetBeans
Launch NetBeans. If the 'Start Page' appears, close it by clicking the 'cross' button next to the 'Start Page' title.
Step 1: Create a New Project
For each Java application, you need to create a 'project' to keep all the source files, classes and relevant resources.
- From 'File' menu ⇒ Choose 'New Project...'.
- The 'Choose Project' diglog pops up ⇒ Under 'Categories', choose 'Java' ⇒ Under 'Projects', choose 'Java Application' ⇒ 'Next'.
- The 'Name and Location' dialog pops up ⇒ Under 'Project Name', enter '
FirstProject
' ⇒ In 'Project Location', select a suitable directory to save your works ⇒ Uncheck 'Use Dedicated Folder for Storing Libraries' ⇒ Uncheck 'Create Main class' ⇒ Finish.
Step 2: Write a Hello-world Java Program
- Right-click on '
FirstProject
' ⇒ New ⇒ Java Class (OR choose the 'File' menu ⇒ 'New File...' ⇒ Categories: 'Java', File Types: 'Java Class' ⇒ 'Next'). - The 'Name and Location' dialog pops up ⇒ In 'Class Name', enter '
Hello
' ⇒ Delete the content in 'Package' if it is not empty ⇒ 'Finish'. - The source file '
Hello.java
' appears in the editor panel. Enter the following codes:
Step 3: Compile & Execute
There is no need to 'compile' the source code in NetBeans explicitly, as NetBeans performs the so-called incremental compilation (i.e., the source statement is compiled as and when it is entered).
To run the program, right-click anywhere in the source (or from the 'Run' menu) ⇒ Run File. Observe the output on the output console.
Notes:
- You should create a NEW Java project for EACH of your Java application.
- Nonetheless, NetBeans allows you to keep more than one programs in a project, which is handy for writing toy programs (such as your tutorial exercises). To run a particular program, open and right-click on the source file ⇒ Run File.
Correcting Syntax Error
NetBeans performs incremented compilation, as and when a source line is entered. It marked a source line with syntax error with a RED CROSS. Point your cursor at the RED CROSS to view the error message.
You CANNOT RUN the program if there is any syntax error (marked by a RED CROSS before the filename). Correct all the syntax errors; and RUN the program.
[TODO] Diagram
HINTS: In some cases, NetBeans shows a ORANGE LIGHT-BULB (for HINTS) next to the ERROR RED-CROSS (Line 5 in the above diagram). You can click on the LIGHT-BULB to get a list of HINTS to resolve this particular error, which may or may not work!
SYNTAX WARNING: marked by a orange triangular exclaimation sign. Unlike errors, warnings may or may not cause problems. Try to fix these warnings as well. But you can RUN your program with warnings.
Read the NetBeans Documentation
At a minimum, you SHOULD READ the 'IDE Basics, Getting Started, Java Application', which is accessible via NetBeans's 'HELP' menu ⇒ Help Contents. This will save you many agonizing hours trying to figure out how to do somethings later.
The 'Help' ⇒ 'Online Doc and Support' (@ http://netbeans.org/kb/index.html) contains many articles and tutorial on using NetBeans.
The NetBeans 'Start Page' also provides many useful links to get you started.
Debugging Program in NetBeans
Step 0: Write a Java Program
The following program computes and prints the factorial of n
(=1*2*3*...*n
). The program, however, has a logical error and produce a wrong answer for n
=20
('The Factorial of 20 is -2102132736
' - a negative number?!).
Let us use the graphic debugger to debug the program.
Step 1: Set an initial Breakpoint
A breakpoint suspends program execution for you to examine the internal states of the program. Before starting the debugger, you need to set at least one breakpoint to suspend the execution inside the program. Set a breakpoint at main()
method by clicking on the left-margin of the line containing main()
. A red circle or an inverted Triangle appears in the left-margin indicating a breakpoint is set at that line.
Step 2: Start Debugging
Right click anywhere on the source code ⇒ 'Debug File'. The program begins execution but suspends its operation at the breakpoint, i.e., the main()
method.
As illustrated in the following diagram, the highlighted line (also pointed to by a green arrow) indicates the statement to be executed in the next step.
Step 3: Step-Over and Watch the Variables and Outputs
Click the 'Step Over' button (or select 'Step Over' in 'Debug' menu) to single-step thru your program. At each of the step, examine the value of the variables (in the 'Variable' panel) and the outputs produced by your program (in the 'Output' Panel), if any. You can also place your cursor at any variable to inspect the content of the variable.
Single-stepping thru the program and watching the values of internal variables and the outputs produced is the ultimate mean in debugging programs - because it is exactly how the computer runs your program!
Step 4: Breakpoint, Run-To-Cursor, Continue and Finish
As mentioned, a breakpoint suspends program execution and let you examine the internal states of the program. To set a breakpoint on a particular statement, click on the left-margin of that line (or select 'Toggle Breakpoint' from 'Run' menu).
'Continue' resumes the program execution, up to the next breakpoint, or till the end of the program.
'Single-step' thru a loop with a large count is time-consuming. You could set a breakpoint at the statement immediately outside the loop (e.g., Line 11 of the above program), and issue 'Continue' to complete the loop.
Alternatively, you can place the cursor on a particular statement, and issue 'Run-To-Cursor' to resume execution up to the line.
'Finish' ends the debugging session. Always terminate your current debugging session using 'Finish' or 'Continue' till the end of the program.
Other Debugger's Features:
Modify the Value of a Variable
You can modify the value of a variable by entering a new value in the 'Variable' panel. This is handy for temporarily modifying the behaviour of a program, without changing the source code.
Step-Into and Step-Out
To debug a method, you need to use 'Step-Into' to step into the first statement of the method. You could use 'Step-Out' to return back to the caller, anywhere within the method. Alternatively, you could set a breakpoint inside a method.
Netbeans Java
NetBeans - Tips & Tricks
General Usage
These are the features that I find to be most useful in NetBeans:
- Maximizing Window (double-click): You can double-click on the 'header' of any panel to maximize that particular panel, and double-click again to restore it back. This is particularly useful for editing source code in full panel.
- Code Auto-Complete (or Intelli-Sense) (ctrl-space): Enter a partial statement (e.g., Sys) and press control-space to activate the auto-complete, which displays all the available choices.
- Javadoc (ctrl-space, alt-F1): Place the cursor on a method or class, and press ctrl-space to view the javadoc; or right-click ⇒ Show Javadoc (alt-F1) to open it on a browser.
- Code Shorthand (tab): For example, you can enter '
sout
' and press TAB for 'System.out.println
'; 'psvm
' for 'public static void main(String[] args) { }
' or 'for
i' + tab for a for-loop. To view and configure code template, choose 'Tools' menu ⇒ 'Options' ⇒ 'Editor' ⇒ 'Code Templates'. - Formatting Source Code (alt-shift-f): Right-click on the source (or from the 'Source' menu) ⇒ Choose 'Format'. NetBeans will layout your source codes with the proper indents and format. To configure the formatting, choose 'Tools' menu ⇒ 'Options' ⇒ 'Editor' ⇒ 'Formatting'.
You can also select the section of codes to be formatted, instead of the entire file. - Hints for Correcting Syntax Error: If there is a syntax error on a statement, a red mark will show up on the left-margin on that statement. You could click on the 'light bulb' to display the error message, and also select from the available hints for correcting that syntax error.
- Rename (Refactor) (ctrl-r): To rename a variable, place the cursor on that variable, right-click ⇒ 'Refactor' ⇒ 'Rename' ⇒ Enter the new name. All the appearances of that variables in the project will be renamed.
- Small Programs: You can keep many small toy programs (with
main()
) in one Java project instead of create a new project for each small program. To run the desired program, on the 'editor' panel ⇒ right-click ⇒ 'Run File'. - Source Toggle Comment: To temporarily comment-off a block of codes, choose 'Source' ⇒ 'Toggle Comment'.
- Error Message Hyperlink: Click on an error message will hyperlink to the corresponding source statement.
- Command-Line Arguments: To provide command-line arguments to your Java program in NetBeans, right-click on the 'project' ⇒ 'Set as Main Project' ⇒ 'Set Configurations' ⇒ 'Customize...' ⇒ 'Run' ⇒ select the 'Main' class ⇒ type your command-line arguments inside the 'Arguments' field ⇒ choose 'Run' menu ⇒ 'Run Main Project'.
- Line Numbers: To show the line numbers, right-click on the left-margin ⇒ 'Show Line Numbers'.
- Changing Font Face and Size: Tools ⇒ Options ⇒ Fonts & Colors ⇒ In 'Category', select 'Default' ⇒ In 'Font', choose the font face and size.
- Resetting Window View: If you mess up the window view (e.g., you accidentally close a window and cannot find it anymore), you can reset the view via 'Window' menu ⇒ 'Reset Windows'.
- Code Templates: For example, when you create a new Java class, NetBeans retrieves the initial contents from the 'Java Class' code template. To configure code templates, select 'Tools' menu ⇒ 'Templates' ⇒ Choose the desired template ⇒ 'Open in Editor'. To set a value of a variable used in the all the code templates (e.g.,
$User
), select 'Tools' menu ⇒ 'Templates' ⇒ 'Settings'. - Displaying Chinese Character: Need to choose a font that support chinese character display, such as 'Monospace', in Tools ⇒ Options ⇒ Fonts & Colors ⇒ Syntax ⇒ default.
- Changing the JDK Location: The Netbeans configuration file is located at '
etcnetbeans.conf
'. Edit the directive 'netbeans_jdkhome
'. - Let me know if you have more tips to be included here.
Java Application Development
- Choosing the JDK version for your program: Right-click on your project ⇒ 'Properties' ⇒ 'Source' node ⇒ You can select the JDK level of your project in pull-donw menu 'Source/Binary Format'.
- Enabling JDK 7 support: If JDK 7 is already installed in your system, right-click on your Project ⇒ 'Properties' ⇒ 'Source' node ⇒ 'Source/Binary Format' ⇒ Select 'JDK 7'. Also check 'Libraries' ⇒ Java Platform ⇒ JDK 7.
If JDK 7 is not installed/configured, install JDK 7. Add JDK 7 support to NetBeans via 'Tool' menu ⇒ 'Java Platforms' ⇒ 'Add Platform...'. - Choosing Default Charset: Right-click on your project ⇒ 'Properties' ⇒ 'Source' node ⇒ 'Encoding' ⇒ choose your desired charset for the text-file I/O from the pull-down menu.
- Enabling Unicode Support for File Encoding: Right-click on your project ⇒ 'Properties' ⇒ 'Source' node ⇒ 'Encoding' ⇒ choose your Unicode encoding (e.g., UTF-8, UTF-16, UTF-16LE, UTF-16GE) for the text-file I/O.
- To include Javadoc/Source: Use 'Library Manager' (select the 'Tools' menu ⇒ 'Libraries'); or 'Java Platform Manager' (select 'Tools' menu ⇒ 'Java Platforms')
- Adding External JAR files & Native Libraries ('.dll', '.lib', '.a', '.so'): Many external Java packages (such as JOGL, Java3D, JAMA, etc) are available to extend the functions of JDK. These packages typically provide a '
lib
' directory containing JAR files ('.jar
') (Java Archive - a single-file package of Java classes) and native libraries ('.dll
', '.lib
' for windows, '.a
', '.so
' for Linux and Mac).
To include an external JAR file ('.jar
') into a project: Expand the project node ⇒ Right-click on 'Libraries' ⇒ 'Add JAR/Folder...' ⇒ Select the desired JAR file or the folder containing the classes.
If the external package contains many JAR files, you could create a user library to contain all the JAR files, and add the library to all the projects that required these JAR files. From 'Tools' menu ⇒ 'Libraries' ⇒ 'New Library...' ⇒ Enter a library name ⇒ Use 'Add JAR/Folder...' to add JAR files into this library.
Many JAR files come with native libraries in the form of '.dll
', '.lib
' (for Windows) and '.a
', '.so
' for Linux/Mac. The directory path of these libraries must be included in JRE's property 'java.library.path
'. This can be done via right-click the project ⇒ Set Configuration ⇒ Customize... ⇒ Run ⇒ In 'VM options', enter '-Djava.library.path=xxx
', wherexxx
is path of the native libraries.
Notes: The JAR files must be included in theCLASSPATH
. The native library directories must be included in JRE's property 'java.library.path
', which normally but not necessarily includes all the paths from thePATH
environment variable. Read 'External JAR files and Native Libraries'.
Writing Java GUI (AWT/Swing) Application in NetBeans
Step 0: Read
- Java GUI Application Learning Trail @ http://www.netbeans.org/kb/trails/matisse.html.
- Swing Tutorial's 'Learning Swing with the NetBeans IDE' @ http://docs.oracle.com/javase/tutorial/uiswing/learn/index.html.
Step 1: Create a New 'Java Application' Project
- Launch NetBeans ⇒ File ⇒ New Project...
- Under 'Categories', choose 'Java' ⇒ Under 'Projects', choose 'Java Application' ⇒ Next.
- In 'Project Name', enter '
FirstNetBeansGUI
' ⇒ Choose a suitable directory for your 'Project Location' ⇒ Uncheck the 'Create Main class' box ⇒ Finish.
Step 2: Write a Java File 'JFrame Form'
- Right-click on the project '
FirstNetBeansGUI
' ⇒ 'New' ⇒ 'JFrame Form...' (or 'Others' ⇒ 'Swing GUI Forms' ⇒ 'JFrame Form'). - In 'Class Name', enter '
NetBeansSwingCounter
' ⇒ Finish. - Create the GUI Components visually:
- From the 'Platte' panel ⇒ 'Swing Controls' ⇒ Drag and drop a '
Label
', 'TextField
', and 'Button
' into the design panel. - Click on the '
jLabel1
' ⇒ In the 'Properties' panel, enter 'Count' in 'text' (You can also single-click on thejLabel1
to change the text). Right-click on thejLable1
⇒ Change Variable Name ⇒ In 'New Name', enter 'lblCount
'. - Similarly, for '
jTextField1
' ⇒ Change the 'text' to 0, and change the 'Variable Name' to 'tfCount
' ⇒ Resize the text field if necessary. - For '
jButton1
' ⇒ Change the 'text' to 'Count', and change the 'Variable Name' to 'btnCount
'.
- From the 'Platte' panel ⇒ 'Swing Controls' ⇒ Drag and drop a '
- Write the event handler for the button by double-clicking the button and enter the following codes:
- Create an instance variable
count
(just below the class declaration) as follows:
Step 3: Compile & Execute
Right-click the source and select 'Run File'.
Step 4: Study the Generated Source Code
Expand the 'Generated Code' and study how the GUI builder declare, allocate and initialize the GUI Component
s in the initComponents()
. Note how the JButton
registers an ActionEvent
listener and how an inner class is used as the listener and provide the event handler actionPerformed()
. Also notice that the main()
method uses a Swing's worker to run the GUI on the Event-Dispatcher thread, instead of the main
thread, for thread-safe operations.
NetBeans and MySQL
Reference: 'Connecting to a MySQL Database' @ http://netbeans.org/kb/docs/ide/mysql.html.
NetBeans (JavaEE) provides direct support to MySQL server. You can use NetBeans as a GUI client to access a MySQL server, as well as an administrative tool (e.g., starting and stopping the server).
Configuring NetBeans to Support MySQL
From NetBeans 'Window' menu ⇒ Select 'Services'. The 'Services' tab shall appear on the left pane
- Right-click on the 'Databases' node ⇒ 'Register MySQL Server'. (If you have already registered a MySQL server, you can right-click on Server node '
MySQL Server at hostname:port
' ⇒ Properties, to modify its properties.) - Select the 'Basic Properties' tab, enter the hostname, port number, root user and password.
- Select the 'Admin Properties' tab:
- Leave the 'Path/URL to admin tool' empty.
- In 'Path to start command', enter '
<MYSQL_HOME>binmysqld.exe
'; in the 'Arguments', enter '--console
' - In 'Path to stop command', enter '
<MYSQL_HOME>binmysqladmin.exe
', in the 'Arguments', enter '-u root -ppassword shutdown
'.
- A server node '
MySQL Server at hostname:port
' appears.
Database Administration - Start/Stop the Server and Create Databases
- You can start the MySQL server by right-clicking on the server node ⇒ select 'start'. [There seems to be a problem here. If a 'connection refused: connect' error occurs, enter the password again.]
- Once the MySQL server is started and connected, you can see the list of databases by expanding the MySQL server node. You can create a new database by right-clicking on it and choose 'Create Database...'.
Create a new Connection
You need a connection to manipulate data. You can create multiple connections with different users and default databases.
- Right-click on the 'Databases' ⇒ 'New Connection...' ⇒ Select the driver 'MySQL Connector/J' ⇒ Next ⇒ Enter hostname, port number, default database, a general username and password ⇒ 'Test Connection' (make sure that MySQL is started) ⇒ Finish.
- A connection node '
jdbc:mysql://hostname:port/defaultDatabase
' appears.
Manipulating Data via a Connection
- Right-click on a connection node (e.g., '
jdbc:mysql://hostname:port/defaultDatabase
') ⇒ Choose 'Connect' (if not connected, provided that the MySQL server has been started). - You can expand the connection node to view all the databases.
- Expand an existing database. There are three sub-nodes 'Tables', 'View' and 'Procedures'. Right-click on the 'Tables' to create table or execute command. Similarly, right-click on the 'View' and 'Procedures'.
- To view/manipulate the records in a table, right-click on the selected table ⇒ You can choose to 'View Data...', 'Execute Command...', etc.
- You can right-click on the connection to 'connect' or 'disconnect' from the server.
Create a SQL Script and Run the Script
You can create a SQL script by right-clicking on a project ⇒ New ⇒ 'SQL File'. You can run the script by right-clicking on the SQL script ⇒ 'Run File' ⇒ Select an existing connection (or create a new connection) to run the script. You could also run a single statement (right-click on the statement ⇒ Run Statement) or a selected group of statements (highlight the statements ⇒ Right-click ⇒ Run Selection).
Developing and Deploying Web Application in NetBeans
Read:
- 'Introduction to Developing Web Applications' @ http://netbeans.org/kb/docs/web/quickstart-webapps.html.
- More articles in 'Java EE & Java Web Learning Trail' @ http://netbeans.org/kb/trails/java-ee.html.
Web (HTTP) Servers
Configuring Web Server
Netbeans
You could configure the web server via 'Tools' menu ⇒ 'Servers'.
Tomcat Server
To configure Tomcat Server, select 'Tools' menu ⇒ 'Servers' ⇒ click 'Add Servers':
- Choose Server: Select the desired Tomcat version ⇒ Next.
- Installation and Login Details: In 'Server Location', fill in the Tomcat installation directory ($CATALINA_HOME) ⇒ Enter the username/password of a tomcat user with 'manager' role. You could either check the 'create user if it does not exist' or define the tomcat user in '
$CATALINA_HOMEconftomcat-users.xml
' as follows:
Running the Web Server
Choose 'Services' ⇒ Expand 'Servers' node ⇒ Right-click on the desired server ⇒ Start/Stop/Restart.
MySQL Database Server
You can also manage the MySQL database server directly from Tomcat. Read 'NetBeans and MySQL' Section.
Netbeans Vs Eclipse
Writing a Hello-World Servlet/JSP Web Application
Create a New Servlet/JSP Project
- From 'File' menu ⇒ choose 'New Project...'.
- 'Choose Project' ⇒ Under 'Categories', choose 'Java Web' ⇒ Under 'Projects', choose 'Web Application' ⇒ 'Next'.
- 'Name and Location' ⇒ In 'Project Name', enter '
HelloServletJSP
' ⇒ In 'Project Location', select a suitable directory to save your works ⇒ Check 'Set as Main Project' ⇒ Next. - 'Server and settings' ⇒ Choose your server, or 'add' a new server ⇒ Next.
- 'Frameworks' ⇒ Select none for pure servlet/JSP application ⇒ Finish.
Writing a Hello-World JSP
A JSP page called 'index.jsp
' is automatically created, which says 'Hello world!'. To execute this JSP, right-click on the project ⇒ 'Run'. The URL is http://localhost:8080/HelloServletJSP/index.jsp
.
Writing a Hello-World Servlet
- Right-click on the project '
HelloServletJSP
' ⇒ New ⇒ Servlet. - 'Name and Location' ⇒ In 'Class Name', enter '
HelloServlet
' ⇒ In 'Package', enter 'hello
' ⇒ Next. - 'Configure Servlet Deployment' ⇒ In 'Servlet Name', enter '
HelloServletExample
' ⇒ In 'URL Pattern', enter 'sayhello
' ⇒ Finish. - Enter the following codes for '
HelloServlet.java
': - To execute the servlet: Right-click on the project ⇒ run ⇒ Change the URL to
http://localhost:8080/HelloServletJSP/sayhello
.
Generating a WAR-file for a Web Application
A WAR (Web Archive) file is basically a zip file for distributing web application in single file. You can use WinZip or WinRAR to inspect or unzip the war file.
To distribute the project as a war-file, right-click project ⇒ 'Clean and Build'. The war file is created in the 'dist
' directory. You can deploy the web application by dropping the war-file into Tomcat's 'webapps
' directory. Tomcat will automatically unzip the war-file and deploy the application upon startup.
Debugging Web Application
The most important reason for using IDE is to use the graphic debugger for debugging the program. You can set a breakpoint in your server-side Java codes, and 'Debug' a web application, similar to a standalone application.
Netbeans With Jdk
Writing a Hello-world JSF 2.0 Web Application
Create a New JSF 2.0 Project
- From 'File' menu ⇒ choose 'New Project...'.
- 'Choose Project' ⇒ Under 'Categories', choose 'Java Web' ⇒ Under 'Projects', choose 'Web Application' ⇒ 'Next'.
- 'Name and Location' ⇒ In 'Project Name', enter '
HelloJSF20
' ⇒ In 'Project Location', select a suitable directory to save your works ⇒ Check 'Set as Main Project' ⇒ Next. - 'Server and settings' ⇒ Choose your server, or 'add' a new server ⇒ Next.
- 'Frameworks' ⇒ Check 'JavaServer Faces' ⇒ In 'Libraries', 'Registered Libraries', select 'JSF 2.0' ⇒ Finish.
- An '
index.xhtml
' JSF page is generated, as follows: To run this facelet, right-click on the project ⇒ Run.
Create a new JSF 2.0 Facelet
- Right-click on the project ⇒ New ⇒ 'Other...'
- 'Choose File Type' ⇒ Under 'Category', select 'JavaServer Faces' ⇒ Under 'File Type', select 'JSF Page' ⇒ Next.
- 'Name and Location' ⇒ In 'File Name', enter '
HelloJSF20
' ⇒ In 'Options', check 'Facelets' ⇒ Finish. - In '
HelloJSF20.xhtml
', enter the following codes: - To execute the JSF page, right-click on the project ⇒ Run ⇒ Change the URL to
http://localhost:8080/HelloJSF20/HelloJSF20.xhtml
.
Writing a Hello-world JSF 1.2 Web Application
Create a New JSF 1.2 Project
- From 'File' menu ⇒ choose 'New Project...'.
- 'Choose Project' ⇒ In 'Categories', choose 'Java Web' ⇒ In 'Projects', choose 'Web Application' ⇒ 'Next'.
- 'Name and Location' ⇒ In 'Project Name', enter '
HelloJSF12
' ⇒ In 'Project Location', select a suitable directory to save your works ⇒ Check 'Set as Main Project' ⇒ Next. - 'Server and settings' ⇒ choose your server, or 'add' a new server ⇒ Next.
- 'Frameworks' ⇒ Check 'JavaServer Faces' ⇒ In 'Libraries', 'Registered Libraries', select 'JSF 1.2' ⇒ Finish.
- A '
WelcomeJSF.jsp
' page is generated, as follows: To run this page, right-click on the project ⇒ Run.
Create a new JSF 1.2 Page
- Right-click on the project ⇒ New ⇒ 'Other...'
- 'Choose File Type' ⇒ In 'Category', select 'JavaServer Faces' ⇒ In 'File Type', select 'JSF Page' ⇒ Next.
- 'Name and Location' ⇒ In 'File Name', enter '
HelloJSF12
' ⇒ In 'Options', check 'JSP File (Standard Syntax)' ⇒ Finish. - In '
HelloJSF12.jsp
', enter the following codes: - To execute the JSF page, right-click on the project ⇒ Run ⇒ Change the URL to
http://localhost:8080/HelloJSF12/faces/HelloJSF12.jsp
.
Debugging Web Applications in NetBeans
You can debug a webapp just like standalone application. For example, you can set breakpoints, single-step through the programs, etc.
REFERENCES & RESOURCES
- NetBeans mother site @ http://netbeans.org.
- NetBeans 'Help' menu.