OOP-ResearchMake It Simpler by Object Oriented Programming

Example 2: Java Swing example for JApplet by Java Swing GUI Tool / JToolBar on BorderLayout with Actions (JButtons) / JMenuItems on JMenu / DocumentListener with JTextArea

Java Swing example for JApplet by Java Swing GUI Tool. Floatable JToolBar can be placed on BorderLayout with Action. The JMenuItems on JMenu are enabled/disabled along with JButtons on JToolBar. For example, when the user modifies the text on JTextArea, JButton for saving the file should be activated on JToolBar. At the same time, the JMenuItem for saving the file will also be activated in JMenu. To be notified of the modification in JTextArea, DocumentListener should be added into Document of the JTextArea.

This example uses XMLPanelEdit. XMLPanelEdit consists of Java Swing GUI tool (Java Swing GUI builder) and abstract Java classes. Java Swing GUI tool included in XMLPanelEdit is the XML editor for representing the JFC Swing GUI in XML.



You can download the free source code of this example: And please read about XMLPanelEdit and download the trial version of it.

About this example

By this example, you can build Applet which works as the very simple text editor. This example is a slight modification of:

through which you can build some simple text editor from XML. In the original version, the main class was the subclass of XMLFrame_1_0. To make it available as JApplet, the main class is now the subclass of XMLPanel_1_0, which can be embed to ContentPane of JApplet. Because JMenu should be embed into JApplet rather than into JPanel, the main class keeps the reference to the enclosing Applet. For details, please read the above link and explore the source code which you will find under:

  • example_src/

All the XML for Java Swing GUI are same as the original ones, so please read the above link. You will find the XML, property resource files and image files under:

  • example/

You can prepare the XML for another Locale by:

, which consists of the XML editor for Java Swing GUI and a set of abstract classes. You can download the trial version of this tool from:

After you download and extract it, please copy xmlrun_try.jar in the directory where this README.html resides. Then, go to:

  • example_src/

and start to compile the source. You will find the shell script which compiles the source files and places the compiled class tree into:

  • example/

In case of Windows platform, please modify this script suitable for your environment. After you compile the source tree, please go to:

  • example/

Then, there is another shell script for building the single distribution JAR file for the intended Applet. It first copies xmlrun_try.jar as text_editor.jar and update it with the compiled class tree and other required files (XML, property resource files and image files). Again, in case of Windows platform, please modify this script suitable for your environment.

HTML for Applet / Access Privileges

All the classes and required resources (such as XML, property files and image files) can be loaded from the single JAR file. Please place your JAR file under the document base of your web server so that it is available at the URL something like:

  • http://localhost/text_editor.jar

Then, your HTML can point your JAR file by the applet tag like below:


   <applet  width="650" height="450"
      code="com.oopreserch.example.VerySimpleApplet.class"
      archive="http://localhost/text_editor.jar">
   </applet>

Assuming that your HTML is accessible at the URL of:

  • http://localhost/example.html

you can start appletviewer from the console by typing:


[foo]$appletviewer http://localhost/example.html

Because the example Applet requires the privileges for its actions,

  • (HOME)/.java.policy

must define the following permissions:


   grant codeBase "http://localhost/text_editor.jar" {
      permission java.util.PropertyPermission "user.home", "read";
      permission java.lang.RuntimePermission "modifyThread";
      permission java.io.FilePermission "/", "read,write";
      permission java.io.FilePermission "/-", "read,write";
   };

In case of Windows platform, please modify/add the FilePermission for the available disk drives.

Running Applet within IE or Mozilla by JavaPlug-In

In the age of JDK 1.0.x, everything was easy. Because the most popular web browser (at that time, it was Netscape) implemented the JVM which was completely compatible with Sun's JDK. It was a good old day. Everybody can run any Applet within her/his web browser. And the Java developers in the world can publish their Applet without bothering about the web browsers. But, since JDK 1.1.x, no web browsers in the world do not catch up with Sun's JDK. Moreover, Microsoft shipped their web browser with something like Java. Whenever you develop your Applet today, you must take account of the browser compatibility.

But don't worry. Sun ships JavaPlug-In, and most web browsers in the world can take advantage of it. By JavaPlug-In, any Applet can run on Java Runtime Environment (JRE) within the web browsers. And, because JavaPlug-In is distributed by Sun, web browsers can use most up-to-date JVM.

By HtmlConverter, you can convert the original HTML with applet tag to the one ready for JavaPlug-In. Once converted, this Applet can be shown also by popular web browsers, such as IE or Mozilla. You can start HtmlConverter in the console by:


[foo]$HtmlConverter -gui

Then, you will see


GUI of HtmlConverter

You can specify the single HTML file or the directory to be converted.

Caution!
All the APIs for Servlet/JSP introduced by this web site are now included in Bento framework:
  • Simpler than JSTL or Apache Struts
  • MVC framework by HTML
  • Input validation from CGI FORM
  • Easy user authentication
  • Easy localization (L10n)
To download the APIs and source code examples, please visit the web site of Bento framework.


JBuilder 2007


General Information

For Java Development

Java and all Java-based trademarks and logos are trademarks or registered of Sun Microsystems, Inc. in the United States and other countries.


ALL CONTENTS COPYRIGHT 1997-2007, OOP-Research Corporation. All rights reserved.