Home > Articles > Programming > User Interface (UI)

The Cross-Platform Java GUI: Designing Code for More Than One Operating System

As cross-platform languages go, Java is considered one of the best choices available. It has a rich library for GUI development that is available on every platform on which Java is available. However, there is more to building a cross-platform GUI than just writing the code. Marcus Zarra steps you through a few things that need to be considered when designing an application with the intent of using it on multiple platforms.
Like this article? We recommend

One of the most accurate complaints users have against Java GUIs is that they do not feel native. While this is partially due to the way that Swing is designed, it is also partially the fault of the developer. It is quite common for a developer to design the GUI to work on his or her platform of choice and then rely solely on Java’s cross-platform nature to make the application available for other operating systems. This behavior naturally leads to users on the other platforms complaining about the lack of a native feel for the application.

Installation

First impressions are vital, and the installation procedure for your application is your only first impression. If the user has to go through an arcane ritual to get your software installed, it already has that "non-native" feel to it. But what is considered an acceptable installation procedure? Unfortunately, that procedure varies completely on each system. Here are a few examples.

Windows

Windows users expect an installer. Simple as that. They expect an application that puts all the little bits in the right places when they run it and places an icon where they want it. They expect the installer to ask them where they want the icons and to only put them there. They also expect this installer to put the right information into the registry so that if they want to uninstall the application it will properly remove all its bits and pieces.

Fortunately, there are several very good Windows installers available on the market today. Some of them are even free or available at a small cost. If Windows is a primary target for your application, it is worth it to not only invest in a good installer but also to make sure you understand it fully so that there are no mishaps. A failed uninstall on Windows is almost as bad as a failed install. An improper uninstall tends to leave a bad aftertaste and might keep the user from selecting any other software from the same developer.

OS X

Arguably OS X has the easiest install available. Users expect to just drag the application to wherever they want it on their computer and just double-click the icon. Anything more than that feels wrong and overly complicated to the user. OS X expects an application to be completely self-contained inside an application directory structure. From the user’s point of view, the application will be just a single icon because OS X automatically treats the folder structure as if it were a single file when it has an .app extension. Unfortunately, there is more to creating a properly built OS X application than just putting everything in the right directory. Assuming that you have access to an OS X machine on which to build your application, there is a simple ant build script that will produce the proper structure for the application bundle:

 <target name="dist.mac" depends="all">
  <mkdir dir="${dist}/${app}"/>
  <property name="appdir" value="${dist}/${app}/${app}.app"/>
  <mkdir dir="${appdir}/Contents/Resources/Java"/>
  <mkdir dir="${appdir}/Contents/Resources/logs"/>
  <copy file="${packaging}/JavaApplicationStub" 
   todir="${appdir}/Contents/MacOS"/>
  <exec command="chmod 755 ${appdir}/Contents/MacOS/JavaApplicationStub"/>
  <copy file="config/log4j.properties" todir="${appdir}/Contents/Resources"/>
  <copy file="config/Info.plist" todir="${appdir}/Contents"/>
  <copy file="images/${app}.icns" todir="${appdir}/Contents/Resources"/>
  <copy todir="${appdir}/Contents/Resources/images">
   <fileset dir="images" includes="*png"/>
  </copy>
  <copy file="dist/${app}.jar" todir="${appdir}/Contents/Resources/Java"/>
  <copy todir="${appdir}/Contents/Resources/Java">
   <fileset dir="lib"/>
  </copy>
  <exec dir="${dist}" executable="hdiutil">
   <arg line="create -ov -srcfolder ${app} ${app}.dmg"/>
  </exec>
  <delete dir="${dist}/${app}"/>
 </target>

This fairly straightforward ant task not only builds the entire file structure needed for the application to be properly executed on OS X but also stores the created application in a disk archive. While this task makes fairly heavy use of variable substitution, the names of the variables should make it clear exactly what it is copying and to where.

This must be executed on an OS X machine because of the launcher that is copied into the file structure. Without that, it would be possible to create this entire thing on any machine. In fact, if you had a copy of the JavaApplicationStub on another machine, it would be possible to build it without access to an OS X machine. You would simply need to remove the hdiutil step and zip or tar up the finished app instead.

Included in this ant build file is the Info.plist file, which is the file that OS X turns to when it needs information about the application. A sample of this file is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
 <dict>
  <key>CFBundleName</key>
  <string>MyApp</string>
  <key>CFBundleVersion</key>
  <string>10.2</string>
  <key>CFBundleAllowMixedLocalizations</key>
  <string>true</string>
  <key>CFBundleExecutable</key>
  <string>JavaApplicationStub</string>
  <key>CFBundleDevelopmentRegion</key>
  <string>English</string>
  <key>CFBundlePackageType</key>
  <string>APPL</string>
  <key>CFBundleSignature</key>
  <string>????</string>
  <key>CFBundleInfoDictionaryVersion</key>
  <string>6.0</string>
  <key>CFBundleIconFile</key>
  <string>MyApp.icns</string>
  <key>Java</key>
  <dict>
   <key>WorkingDirectory</key>
   <string>$APP_PACKAGE/Contents/Resources</string>
   <key>MainClass</key>
   <string>com.zarrastudios.Main</string>
   <key>JVMVersion</key>
   <string>1.3+</string>
   <key>ClassPath</key>
   <array>
    <string>$JAVAROOT/myapp.jar</string>
    <string>$JAVAROOT/commons-logging.jar</string>
   </array>
   <key>Properties</key>
   <dict>
    <key>apple.laf.useScreenMenuBar</key>
    <string>true</string>
    <key>com.apple.hwaccel</key>
    <string>true</string>
   </dict>
  </dict>
 </dict>
</plist>

This simple xml file gives OS X all the information it needs about the application, its icon, its properties, and the location of all files to properly build the classpath prior to execution.

Solaris

Sun has its own packaging system and recommends that deployments for Solaris use their packages instead of just a tar file. Fortunately, its system, although arcane, is simple to build against once you are familiar with it. I have yet to produce an ant build script for Sun and I generally run the steps from the command line. First, it requires two files that will describe the package.

The first file is called pkginfo and it looks like this:

PKG="MyAppPackage"
NAME="MyApp"
ARCH="sparc"
VERSION="1.00"
CATEGORY="application"
VENDOR="zarrastudios.com"
EMAIL="nobody@nowhere.com"
PSTAMP="Marcus S. Zarra"
BASEDIR="/usr/local"
CLASSES="none"

The second file is called prototype:

i pkginfo=./pkginfo-client
d none /usr 0755 root other
d none /usr/local 0755 root other
d none /usr/local/MyApp 0755 root other
d none /usr/local/MyApp/conf 0755 root other
d none /usr/local/MyApp/lib 0755 root other
f none /usr/local/MyApp/lib/example.jar 0644 root other
f none /usr/local/MyApp/conf/log4j.xml 0644 root other

With these two files in hand, it is now just a matter of executing two command-line programs to build the package:

pkgmk -r ´pwd´ -o MyApp

This produces the actual package. Once it is created, the next step is to make it transportable:

cd /var/spool/pkg
pkgtrans -s ´pwd´ /tmp/MyApp
  <<Select MyApp from the menu>>
cd /tmp
gzip MyApp

Now the completed application is sitting inside a gzipped Sun package and is ready for deployment.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020