OOP-ResearchMake It Simpler by Object Oriented Programming

OOP XMLPanelEdit (Page 2): Java Swing GUI tool / Java Swing GUI builder / Java Swing GUI by XML / XML Editor for Java Swing GUI

XMLPanelEdit consists of Java Swing GUI builder and related classes. This Java Swing GUI tool is the XML editor for XML of Swing GUI. By this XML editor, you can easily define Java Swing GUI in XML.

JPanel on JFrame or JDialog can have any Layout, i.e. FlowLayout, BorderLayout, BoxLayout and GridLayout. So, 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. 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

-- Our Java Swing GUI can alter its representation on the fly!
-- We are not required to know about XML at all!
-- Easy internationalization (i18n)!

With XMLPanelEdit, all we have to do is

  1. to define the structure of our Java Swing GUI,
  2. to save it as the XML document
  3. and to generate our Java Swing GUI based on the XML document within our Java programs.


Go Site Map

The procedure 1 and 2 above is achieved by the Class, com.oopreserch.xml.edit.XMLPanelEdit, which acts as the XML editor and the viewer for our XML documents representing our Java Swing GUI.

Image of XMLPanelEdit
View Full image

As you see in the above picture, it is the easy-to-use XML editor written in Java. The upper half JTree represents the whole structure of our Java Swing GUI and we can edit the properties of the selected element in the lower part of the window. Inserting the child element can be done through clicking the button and selecting the component you wish to add from the dialog shown below.

Image of SelectDialog
View Full image

Note that only the appropriate Java components will be listed on the dialog, based on the selected element on the upper side JTree. And moreover, our Java Swing GUI can be viewed through the GUIViewDiag of the XML editor.

Go Site Map

The 3rd procedure can be done with the Class, com.oopreserch.xml.util.GUIParser_1_0, which offers some simple methods as follows:


    public synchronized void setPanel(org.w3c.dom.Element el,
                                      JComponent panel)

    public JComponent getGUI(String name)

    public Action getAction(String name)
    


After our XML document is parsed through DOM parser, we retrieve the root element from the parsed document object and pass it as the first argument of setPanel(org.w3c.dom.Element el, JComponent panel) along with the instance of JPanel as the second argument. Then, the child elements specified in the XML document are placed on JPanel according to the specified Layout. Our code may look like the following:

/*
 * The child components are added onto 'panel'.
 */ 
 JPanel panel=new JPanel();
 String url="xml/our_gui.xml";
 GUIParser_1_0 gui=new GUIParser_1_0();
 DOMParser parser=new DOMParser();
 ErrorHandler e_diag=new XMLErrorDiag("WatchDog :XML Error");
 try{
  /*
   * Configure the parser
   */
   parser.setFeature(
   "http://xml.org/sax/features/validation",true);
   parser.setFeature(
   "http://apache.org/xml/features/allow-java-encodings",true);
   parser.setErrorHandler(e_diag);
   /*
    * Parse our XML document, "xml/our_gui.xml".
    */
   parser.parse(url);
   /*
    * Get the parsed document object.
    */
   org.w3c.dom.Document doc=parser.getDocument();
   /*
    * Get the 'root' element from
    * the parsed document object.
    */
   org.w3c.dom.Element root=doc.getDocumentElement();
   /*
    * Initialize the child components
    * and place them on to 'panel', the instance of JPanel.
    */
   gui.setPanel(root,panel);
 }
 catch(SAXException ex){
   e_diag.println(ex.toString());
 }
 catch(DOMException ex){
   e_diag.println(ex.toString());
 }
 catch(IOException ex){
   e_diag.println(ex.toString());
 }



Now the instance of JPanel is the same one just as we saw on the GUIViewDiag of the XML editor while editing our XML document.
And given the root element of our XML document, we can call setPanel(org.w3c.dom.Element el, JComponent panel) at anytime and anywhere in our programs, which means our Java Swing GUI can be altered on the fly! The best example of this is GUIViewDiag, which represents our Java Swing GUI while editing our XML document. The following is the quote from its source code:

    public void setGUI(org.w3c.dom.Element root){
	gui_panel.removeAll();
	gui_panel.setBorder(null);
	gui.setPanel(root,gui_panel);
	gui_panel.validate();



When we click the View button on the XML editor, the XML editor retrieves the root element of the XML document which currently we are editing, and pass it into the GUIViewDiag. Then, GUIViewDiag can represents our Java Swing GUI at that time.
But how can we set the Model and add the EvenetListner to each child component? In other words, how can we get the references to child components? It is achieved by the method of getGUI(String name). In the XML documents, the unique name should be given to each child components, and we can specify this name for retrieving the references to them. It looks like the following:

	JButton close_b=(JButton)gui.getGUI("close_b");
	close_b.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent ev){
		closeChild();
	    }
	});



As for the JToolBar, the child components are added as the instance of Actions. In this case we use getAction(String name) instead of getGUI(String name). And the returned Action can be embed into JMenuItem on JMenu. By this way, we can toggle (enable/disable) the JButton on JToolBar and JMenuItem on JMenu at the same time.

But as you notice, the 3rd procedure requires us to know how to parse the XML document. But don't worry. The following steps may always be the same to almost all the programs:

  1. Parse the XML document by the parser.
  2. Get the parsed document object from the parser.
  3. Retrieve the root element from the parsed document object.
  4. Placing the child components onto the instance of JPanel by setPanel(org.w3c.dom.Element el, JComponent panel).
So, the 3 abstract Classes are included in XMLPanelEdit for these tedious works. They are:
  • com.oopreserch.xml.util.XMLPanel_1_0
  • com.oopreserch.xml.util.XMLFrame_1_0
  • com.oopreserch.xml.util.XMLDialog_1_0
If we define the subclass of them, our subclass reads the XML and generate the Java Swing GUI automatically. Our subclasses must define the method of:
  • public void iniParts()
and should override:
  • public void invokeAction(ToolBarEvent tbe)
in case that JToolBar is placed on it.

Within the constructor of our subclass, we should invoke readXML( ), which is defined in these abstract Classes and is responsible for the following tasks:
  1. Read the property resource file for ResourceBundle.
  2. Get the URL of the XML document from ResourceBundle.
  3. Parse the XML document by the parser.
  4. Get the parsed document object from the parser.
  5. Retrieve the root element from the parsed document object.
  6. Placing the child Java components onto JPanel, JFrame or JDialog.
So our subclass may be something like the following:

import com.oopreserch.xml.util.XMLFrame_1_0;

public class SubclassFrame extends XMLFrame_1_0{

    public SubclassFrame(){
	super(true);
	readXML();
	String title=res.getString("title");
	setTitle(title);
	iniParts();
    }

    public void iniParts(){
    	JButton beep=(JButton)gui.getGUI("button");
	beep.addActionListener(new ActionListener(){
	    public void actionPerformed(ActionEvent ev){
		System.getToolkit.beep();
	    }
	});
    }
    
}    



Note that the variable res is the instance of java.util.ResourceBundle which is read from property resource file.

For details about property resource file and ResourceBundle, see the related section of JavaTutorial.

The property resource file for each Locale must specify the URL for XML to be read, which means the different XML can be read based on the Locale of the runtime environment. For example, SubclassName_fr.properties may contain the following lines:

  • xml_url=xml/SubclassPanelForFrench.xml
and the referred XML file may contain the French text as the text label on our Java Swing GUI. And, by this way, internationalization (i18n) can be done much easier than ResourceBundle based solution.

Imagine that the 5 JButtons with the text labels are placed on our Java Swing GUI. If we fully depend on ResourceBundle to show the Locale specific text labels, our source code will look like:

	String text_1=res.getString("text_1");
	button_1.setText(text_1);
	String text_2=res.getString("text_2");
	button_2.setText(text_2);
	String text_3=res.getString("text_3");
	button_3.setText(text_3);
	String text_4=res.getString("text_4");
	button_4.setText(text_4);
	String text_5=res.getString("text_5");
	button_5.setText(text_5);



And if the Locale specific texts for accessibility and tooltips must be also specified, more than 3 times of the work will be required!

Which do you want to do?

Go Site Map

Benefits of XMLPanelEdit

With XMLPanelEdit:

  • we can move the GUI-related part of our source code into the XML document. This makes it possible that we can concentrate on designing how our Class should work, without bothering about how it will be represented on the screen, and we can modify our GUI without touching any part of our source code.
  • we can edit our XML documents which represent our Java Swing GUIs quickly by the easy-to-use XML editor. And, by extending the well-defined abstract Classes, Java Swing GUI can easily be generated from those XML documents. While XMLPanelEdit is based on XML technology, we are not required to know about XML at all.
  • our Java Swing GUI can alter its representation on the fly.
  • internationalization (i18n) is easily achieved. We are no longer need to rely wholly on ResourceBundlethe from the property resource file.
And as a result, we can take the full advantage of OOP and build up the enterprise oriented GUI easily!

Go Site Map

Example

At glance, XMLPanelEdit seems to be somewhat complicated tool and API. But, who in the world develops the tool or API to make things difficult? And, in fact, you can easily develop the attractive Swing based application by XMLPanelEdit. There are some examples listed below:

Please look into the source code of these examples. To try them, you will need to install OOP XMLPanelEdit. You can download the 30 days trial version from our online shop at FREE. Let's download it and try.

Go Site Map

Documentation

For details about OOP XMLPanelEdit, please read through:

As always, The Java Tutorial from Sun will shed the light on your Java development. Specifically, the sections below will be helpful for your Swing GUI: If you consult these tutorials while exploring the examples for XMLPanelEdit, you will be soon familiar with Swing. So, even if this is your first step to Swing, don't hesitate to try XMLPanelEdit. Just try, then you'll get something!

Go Site Map Read More

Latest version: 2.1

The latest version of XMLPanelEdit is 2.1. The earlier version depends on Apache Xerces, but it is no longer required. Instead, XMLPanelEdit Version 2.0 (and later) depends on JAXP that is shipped with JDK 1.4.

The distribution file of your application developed by XMLPanelEdit Version 2.0 (and later) can be much smaller in its size now. But, at the same time, it can work only on JRE 1.4 or later. If you need to make your applications compatible also with JDK 1.3, the earlier version of XMLPanelEdit will be required. In this case, please feel free to contact us.

Go Site Map

Download

This API is now a part of Bento framework. There are the better documentations and source code examples on the web site for Bento framework. There, you will also find the link to download the entire framework or each API in it.

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.