Example 3: Java Swing GUI example by XML / How to use GridLayout and BoxLayout / ActionListener on JComboBox
In this example, we prepare 3 XML for the single Java Swing GUI, JPanel with GridLayout and BoxLayout.
Each XML defines the different number of JLabel and JTextField (or JButton) within the JPanel.
When the user selects the item from JComboBox, ActionListener asks the JPanel to reload the corresponding XML, and the Java Swing GUI will be updated on the fly.
By GridLayout and BoxLayout, the pairs of JLabel and JTextField (or JButton) are placed gracefully in the cell of the matrix within JPanel.
This example uses XMLPanelEdit.
XMLPanelEdit consists of Java Swing GUI tool (Java Swing GUI builder) and abstract Java classes.
Subject : Dynamic contents in JPanel
The main subject of this example is:
The user can selects the level from JComboBox. Based on the selected level, the different set of JTextField will be shown on JPanel.
To update the contents of JPanel, we will prepare the 3 XML, each of which describes the structure of the above GUI. Whenever the item is selected from JComboBox, ActionListener on JComboBox is responsible for asking JPanel to update its contents. Then, JPanel will reload the appropriate XML on demand to update its contents.
In this JPanel, the series of JLabel (or JButton) will be placed along with the available JTextField. By the help of GridLayout and BoxLayout, these pairs of JLabel (or JButton) and JTextField are placed gracefully on the cell of the matrix.
To try this example, you will need to download XMLPanelEdit. You can download the 30 days trial version at FREE from:
This class is the subclass of XMLPanel_1_0, and can generate the 3 different GUI on the fly. Under dist directory, you will find:
Then, open the above XML and explore their structures. You will notice that they are very similar with each other.
To place the JLabel (JButton) along with JTextField, each of these XML defines the nested JPanel, i.e. while the enclosing JPanel has BoxLayout of X_AXIS (the horizontal direction), each of the 2 child JPanel has GridLayout of 1 column. In each XML, these 2 child JPanel have the same number of rows. By this way, JLabel (or JButton) and its corresponding JTextField are horizontally aligned on the same row. The column for JLabel (or JButton) and the one for JTextField are placed into the separated JPanel, because we'd like to make the width of JTextField wider than that of JLabel. If both columns are placed into the single JPanel, its GridLayout will allocate the same width to both of the columns. And this is not what we expect.
You will also find:
The constructor of ChildEditor reads standard.xml as default, but you can let it to reload any of these 3 XML on demand through:
Then, ChildEditor will update its appearance based on your request.
The source of this class is available under src directory. Within the source, please look into the implementation of:
Whenever this method invokes:
, it check if the returned object is null or not. And, only if the returned object is not null, this method casts the returned object to the appropriate type of object and add the ActionListener on it. By this way, ChildEditor can be ready for the different XML.
Anyway, let's compile this class. Under src directory, you will find the 2 UNIX shell script. Please open build_1.sh and edit it if required. This script compiles the source of ChildEditor and places the compiled class files into dist directory.
This class is the main window of this example and the subclass of XMLFrame_1_0.
is invoked, which forces the instance of ChildEditor to reload another XML.
The rest of the implementation of this class is very simple, because this example is intended to show you the way to update the GUI by XML.
Please open build_2.sh and edit it if required. This script compiles the source of PropertyEditor and places the compiled class files into dist directory.
After compilation, please go to dist directory. Then, please open build.sh and edit it if required. This script will prepare the JAR file for the distribution.
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.