Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Scout/HowTo/3.9/Using the native AWT FileChooser with Swing clients

< Scout‎ | HowTo‎ | 3.9

The Scout documentation has been moved to https://eclipsescout.github.io/.

This how-to describes how to add use the native (AWT) FileChooser with Swing clients.

Steps

In order to use the native (Windows) file chooser, you need to tell scout to use the AWT file chooser. This is done by overwriting the createFileChooser() method in SwingEnvironment:

@Override
public ISwingScoutFileChooser createFileChooser(Window w, IFileChooser fc) {
   ISwingScoutFileChooser ui = new SwingScoutFileChooser(this, fc, w, true);
   decorate(fc, ui);
   return ui;
}

Unfortunately the SwingScoutFileChooser is broken with regard to the title bar of the file chooser dialog when using the AWT variant: It uses the file extension filter pattern as title string.

In order to fix this, create a new class SwingScoutFileChooserFix which is a 1:1 copy of the original class (derriving from it unfortunately does not work as the methods that need to be fixed are declared private) and rename the class name accordingly.

In that new class, change the showFileChooserAWT() method as follows:

Replace the following code:

    SecurityManager sm = System.getSecurityManager();
    try {
      System.setSecurityManager(null);
      //
      StringBuffer buf = new StringBuffer();
      if (extensions != null) {
        for (int i = 0; i < extensions.length; i++) {
          if (i > 0) {
            buf.append(", ");
          }
          buf.append("*." + extensions[i]);
        }
      }
      if (buf.length() == 0) {
        buf.append("*.*");
      }
      dlg = createFileChooserAWT(m_owner, buf.toString(), openMode);
      System.setSecurityManager(sm);
    }
    catch (Exception e) {

with the following code:

   SecurityManager sm = System.getSecurityManager();
   try {
     System.setSecurityManager(null);
     if (m_owner instanceof Dialog) {
       dlg = new FileDialog((Dialog) m_owner, TEXTS.get(openMode&nbsp;? Strings.Load&nbsp;: Strings.Save), openMode&nbsp;? FileDialog.LOAD&nbsp;: FileDialog.SAVE);
     }
     else if (m_owner instanceof Frame) {
       dlg = new FileDialog((Frame) m_owner, TEXTS.get(openMode&nbsp;? Strings.Load&nbsp;: Strings.Save), openMode&nbsp;? FileDialog.LOAD&nbsp;: FileDialog.SAVE);
     }
     else {
       dlg = new FileDialog(new Frame(), TEXTS.get(openMode&nbsp;? Strings.Load&nbsp;: Strings.Save), openMode&nbsp;? FileDialog.LOAD&nbsp;: FileDialog.SAVE);
     }
     System.setSecurityManager(sm);
   }
   catch (Exception e) {

Accordingly, you want to create a new instance of the SwingScoutFileChooserFix class in SwingEnvironment.createFileChooser():

 @Override
 public ISwingScoutFileChooser createFileChooser(Window w, IFileChooser fc) {
   ISwingScoutFileChooser ui = new SwingScoutFileChooserFix(this, fc, w, true);
   decorate(fc, ui);
   return ui;
 }

Back to the top