Tutorial for XMLPanelEdit: Java Swing GUI by XML > How to write Java Swing GUI in XML
Previous Page Contents Next Page
OOP XMLPanelEdit home page

Contact Us

How to write Java Swing GUI in XML

1: How to write Java Swing GUI in XML

The remarkable points of Java Swing GUI are:

Based on these facts, DTD for XMLPanelEdit is defined. It is available at the link below: In this section, I'll describe about some important points of this DTD. By the easy-to-use XML editor (which is included in XMLPanelEdit), you can write your Swing GUI in XML, even if you are not familiar with XML/DTD. But, I recommend you to read through this section.

1.1: How to represent the tree-structure

1.1.1: The root element of the tree-structure

To represent the tree-structure of Java Swing GUI, the root element must always be the same kind of Java component.
Because JPanel can be placed on JFrame or JDialog, JPanel is always the root element of XML.

1.1.2: Intermediate-level container classes

As a child component, any Java components can be placed on the root element (JPanel). JButton, JLable or JTextField is the typical child component. But, 'intermediate-level' container (such as JTabbedPane or JScrollPane) is also the child component in the XML. Moreover, another JPanel can be the child component of the root JPanel. In this case, we can think of the child JPanel as 'intermediate-level' container class.
Each 'intermediate-level' container class has the rule about its child component. The tables below show them:

Intermediate class Chid component(s) The number of chid component(s)
JPanel JPanel
JToolBar*1
JTabbedPane
JScrollPane
JSplitPane
Glue (Horizontal or vertical)
RigidArea
Other components
As many as you like
JTabbedPane JPanel As many as you like
JScrollPane JPanel
Other components
Only one
JToolBar Action
Other components
ActionSpace
As many as you like
JSplitPane JPanel
JScrollPane
Other components
Always two

*1: JToolBar can be added into JPanel only when JPanel has the layout of 'BorderLayout'. And in such a case, only one JToolBar can be added on one of the 4 corners of BorderLayout,i.e. either on NORTH, EAST, SOUTH or WEST.

1.1.3: JToolBar and its children

JToolBar is the special kind of 'intermediate-level' container class, because its child component is 'Action' object. 'Action' object appears as JButton on JToolBar, but it can also be JMenuItem on JMenu. Because the pair of JButton and JMenuItem comes from the same 'Action' object, they can be activated/disabled at the same time. This is very useful in many cases. In the subsequent section, I'll describe how to play with 'Action' object.


1.2: What attributes can be set on each components

1.2.1: Layout, Size, Border, Insets and Dimension

Let's look at the quote from the DTD:

   <!ENTITY % panel.mdl
      "layout?,size?,border?,toolbar?,panel.child*">

   <!ENTITY % tab.pane.mdl
      "size?,border?,tab.pane.child+">

   <!ENTITY % component.mdl
      "size?,border?">

As you notice, the DTD defines Layout, Size, Border, Insets and Dimension as the child components.
But, when you work with Swing GUI by XML editor, please think of Layout, Size, Border, Insets and Dimension as the special kind of attributes.

The table below lists the available attributes for each component:

Component Attributes
JPanel Layout
Size
Border
JTabbedPane Size
Border
JScrollPane Size
Border
JSplitPane Size
Border
JToolBar Size
Border
Insets
Action Size
Border
ActionSpace Dimension
RigedArea Dimension
Other components Size
Border

The following subsections describes about each attributes.

1.2.1.1: Layout

Layout must be one of:

Each layout has its own attributes:
FlowLayout
We must specify how components are aligned by one of the following constants:
CENTER LEADING LEFT RIGHT TRAILING
BoxLayout
We must specify the direction of placing the components. It is assigned by:
X_AXIS Y_AXIS
GridLayout
We must specify the number of columns and the number of rows. We can also specify the gap between each component by the horizontal gap and the vertical gap.
BorderLayout
We can specify the gap between each component by the horizontal gap and the vertical gap.

1.2.1.2: Size

Any of three kind of sizes can be set as the Size. They are:

And both of the width and the height must be specified for each of them.

1.2.1.3: Border

Border must be one of:

In case of TITLED border, we must specify the title of the border. Any string can be assigned as the title. For any border, we can specify the insets of the border, i.e. how far from the edge of the component the border is rendered. The 4 integer must be specified for the insets: TOP, BOTTOM, LEFT and RIGHT.

1.2.1.4: Insets

Insets attribute specifies how far form the edge lines of the component. This means the 4 integer must be specified for this attribute: they are TOP, BOTTOM, LEFT and RIGHT.

1.2.1.5: Dimension

Dimension attribute consists of the width and the height. We must specify the integers for both of them.

1.2.2: Other attributes

The attributes other than Layout, Size, Border, Insets and Dimension are well-known, because they are the 'Java-Beans properties'. You can easily guess what they mean.
The table below lists the available attributes for each component:

Component Attributes
JPanel isOpaque
Name of panel
X alignment
Y alignment
Label text
URL for image file
Position in border layout
Name for accessiblity
Description for accessibility
Foreground color
Background color
JScrollPane isOpaque
Name of component
Horizontal block increment on scroll
Horizontal unit increment on scroll
Vertical block increment on scroll
Vertical unit increment on scroll
X alignment
Y alignment
Position in border layout
Name for accessibility
Description for accessibility
Foreground color
Background color
JTabbedPane isOpaque
Font name
Font style
Font size
X alignment
Y alignment
Position in border layout
Name for accessibility
Description for accessibility
Foreground color
Background color
JToolBar isOpaque
Direction of toolbar
isFloatable
Position in border layout
Name for accessibility
Description for accessibility
Foreground color
Background color
JSplitPane isOpaque
Direction of splitpane
Is continuous update on drag
Dividor location
Dividor size
Is quick expantion on click
X alignment
Y alignment
Position in border layout
Name for accessibility
Description for accessibility
Foreground color
Background color
Action isOpaque
Name of component
Label text
URL for image file
Action command
Font name
Font style
Font size
X alignment
Y alignment
Name for accessibility
Description for accessibility
Tooltip text
Foreground color
Background color
Horizontal alignment
Horizontal text position
Vertical alignment
Vertical text position
Gap between text and image
Box glue Direction of box glue
Other components isOpaque
isWordwrap
isEditable
Class name
Name of component
Label text
URL for image file
Accessibility description about image
Action command
Font name
Font style
Font size
X alignment
Y alignment
Position in border layout
Name for accessibility
Description for accessibility
Tooltip text
Foreground color
Background color
Horizontal alignment
Horizontal text position
Vertical alignment
Vertical text position
Gap between text and image

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


Previous Page Contents Next Page
OOP XMLPanelEdit home page

Contact Us


ALL CONTENTS COPYRIGHT 2002, Jun Inamori. All rights reserved.
Any questions and comments are welcome to Jun Inamori .