Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Associate types of tabs with icons



It is possible to associate types of tabs to icons at the GUI application level, avoiding to provide the icon in each action.

To do that:
  • You need to register the icons to their asssociated keys. It can be performed anywhere in the code, providing that you have access to the GUI application
  • Then you can use one the the addTab method in the GUI application which allows to secifiy he tab type

Registering the icons to their associated keys

Registering icons to their asssociated keys ust be done through the SwingTabTypeManager associated with the GUI application.

To get the SwingTabTypeManager, you need to call the TabbedApplication.getTabTypeManager() method (the manager is null by default, it will be created with the dirst call of this method).

The SwingTabTypeManager has one usefu method wich can be used to register an icon:
Note that if an Icon has already been registerd to a key, trying to register another icon with the same key will not do anything and the method will return false.


For example:
   SwingTabTypeManager tabTypeManager = getTabTypeManager(); // here the type manager is created
   URL url = // an Icon url
   tabTypeManager.registerIcon(url, "image"); // the icon is registered to the "image" key

Using the icons keys for the tab icons

Several methods allow to set the tab type when adding a tab: Additionally, all methods which use a SwingFileProperties can also make use of the icon type by using the FileProperties.setType(String type) method.

If the type does not correspond to any icon, the tab will not have any icon


For example:
      public class ImportDocumentAction extends AbstractMDIAction {
         public ImportDocumentAction(GUIApplication appli, String name) {
           super(appli, name);
           this.setDescription("Open Document", "Open Document");
         }

         public void run() throws Exception {
           TabbedApplication gui = (TabbedApplication) app;
           JFileChooser chooser = new JFileChooser("Open Document");
           chooser.setDialogTitle("Save Document");
           chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
           if (chooser.showOpenDialog(gui.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) {
             URL url = chooser.getSelectedFile().toURL();
             File file = chooser.getSelectedFile();
             JEditorPane pane = new JEditorPane();
             JScrollPane scroll = new JScrollPane(pane);
             pane.setPage(url);
             SwingFileProperties prop = new SwingFileProperties(file.getName(), scroll, pane.getDocument());
             prop.setType("text");
             gui.addTab(pane, prop);
           }
         }
      }

See also


Categories: General | Gui | Swing

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences