Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Handling tabs



A GUIApplication has an API allowing to:
  • Add a tab
  • Select a tab
  • Close a tab, all tabs except one, or all tabs
  • Add a close button in tabs
  • Specify that a tab has been modified

Adding a tab

You can add a tab through one of these methods:

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.

Examples

The most simple way to add a tab is (for example in the Basic tutorial:
      public void run() throws Exception {
        JFileChooser chooser = new JFileChooser("Open Image");
        chooser.setDialogTitle("Open Image");
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) {
          File file = chooser.getSelectedFile();
          BufferedImage image = ImageIO.read(file); // the object associated with the tab
          if (image == null) throw new Exception("Bad File type");

          JScrollPane pane = new JScrollPane(new ImagePanel(image)); // the content of the panel to show in the tab
          String tabName = file.getName(); // the name of the tab
          app.addTab(pane, image, tabName);
        }
      }
A more elaborate way to do the same thing using a FileProperties instance would be:
   public void run() throws Exception {
     JFileChooser chooser = new JFileChooser("Open Image");
     chooser.setDialogTitle("Open Image");
     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
     if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) {
       File file = chooser.getSelectedFile();
       BufferedImage image = ImageIO.read(file); // the object associated with the tab
       if (image == null) throw new Exception("Bad File type");

       JScrollPane pane = new JScrollPane(new ImagePanel(image)); // the content of the panel to show in the tab
       String tabName = file.getName(); // the name of the tab
          SwingFileProperties properties = new SwingFileProperties(tabName, comp, image);
          app.addTab(properties);
     }
   }
We can also want to add a tooltip on the tab, for example here to show the path of the file (here we choose not to have an Icon on the tab):
   public void run() throws Exception {
     JFileChooser chooser = new JFileChooser("Open Image");
     chooser.setDialogTitle("Open Image");
     chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
     if (chooser.showOpenDialog(app.getApplicationWindow()) == JFileChooser.APPROVE_OPTION) {
       File file = chooser.getSelectedFile();
       BufferedImage image = ImageIO.read(file); // the object associated with the tab
       if (image == null) throw new Exception("Bad File type");

       JScrollPane pane = new JScrollPane(new ImagePanel(image)); // the content of the panel to show in the tab
       String tabName = file.getName(); // the name of the tab
       String path = file.getAbsolutePath();
          SwingFileProperties properties = new SwingFileProperties(tabName, comp, image);
          app.addTab(null, properties, path);
     }
   }

Close a tab


You can close the selected tab by one of the following methods: Note that the user will also be able to close a tab by clicking on the cross on the tab, depending on the closable tabs behavior.

Closable tabs

The AbstractMDIApplication.hasClosableTab(boolean hasClosableTab) allows to specify that the tabs must have a close button (there is no close button by default):
closabletabs
For example the following application has closable tabs:
   public class MyApplication extends AbstractMDIApplication {      
     public MyApplication() {
       super("My Application");
       this.hasClosableTab(true);
     }      
   }

Note that you can still close tabs even if the tabs do not have a close button, but you must provide the tab popup menu yourself in that case.

Showing modified tabs

The GUIApplication has an API to specify that a tab has been modified. The TabbedApplication.setModified(String name, boolean modified) and TabbedApplication.setCurrentTabModified(boolean modified) allow to define that a tab content has been modified:
  • Setting the boolean as true will show the tab as modified (with a trailing "*" character
  • Setting the boolean as false will show the tab in the default unmodified state

modifiedtabs

Listen to tab updates

The MDIApplicationListener interface allows a listener to be notified when a tab is eelected or is closed.

This interface is registered with the TabbedApplication.addApplicationListener(MDIApplicationListener listener) method.

The interface has the following methods:

Registering the listener

You register the listener by calling the TabbedApplication.addApplicationListener(MDIApplicationListener listener) method.

fireTabBeforeClosing usage

The method MDIApplicationListener.fireTabBeforeClosing(FileProperties properties) returns a boolean specifying if the tab can be closed. By default the method returns true, but it is psosible to override the method, for example to add a popup allowing the user to cancel the close after clicking on the cross on the tab.

fireUpdateSelectedTab usage

The method MDIApplicationListener.fireUpdateSelectedTab(FileProperties properties) is different from the MDIApplicationListener.fireTabChanged(FileProperties properties) method because another selected tab will be set after removing a tab.

For example, if you have two tabs:
  • FileProperties prop1
  • Currently selected tab: FileProperties prop2
and you remove the second tab:
  • You will receive a fireTabRemoved with prop2
  • You will receive a fireUpdateSelectedTab with prop1, because prop1 becomes the currently selected tab
If you have ony one tab:
  • Currently selected tab: FileProperties prop1
and you remove the tab:
  • You will receive a fireTabRemoved with prop1
  • You will receive a fireUpdateSelectedTab with null, because there is no more selected tab

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