Java Technologies in Desktop Applications
By Dana Nourie, October 2006
As you learn the details and syntax of the Java programming language, a question begins to emerge: Which Java technologies do I need to create a desktop application, and which packages should I import? The answer depends on what kind of application you are building and what it does.
This article is aimed at developers new to Java technologies. It describes the technologies involved in creating various types of desktop applications and when you might decide to use them. Watch for future articles that will give details about how to use these technologies and how to use them together. In the meantime, you can get started learning some of the technologies by following links within this article.
To use any of the technologies in this article, you must have the Java platform installed on your computer.
The Type, Look, and Function of Your Desktop Application
Before you read about the many Java technologies available, you must understand who will use your application, how they will use it, whether it will reside on an individual machine or on a network, as well as what kind of look or graphical user interface (GUI) the application will have -- if any.
Before you create an application, consider the five following areas:
- The application's distribution
- The GUI or lack of a GUI
- The application's functionalities
- Deployment of the application
- Other Java technologies
The Application's Distribution
First, you must decide whether your application will be distributed. A distributed application is a program that runs on more than one computer and communicates through a network. Some distributed applications are two separate software programs: the back-end server software and the front-end client software. Back-end software runs on a shared system, such as the Solaris Operating Environment or Linux, and manages shared resources, such as disks, printers, and modems. The back-end software also contains the main processing capability for the application. The front-end client software runs on workstations or individual computers. It is what users see when they use the application. The front-end client software handles user interface functions, such as receiving input from a keyboard and displaying output to a screen.
Distributed applications can be simple, requiring a single client computer and a single server, or more complex, allowing many client computers and several servers. Nondistributed applications run on the local machine and do not need to access back-end servers. You might write a simple calculator program, for instance, that might run only locally, though you can also make these kinds of applications distributed.
Very often, applications you create will be distributed programs and will run on networks or on the Internet for many computers to use.
To create distributed applications, you will need to learn about and use Java Remote Method Invocation (Java RMI), in which the methods of remote Java objects can be invoked from other Java virtual machines (JVMs), * possibly on different hosts. Java RMI uses object serialization to marshal and unmarshal parameters and does not truncate types, supporting true object-oriented polymorphism.
Following are some of the packages you will use to implement Java RMI:
Notice that the following package names start with javax
, not java
:
For background information and instruction on using Java RMI, read the Java RMI lesson in the Java Tutorial.
The GUI or Lack of GUI
Applications generally contain many miniprograms with various functions. Some miniprograms have a graphical user interface (GUI), if only a simple window or dialog box, and many miniprograms have no GUI. The application itself, however, is likely to have a main GUI that consists of menus, buttons, tool bars, text fields, and other graphical features. The GUI is mainly for user input, whether the user clicks a button or types in information, and a GUI can provide the user with additional information. In addition, GUI components often display information back to the user.
To create the GUI, you use the Java Foundation Classes/Swing (JFC/Swing) and Abstract Window Toolkit (AWT) API. The many classes and interfaces in those packages allow you to easily create buttons, check-box objects, text fields, and other components, as well as components to organize them.
By far the easiest way to create the frame and all the GUI components for your application is to use an integrated development environment (IDE), such as the NetBeans IDE. This IDE allows you to drag and drop your components into place while it writes the complex component code for you. It's easy to learn and saves you a lot of time. But every developer must understand how the JFC/Swing and AWT code works, so you should learn to program enough components by hand to understand the concepts.
Table 1: Main Packages to Use for GUI Programs
Of course, you don't have to import all of these packages, just the ones you use. If you use the NetBeans IDE, you will find that this IDE creates import statements in your code as you use JFC/Swing or AWT components. You can get a good idea of what many of these packages do just from their names.
To download the latest version of the NetBeans IDE, go to the NetBeans IDE downloads page.
To get started using the NetBeans IDE, see the NetBeans IDE 5.0 Quick Start Guide.
To begin learning how to code using JFC/Swing, see the lesson A Brief Introduction to the Swing Package in the Java Tutorial.
The Application's Functionalities
This article cannot discuss every functionality that you might use in your application, but some functionalities are common to many or most applications.
To get buttons, menus, and text fields to do something in your GUI, you will need to understand how event handling works. An event handler is a class that contains the instructions for what should happen when the user pushes a button or selects a menu. From there, a number of operations can take place: The application can display information back to the GUI, write data to files or a database, do math operations and show the results in bar graphs, or do something as simple as open a dialog box for the user to enter more information.
For more information on how to code GUI applications, see What Is Swing? in the Java Tutorial. You can learn how to create and use JFC/Swing components, write event handlers, and more.
Reading and Writing Data
Writing user input to a flat file system is a common operation in applications, as is reading from a file to write back to the GUI. For instance, a user may enter a name, address, and phone number into a GUI form to register the software on a server. One way of handling this data is to have the application write the data to a file on a host computer or a server.
The java.io
package provides for system input and output (I/O) through data streams, serialization, and the file system. An I/O stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays. Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data, whereas others manipulate and transform the data in useful ways.
No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. Two of the more popular classes used in the java.io
package are FileInputStream
and FileOutputStream
. Both create byte streams linked to files. To learn more about streams, see I/O Streams in the Java Tutorial.
A common approach to storing data is to use a database management system. To code your application to write data to a database and then retrieve the results, you will need to learn the JDBC API. The JDBC API provides universal data access from the Java programming language, allowing you to access virtually any data source -- from relational databases to spreadsheets and flat files. JDBC technology also provides a common base on which to build tools and alternate interfaces.
The JDBC API supports both two-tier and three-tier models for database access.
In the two-tier model, a Java applet or application talks directly to the data source. This requires a JDBC technology driver that can communicate with the particular data source that the application accesses. A user's commands are delivered to the database or other data source, and the results of those statements are sent back to the user. The data source may be located on another machine that the user is connected to by way of a network. This is referred to as a client-server configuration: The user's machine is the client, and the machine housing the data source is the server. The network can be a company intranet, for example, which connects employees within a company, or it can be the Internet.
In the three-tier model, the application sends commands to a middle tier of services, which then sends the commands to the data source. The data source processes the commands and sends the results back to the middle tier, which then sends them to the user. Management information system (MIS) directors find the three-tier model very attractive because the middle tier makes it possible to maintain control over access and the kinds of updates that can be made to corporate data. Another advantage is that the three-tier model simplifies the deployment of applications. For instance, data can be changed in a database or new components added without interfering with the user experience. Finally, in many cases, the three-tier architecture can provide performance advantages because multiple transactions and memory are more efficient.
For writing applications that access a database using the JDBC API, you will use the following:
- Classes and interfaces from the
java.sql
package. - A database management system, such as the Java DB database system. You can also download Java DB with the Sun Java System Application Server Platform Edition 9 as part of the Java EE download.
- The appropriate drivers for your database management system.
To learn more about using the JDBC API in your application, see Getting Started With the JDBC API.
To download a server and database, as well as an IDE to write the application, go to the NetBeans IDE 5.0 download page.
Performing Calculations and Managing Data
Much of your application contains code for instructions that must occur behind the scenes. You will likely do a lot of programming using strings and numbers. Because you must type all data in the Java programming language, you should be familiar with using the java.lang
package.
In the java.lang
package, you should understand strings, a sequence of characters. Strings are widely used in Java technology programming. The String
class is used to create and manipulate strings, as is the StringBuilder
class.
You should also become familiar with the Number
class and its subclasses in the java.lang
package, which enable you to use instantiations of these classes rather than the primitive number types. The PrintStream
and DecimalFormat
classes provide methods for writing formatted numerical output. Finally, the Math
class contains mathematical functions to complement the operators built into the language. This class has methods for trigonometric functions, exponential functions, and so on.
In order to handle or manipulate various types of data, you will need to learn about the collections framework, a unified architecture for representing and manipulating collections. A collection, sometimes called a container, is an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, collections represent data items that form a natural group, such as a poker hand, a collection of cards, a mail folder, a collection of letters, or a telephone directory that maps names to phone numbers.
Figure 1 shows the collection framework and some of the popular interfaces involved and found in the java.util
package.
Core Collection
interfaces are the foundation of the collections framework. A Set
is a special kind of Collection
, a SortedSet
is a special kind of Set
, and so forth. Note also that the hierarchy consists of two distinct trees. A Map
is not a true Collection
even though it is used in similar fashion as a collection. The Map
interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings.
Concurrency
Applications must often do more than one thing at a time. For example, a streaming audio application must simultaneously read the digital audio off the network, decompress it, manage playback, and update its display to the user. A word processing program should always be ready to respond to keyboard and mouse events, no matter how busy it is reformatting text or updating the display. Software that can do such things is known as concurrent software.
Basic currency support is available in the java.util.concurrent
package.
Error Handling
The Java programming language uses exceptions to handle errors and other unexpected events. An exception is an event that occurs during the execution of a program and that disrupts the normal flow of the program's instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is known as throwing an exception.
To handle the exceptions in your application, you will need to understand how to write your code so that the application can catch or throw an exception. You can learn all the details about exception handling in the Exceptions lesson in the Java Tutorial.
Deployment of the Application
Use Java Plug-in technology to deploy applets when you want your application to run within a browser or when the application is tightly integrated with web page content. When you want your application to stand alone on the user's desktop.
Java Web Start technology requires that applications be packed in Java Archive (JAR) files. The JAR file format enables you to bundle multiple files into a single archive file. Typically, a JAR file contains the class files and auxiliary resources associated with applets and applications.
JAR files are packaged with the ZIP file format, so you can use them for tasks such as lossless data compression, archiving, decompression, and archive unpacking. These tasks are among the most common uses of JAR files, and you will discover many of the benefits of the JAR file by using only these basic features.
If you want to take advantage of advanced functionality provided by the JAR file format, such as electronic signing, you will first need to become familiar with the fundamental operations. To perform basic tasks with JAR files, you use the JAR tool provided as part of the Java Development Kit (JDK). The lesson Using JAR Files: The Basics in the Java Tutorial explains the details of using the JAR tool.
Once you have your application packaged in a JAR file, you can use Java Web Start technology to deploy the application. Java Web Start software provides the power to launch full-featured applications with a single click. The user can download and launch applications, such as a complete spreadsheet program or an Internet chat client, without going through complicated installation procedures.
Instead, with Java Web Start software, the user launches a Java technology-based application by clicking a link in a web page. The link points to a Java Network Launching Protocol (JNLP) file, which instructs the Java Web Start software to download, cache, and run the application.
To learn the details of using Java Web Start software to deploy your application, read the Java Web Start software lesson in the Java Tutorial.
Other Java Technologies
This section will discuss some other Java technologies you should consider.
Application Security
Most developers are very concerned with application security, and Java technologies help address this concern in many ways. Java security technology includes a large set of APIs, tools, and implementations of commonly used security algorithms, mechanisms, and protocols. The Java platform security APIs span a wide range of areas, including cryptography, public key infrastructure, secure communication, authentication, and access control. Java security technology provides you with a comprehensive security framework for writing applications, and it also provides the user or administrator with a a set of tools to securely manage applications.
There are too many security packages and classes to list here, but several excellent sources can get you started learning about application security using Java technologies. See the Security page for a list of programmer's guides, API specifications, tools, and tutorials. Additionally, you can visit the Java SE Security page, which includes many links to articles, white papers, and other information.
Full-Screen Exclusive Mode API
If you need high performance graphics in your application, for programs such as games or slide shows, then you should learn about the Full-Screen Exclusive Mode API. Full-screen exclusive mode is a powerful new feature that enables you to suspend the windowing system so that the application can draw directly to the screen. The Full-Screen Exclusive Mode API lesson in the Java Tutorial teaches you everything you need to know about using this API.
Internationalization
Internationalization is the process of designing an application so that it can be adapted to various languages and regions without engineering changes. Sometimes the term internationalization is abbreviated as i18n, because there are 18 letters between the first and the last letters. The Internationalization trail in the Java Tutorial teaches you what you need to know about internationalization and localization. Localization, often abbreviated as L10N, is the process of adapting software for a specific region or language by adding locale-specific components and translating text. The package you will use most for internationalization is java.util
. For additional information, see the Core Java Internationalization page.
Summary
The sheer number of Java technologies available can be confusing, but this article gives you a head start on the ones you will need to learn to develop the kind of desktop applications you want. If you want your applications to have certain functionalities, you are likely to find a well-developed Java technology to help you program the features you need.
To find a succinct list of Java technologies and learn what they are for, see the article " Unraveling Java Terminology." This article defines the most common Java terms and provides links for more information and downloads. In addition, you can see how the technologies fit together and get to their documentation quickly through the Java SE 6 Platform at a Glance page.
Look for instructions on how to use these Java technologies for application development in future issues of Java Technology Fundamentals newsletter and in the New to Java Programming Center.
* As used on this web site, the terms "Java virtual machine" or "JVM" mean a virtual machine for the Java platform.