Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/HowTo/4.0/Using the native AWT FileChooser with Swing clients"

< Scout‎ | HowTo‎ | 4.0
(Created page with "{{ScoutPage|cat=HowTo 4.0}} This how-to describes how to add use the native (AWT) FileChooser with Swing clients. = Steps = In order to use the native (Windows) file cho...")
 
(Replaced content with "The Scout documentation has been moved to https://eclipsescout.github.io/.")
 
Line 1: Line 1:
{{ScoutPage|cat=HowTo 4.0}}
+
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''':  
+
 
+
<source lang="java">@Override
+
public ISwingScoutFileChooser createFileChooser(Window w, IFileChooser fc) {
+
  ISwingScoutFileChooser ui = new SwingScoutFileChooser(this, fc, w, true);
+
  decorate(fc, ui);
+
  return ui;
+
}
+
</source>
+
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.<br>
+
 
+
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.<br>
+
 
+
In that new class, change the '''showFileChooserAWT()''' method as follows: <br>
+
 
+
Replace the following code:
+
 
+
<source lang="java">
+
    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) {
+
</source>
+
with the following code:
+
<source lang="java">  SecurityManager sm = System.getSecurityManager();
+
  try {
+
    System.setSecurityManager(null);
+
    if (m_owner instanceof Dialog) {
+
      dlg = new FileDialog((Dialog) m_owner, TEXTS.get(openMode ? "Load": "Save"), openMode ? FileDialog.LOAD : FileDialog.SAVE);
+
    }
+
    else if (m_owner instanceof Frame) {
+
      dlg = new FileDialog((Frame) m_owner, TEXTS.get(openMode ? "Load" : "Save"), openMod ? FileDialog.LOAD : FileDialog.SAVE);
+
    }
+
    else {
+
      dlg = new FileDialog(new Frame(), TEXTS.get(openMode ? "Load" : "Save"), openMode ? FileDialog.LOAD : FileDialog.SAVE);
+
    }
+
    System.setSecurityManager(sm);
+
  }
+
  catch (Exception e) {
+
</source>
+
Accordingly, you want to create a new instance of the '''SwingScoutFileChooserFix''' class in '''SwingEnvironment.createFileChooser()''':<br>
+
<source lang="java"> @Override
+
public ISwingScoutFileChooser createFileChooser(Window w, IFileChooser fc) {
+
  ISwingScoutFileChooser ui = new SwingScoutFileChooserFix(this, fc, w, true);
+
  decorate(fc, ui);
+
  return ui;
+
}
+
</source>
+

Latest revision as of 07:35, 18 March 2024

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

Back to the top