Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

FileProperties



The SwingFileProperties class allows to specify the component which will be put in a GUI application tab. This is a generic class which can be used in both JavaFX and Swing applications.

This article explains how to use the SwingFileProperties class. This class extends the generic FileProperties class which has the same characteristics.

Overview

The SwingFileProperties class allows to set:
  • The name of the tab
  • The associated component
  • The underlying object
  • If the tab is closeable
  • If the tab is editable

It is also possible to add an optional URL to set the path of the file associated with the properties if necessary.

Example

For example, in the Basic tutorial, we add a tab for an Image:
   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);
     }
   }
You see that:
  • The name argument is the name of the file used for the tab name
  • The image argument is the underlying object (which in this case is an Image)
  • The pane argument is the component (in this case a ScrollPane) which will be put in the tab

Setting the associated tab as not closeable

Main Article: Handling tabs

The following methods allow to create the associated tab as not closeable:
By default the tab is closeable.


For example:
      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, false);
          app.addTab(properties);
        }
      }

Setting the tab as not closeable

Main Article: GUI application

By default all tabs are closeable. The following methods allow to create the associated tab as not closeable: For example:
      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, false);
          app.addTab(properties);
        }
      }

By default not closeable tabs can not be forced closed (when selecting "Close All Tabs", or "Close Other Tabs"). The TabbedApplication.getDefaultForceCloseTab() can be overriden allows to change this behavior.

Setting the associated message area as editable

Main Article: Message area type

By default the tab is not editable. The following methods allow to create the associated tab as editable: For example:
      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, false, true);
          app.addTab(properties);
        }
      }

This option will have an effect only if the Message area type has the MessageAreaType.MESSAGEAREA_EDITABLE_PER_TAB component.

See also


Categories: Gui | Swing

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