Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Applet with JFilechooser called from a Javascript blocks paint messages to Browser  RSS feed

 
zapata zane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
------Applet Code Begin:-----
import java.awt.Color;
import java.awt.Graphics;
import java.awt.AWTEvent;
import java.awt.event.AWTEventListener.*;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class SampleApplet extends JApplet
{

boolean allowDirs=false;
boolean allowFiles=true;
boolean hidden=false;

File lastUserDir=null;


public void init()
{

}

public void start()
{
}

public void stop()
{
}

public String selectFiles()
{
String choosenFiles = null;
choosenFiles = new String((String)java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction()
{
public Object run()
{
String choosenFiles=new String();
JFilechooser fc = new JFilechooser();
fc.setFileSelectionMode(allowDirs ? (allowFiles ? JFileChooser.FILES_AND_DIRECTORIES
: JFileChooser.DIRECTORIES_ONLY)
: JFileChooser.FILES_ONLY);
fc.setMultiSelectionEnabled(true);

int returnVal = fc.showOpenDialog(null);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
choosenFiles = "The selected filesnames will be stuffed here";
}

return choosenFiles; //return whatever you want
}
}));
return choosenFiles;
}
};
------Applet Code End:-----
 
Campbell Ritchie
Sheriff
Posts: 55278
156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please say what the problem is. And please use code tags; they make the posting much easier to read.
 
zapata zane
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to create an applet that can:



1) be called from a Javascript
2) displays a file selection dialog for multi-selecting files
3) returns the selected filenames in a string to the JavaScript
I am able to use doPrivileged to apply the necessary security context to launch a JFilechooser and return the filenames selected. However, When the JFilechooser dialog is visible and the user moves the dialog window around the HTML pages dose not receive any repaint messages. They seem to be blocked by the thread that launched the JFilechooser dialog and is probably blocking update events as the dialog is still visible.

I know I need some type of a message pump so the child thread can inform the parent thread and the browser to repaint. However, I don't know how to do this.

Please help.

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When Campbell asked you to use CODE tags he meant that you should post formatted code; as it is, the code is still hard to read because of the lack of indentation.

I would guess that the problem is that the JavaScript call doesn't return until the Java code is done, and thus the processing in the browser is suspended. A way around this would be to start a new thread in Java code that opens the file chooser, and reports the result to the web page once it's done (by calling a JavaScript method or using the Common DOM API). That way, the original JavaScript call would terminate quickly and browser processing could resume right away.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!