FAQ

  1. How do I create a simple application using UIC?
  2. How do I use Qt Designer?
  3. How do I use MainWindow as a basis for my application?
  4. How do I include icons on toolbars and menus?
  5. How do I set up an icon repository?
  6. How do I create a context (right click) menu?
  7. How do I create a splash screen?
  8. How do I set up translations?
  9. How do I set up translation from a single file
  10. What is an ActionFactory and how does it make my life easier?
  11. How do I work with the classes generated by compiling my interface design?
  12. What are slots?
  13. What is the "Settings" menu?
  14. How do I show or hide the "Settings" menu?
  15. What is the toolbar configuration menu?
  16. How do I show or hide the toolbar configuration menu?
  17. How do I specify an action target method which takes arguments?
  18. How do I make sure my UI actions happen on the event thread, and my non-UI actions do not?

How do I create a simple application using UIC?

  1. Use Qt Designer to define the look of your application. Compile the .ui files with UIC. [more]
  2. Create a main class for your application which extends MainWindow. [API]
  3. Write an XML file which defines the menus and toolbars used by your application. [API1 API 2]


How do I use Qt Designer?

  1. Download this application.
  2. For you main window, base your design on QWidget. For dialogs, base your design on QDialog. Ignore the other Q... things for now.
  3. Be sure to apply a layout to the entire, unselected, widget when you are done. It doesn't matter whether it is horizontal, vertical, or grid. Click somewhere on the form so NONE of the individual controls is selected, and then pick a layout.
  4. Qt saves the design in an XML file with ".ui" suffix. Compile these into java files with UIC. [more]


How do I use MainWindow as a basis for my application?

  1. Create a class which extends MainWindow. Include a "main" method, which will start your application. [API]
  2. In the "main" method, create your application and go through the basic application lifecycle:
    app = new MainApp();
    app.splash();
    app.init();
    app.start();
    splash displays the splash screen.
    init does whatever preparation is needed - reading data, setting up localization, initializing the ActionFactory, creating screen widgets.
    start puts the application on the screen.


How do I include icons on toolbars and menus?

  1. Tell the ActionFactory where your icon repository is.
  2. When you create an action, include a reference to its icon.
    UICAction action = factory.addAction("file_exit", this, "exit", 
        translator.i18n("E&xit"),
        "org.uic.exampleTwo.images.exitIcon",
        "Alt-F4", 
        translator.i18n("Exit the application"), 
        translator.i18n("Close the application and exit"));


How do I set up an icon repository?


How do I create a context (right click) menu?

  1. Attach a ComponentMouseClickedAction to the component where you want the context menu to appear.
    ComponentMouseClickedAction mouseAction =
        new ComponentMouseClickedAction(this, "mouseClickedSlot");
    mouseAction.setDirect(true);
    mouseAction.add(this); 
  2. Write a method which retrieves the context menu and displays it. This method must decide if the mouse click is requesting a context menu.
    public void mouseClickedSlot(int x, int y, int button){
        if (button == ComponentMouseClickedAction.MOUSE_BUTTON3) {
            ActionFactory actionFactory = MainApp.getActionFactory();
            JPopupMenu popup = actionFactory.getPopupMenu("animalEditor_popup");
            popup.show(this, x, y);
        }
    }
  3. The context menu must be displayed on the event thread. Since it is in reponse to a UI event, if the target method runs on the same thread as the firing event, it will be on the correct thread. The method setDirect(true) takes care of this.


How do I create a splash screen?

    Before you start your application, find and load your splash screen.
  1. Using the class loader, find the image for the splash screen. The getResource call works equally well, whether the image is in the file system or in a jar. Create your new splash screen from this image.
  2. If you are displaying a progress bar, set the progess to zero.
  3. Use setPartnerComponent to tell the splash screen when to close. It will disappear when the component is displayed. Typically, you want this component to be the main JFrame of your application. But it can be any component.

    Here is a method which does these three things. It is a method on the main class for the application, which can extend either JFrame or MainWindow.

    private void splash() {
        ClassLoader loader = this.getClass().getClassLoader();
        URL splashURL = loader.getResource("/images/AnimalSplash.png");
        try {
            mainSplash = new SplashScreen(splashURL,true);
            mainSplash.setProgress(0);
            mainSplash.setPartnerComponent(this.getFrame());
        } catch (IOException e) {
            // no splash found in classpath.  Probaby forgot to add it to the jar.
            e.printStackTrace();
        }
    }


How do I set up translations?


How do I set up translation from a single file?


What is an ActionFactory and how does it make my life easier?


How do I work with the classes generated by compiling my interface design?


What are slots?


What is the "Settings" menu?


How do I show or hide the "Settings" menu?


What is the toolbar configuration menu?


How do I show or hide the toolbar configuration menu?


How do I specify an action target method which takes arguments?


How do I make sure my UI actions happen on the event thread, and my non-UI actions do not?

    Actions can be fired on either the event thread or a regular thread. By default, the action's target method will be called on a regular thread. There are two ways to get a method called on the event thread:
  1. Most actions you use are derived from SwingAction or UICAction, then you can just call setUIUpdateCommand. The method passed in will be called on the event thread after the action's target method completes. [API]
  2. If you know the action will be fired on the event thread, call setDirect(true) on the action. This causes the target method to be called directly from where the action is fired, and not in the actions worker thread. All of the actions which derive from SwingAction are fired by UI activity, so they will be fired on the event thread.