开发者

SWT FileDialog in Applet does not show up

I am not a SWT user, but I need one of its functionality in JApplet I am working on: FileDialog. And I have problem when I run applet withing a browser: the dialog will not popup, no exception is thrown, nothing (but blink of the window) happens.

Applet is only for Windows users, that is the requirement.

When I run the same applet from appletviewer in Eclipse, it works fine.

I added all the SWT libraries to the PATH in Windows, so they should be available for VM. I searched over the Internet, but I could not find anything that could help me.

The code I am using:

final Display display = new Display();
    final java.awt.Canvas awtParent = new java.awt.Canvas();

    applet.getApplet().add(awtParent);

    final Shell swtParent = SWT_AWT.new_Shell(display, awtParent);
    try {
        FileDialog dialog = new FileDialog(swtParent, SWT.OPEN);
        dialog.setFilterExtensions(getMediaFilesExtensions());
        dialog.setFilterNames(new String[] {"All files", "Media Files"});
        String fileName = dialog.open();
        File[] files = null;
        if (fileName != null) {
            files = new File[] { new File(dialog.getFileName()) };
        }
        return files;
    } finally {         
        display.syncExec(new Runnable () {
            public void run () {
                if (swtParent != null && !swtParent.isDisposed ()) swtParent.dispose ();                    
                display.dispose ();
                applet.getApplet().remove(awtParent);
            }
        });
    }

This code is called while user click on the button. The rest of the application is Swing based.

I work on Windows 7 with SWT 3.7.1. All the DLLs are in java.library.path (%PATH% in windows)

Do you have any advice?

BTW: Swing with native L&F is not an option. I need native file selection dialog.

This is the runnable demo applet I created. It requires: swt-win32-x86-3.7.jar.

package com.applet;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Arrays;

import javax.swing.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.*;

public class DemoApplet extends JApplet {
    @Override
    public void init() {
        JPanel mainPanel = new JPanel();
        JButton button = new JButton(new AbstractAction("Select files") {
            @Override
            public void actionPerformed(ActionEvent e) {
                File[] filesNative = getFilesNative();
                String msg = "Selected files: " + Arrays.toString(filesNative);
                JOptionPane.showMessageDialog(null, msg);
            }
        });
        mainPanel.add(button, BorderLayout.CENTER);
        add(mainPanel);
        super.init();
    }

    private File[] getFilesNative() {
        final Display display = new Display();
        final java.awt.Canvas awtParent = new java.awt.Canvas();

        add(awtParent);

        final Shell swtParent = SWT_AWT.new_Shell(display, awtParent);
        try {
            FileDialog dialog = new 开发者_Go百科FileDialog(swtParent, SWT.OPEN);
            dialog.setFilterNames(new String[] {"All files", "Media Files"});
            String fileName = dialog.open();
            File[] files = null;
            if (fileName != null) {
                files = new File[] { new File(dialog.getFileName()) };
            }
            return files;
        } finally {         
            display.syncExec(new Runnable () {
                public void run () {
                    if (swtParent != null && !swtParent.isDisposed ()) swtParent.dispose ();                    
                    display.dispose ();
                    remove(awtParent);
                }
            });
        }
    }
}


If you want to create a dialog in an applet you must find the parent frame. I ran into this issue recently as well and found this useful: http://www.jguru.com/faq/view.jsp?EID=27423

public Frame findParentFrame(){
    java.awt.Component c = getParent();

    while(true) {
        if(c instanceof Frame)
            return (Frame)c;
        c = c.getParent();
    }
}

This will get you the parent frame, which you pass to the dialog constructor:

FileDialog fd = new FileDialog(findParentFrame(), "Save Building", FileDialog.SAVE);

Note that some browsers might block this as a popup.


If you are using Java SE 6u26, you might be running into Bug 7056092 - updating to Java SE 6u27 fixed the issue of file dialogs not opening in SWT apps for me


"no exception is thrown, nothing (but blink of the window) happens" SWT usually write logfiles instead of throwing exceptions in case of display problems (in generally). I don't know much about SWT based applets, but i wondered, is there any logfile in this case?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜