The XML GUI Engine

Home
Switching from SwiXml
Examples
Downloads
Project on Sourceforge

In this example, you will learn about the basic features of SwixNG. If you want to get into the system easily, I recommend you start up your development environment now, get the latest version of the SwixNG and SwixNG_Swing libraries and create a new Java Project using them.


Creating a SwixNG application

First, we will create the most simple Application: a simple frame.

<frame id="app" title="My Application" size="600,400"
	defaultCloseOperation="JFrame.EXIT_ON_CLOSE">
</frame>
				
test.sng

The basic Application class looks like this:

public class Test {
	ObjectEngine swix = new SwingEngine(this);

	public Window app;	// gets set by the engine

	public Test() throws Exception {
		swix.render("test.sng");
		app.setVisible(true);
	}

	public static void main(String[] args) throws Exception {
		new Test();
	}
}
				
Test.java

The app field of your class will be set automatically by the engine. This works for each and every object that's defined in your XML descriptor. Give it an ID, create a public field of the same name in your client class and you have it! Now, if you run the application, it will display an empty frame with the title "My Application". If you close the window, the application is automatically terminated.


Using Actions

Now that we got a nice frame, we want to add a possibility to interact with our program. Therefore you have to define an action in your java code:

public class Test {

	public Action exitAction = new AbstractAction() {
		public void actionPerformed(ActionEvent event) {
			System.exit(0);
		}
	};

	...
}
				
Test.java

We could create a menu entry for this, or a little button. Thinking about it, we will do both:

<frame id="app" title="My Application" size="600,400"
	defaultCloseOperation="JFrame.EXIT_ON_CLOSE">
	<menubar>
		<menu text="File">
			<menuitem text="Exit" action="exitAction" />
		</menu>
	</menubar>
	<button text="Exit" action="exitAction" />
</frame>
				
test.sng

For now, just start the application and look how the interaction works.


Simple Localization

The next step is to localize your application, i.e. make the UI multi-language-capable. For hotline support, it's sometimes crucial to have the same application for multiple languages. Let's go ahead and give them the possibility to localize your UI at runtime. First of all, you need the actions that switch between the languages. To ease this, we'll try another scheme to create actions - a parametrized delegate:

public class Test {

	public void setLang(Locale locale) {
		swix.setLocale(locale);
	}

	...
}
				
Test.java

Next, let's create the menu entries:

<frame id="app" title="My Application" size="600,400"
	defaultCloseOperation="JFrame.EXIT_ON_CLOSE" bundle="test">
	<menubar>
		<menu name="mLanguage">
			<menuitem text="English" action="setLang(Locale.ENGLISH)" />
			<menuitem text="Deutsch" action="setLang(Locale.GERMAN)" />
		</menu>

	...
</frame>
				
test.sng

Note how we added a "bundle" attribute to the frame. As you can see, we've given the new menu a "name" attribute, but omitted the "text" attribute. This is because the text now gets read from the properties! The property string to define is "mLanguage.text", i.e. "<name>."<attribute>". If there is no "name" attribute, the engine accepts the ID of the element as well. The still existing "text" attributes of the exit button and menu item will also be used for localization: they are used to directly point to a string in the properties files.

The one big step we have to take now is to define the properties files for english and german. To enable people in different countries to use the program at least in english, we put the localized strings into the default bundle:

app.title=Example Program
File=File
mLanguage.text=Language
exit.text=Exit
				
test.properties

And now the german texts:

app.title=Beispielprogramm
File=Datei
mLanguage.text=Sprache
Exit=Beenden
				
test_de.properties

Now start your program again and look how the language of the whole GUI changes at a single click.


Further reading

There are more examples to come, including, but not limited to:

  • Working with includes
  • Localizing model classes
  • Using Layouts
  • Adding custom Components
  • Using the Plug-in mechanism

Be sure to check back and get the latest examples as they are online. Also, you can cget the "SwixNG_Samples" package and discover the nuts and bolts of the engine on your own. Have Fun!

SourceForge.net Logo
SwixNG (c) 2004 Michael Klaus
All trademarks are property of their respective owners.