The XML GUI Engine |
||||||
|
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 applicationFirst, 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 Using ActionsNow 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 LocalizationThe 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=Exittest.properties And now the german texts: app.title=Beispielprogramm File=Datei mLanguage.text=Sprache Exit=Beendentest_de.properties Now start your program again and look how the language of the whole GUI changes at a single click. Further readingThere are more examples to come, including, but not limited to:
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! |
|||||
All trademarks are property of their respective owners. |