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:

Close a tab

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

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);
     }
   }

Closable tabs

The AbstractMDIApplication.hasClosableTab(boolean) 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);
    }      
  }

Showing modified tabs

The GUIApplication has an API to specify that a tab has been modified. The TabbedApplication.setModified(String, boolean) and TabbedApplication.setCurrentTabModified(boolean) 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

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