Creating a Frame

NetBeans is using javax.swing.JFrame as a base for all Swing applications; in this recipe, we will use the word Frame to denote instances of javax.swing.JFrame class.

The Frame is nothing more than a border where the developer will place the components; think of a real frame where the canvas goes, so the artists can paint on it, and where other components will be added. The provided implementation from NetBeans is very basic in its content but it already has some basic controls, so the designer can close, expand, and align the components implemented.

Desktop Applications will have at least one Frame, but several other components can be included in its "body".

Getting ready

For this recipe, you will require a Java Desktop Project created according to the recipe Creating a Java Desktop Application recipe, given at the beginning of this chapter.

How to do it...

With the IDE and the Java Desktop Application project:

  1. Right-click desktopapp package, select New, and click on JFrame Form....
  2. Under Class Name, write MyJFrame and click Finish.
How to do it...

An empty JFrame will be shown on the editor.

How it works...

The IDE creates the MyJFrame.java class and places it under the desktopapp package. NetBeans then automatically generates the code for the default close operation, adds and configures a javax.swing.GroupLayout, and names it as "Form".

The execution behavior is also added by the IDE with a main method added to the body of the class, different from what was previously seen in Creating a Java Desktop Application.

So this is more of a "fresh start" for the entire application. No window, no status bar, and no other components. It is a clean sheet where the developers have total liberty to do what they want.

There's more...

Want to deploy your Swing application on a different platform from the one where it is being written? Want to check out what code was generated by NetBeans in the background? Your application does not need to close when pressing the x (close) button on the corner of the window.

Changing the look and feel of your application

Swing gives you the possibility of changing how your applications Look and Feel. The Look and Feel of an application is basically how the window border, buttons, titles, and system colors of a program are specified.

Swing has different Look and Feel for various operating systems, so your application will look different when launched under Windows, Linux, or Solaris.

To change the Look and Feel of you application:

  1. Select desktopapp under the Projects window and right-click on it.
  2. Then select Properties, expand the Application node, and select Desktop App.
  3. Click on the drop-down named Look & Feel and select the desired one.

If you wish to check the look and feel of a configuration before setting it up as the default, then open the MyJFrame.java class and on the GUI Designer, click on Design and the Inspector. A window will appear on the left corner of the IDE.

Changing the look and feel of your application

Then right-click on the JFrame icon, select Preview Design and click on one of the three options given by the IDE: Metal, CDE/Motif, or GTK+.

On clicking the desired choice, the IDE will then launch the application with the modified Look and Feel.

NetBeans also gives users the option of downloading different Look and Feel files. Please visit the following links for more information:

http://kenai.com/projects/nbsubstance/pages/Home

http://www.jtattoo.net/

Checking the generated code

One of the main benefits in using the GUI Design is how practical and trivial it is to start your own application. If we check the contents of the files created by NetBeans, we can see how much heavy lifting the IDE does for us.

To check some of that heavy lifting, on the GUI Designer, click on Source; the IDE will then show the contents of MyJFrame.java.

Inside the editor, the code generated by the IDE is "hidden" from the user with an IDE configuration parameter.

Checking the generated code

Expand the Generated Code and then the initComponents method to see the contents.

Where to find the default closing behavior

With the MyJFrame.java open and Design button toggled, check the properties window. The Properties button under Properties needs to be toggled.

Where to find the default closing behavior

By clicking on the right-hand side of defaultCloseOperation, you'll get a drop-down menu with the following options:

  • HIDE: Hides the window when clicking the exit button. It is possible to see whether the application is still running through the IDE.
  • EXIT_ON_CLOSE: Closes the window when clicking the exit button. Similar to as if the window had called System.exit(0).
  • DO_NOTHING: No apparent response from the Frame.
  • DISPOSE: Disposes (closes) the window. It behaves similarly to the EXIT_ON_CLOSE option when only one window is being used by that particular JVM.
..................Content has been hidden....................

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