OOP-ResearchMake It Simpler by Object Oriented Programming

Example 1: Your first Java Swing GUI by XML / JToolBar on BorderLayout / JToolBar with Action / JMenuItem on JMenu

Java Swing example for JToolBar with Actions on BorderLayout. The Action on JToolBar can be embed to JMenuItem on JMenu. And JButton on JToolBar and JMenuItem on JMenu can be toggled at the same time.

In this Java Swing GUI example, we will build some very simple text editor.
When the user modifies the text on JTextArea, JButton for saving the file should be activated. And we'd like to place this interactive JButton both on JToolBar and JMenuItem.

To make it possible, we will allocate the same Action to JToolBar and JMenuItem. By enabling and disabling the single Action instance, JButton on JToolBar and its corresponding JMenuItem can be toggled at the same time.
JToolBar should be placed on JPanel with BorderLayout. And to be notified of the modification in JTextArea, DocumentListener should be added into Document of the JTextArea.

The source code of this example is included in the distribution of XMLPanelEdit. The free trial version of XMLPanelEdit is available at our online shop.

When you finish this example, please try other examples in the list below.

Related Pages:

TOC of thie example:


About XMLPanelEdit...

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 place JButton, JCheckBox, JRadioButton, JLable, JTextField or any other JComponents anywhere within JFrame, JDialog or JPanel. The Java components can be placed on the intermediate Swing containers, such as JTabbedPane, JSplitPane and JScrollPane. For example, JTable, JTree, JTextArea or JTextPane can be placed on JScrollPane. JPanel, JFrame or JDialog can have any LayoutManager, i.e. FlowLayout, BorderLayout, BoxLayout and GridLayout. When BorderLayout is specified as the Layout, JToolBar with Actions can be placed on JPanel, and JMenuItems with the same Actions can be embed into JMenu.

You can prepare XML for your Java Swing GUI in as many human languages as you like, i.e. XML for your Java Swing GUI can be written in Japanese Shift_JIS, Chinese BIG5, Chinese GB2312 and Russian KOI8-R. By ResourceBundle read from the property resource file, the XML appropriate for the user's Locale will be selected at runtime. By this way, your Java Swing GUI can be internationalization (i18n) ready.

The abstract Java classes in XMLPanelEdit represent JFrame, JDialog and JPanel. These Java classes parse the XML written by Swing GUI tool and generate your Java Swing GUI. And your Java Swing GUI can access all the JComponents placed on it. For example, ActionListener can be added into JButton on JDialog. Or Actions on JToolBar and their corresponding JMenuItems on JMenu can be enabled/disabled at the same time.

Related pages about XMLPanelEdit:

Subject: Text editor by Swing

In this Java Swing GUI example, we will build some very simple text editor. It looks like this:

Image of simple editor

The JButtons on JToolBar are interactive, i.e. they are enabled and disabled by the user's action. For example, when the user modifies the text on JTextArea, JButton for saving the file should be activated. And we'd like to place these interactive JButtons both on JToolBar and JMenuItem:

Image of simple editor

To make it possible, we will allocate the same Action to JToolBar and JMenuItem. By enabling and disabling the single Action instance, JButton on JToolBar and its coressponding JMenuItem can be toggled at the same time. And to be notified of the modification in JTextArea, DocumentListener should be added into Document of the JTextArea.

JToolBar is floatable and should be placed on JPanel with BorderLayout:

Image of simple editor

When the user clicks the JButton under JTextArea, the JDialog will pop up:

Image of simple editor

Another JDialog will appear when the user tries to exit the editor without saving the modified text. This JDialog will be used to report the error.



Next Page


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.