Skip to content

Instantly share code, notes, and snippets.

@killinterpol
Last active August 29, 2015 14:04
Show Gist options
  • Save killinterpol/7f2a8e318af3f9f51118 to your computer and use it in GitHub Desktop.
Save killinterpol/7f2a8e318af3f9f51118 to your computer and use it in GitHub Desktop.
JFileChooser inside applet
import java.util.ArrayList;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.JFileChooser;
import netscape.javascript.*;
public class myApplet extends JApplet {
private ArrayList<String> imageList = new ArrayList<>();
private void finish() {
JSObject window = null;
try {
window = JSObject.getWindow(this);
String results = null;
if (imageList.size() > 0) { Arrays.toString(imageList.toArray()) };
if (results != null) {
window.eval("appletCallback(" + results + ")");
} else {
window.eval("appletCallback()");
}
} catch (Exception ex) {
//do nothing
}
}
@Override
public void init() {
try {
//set display prefs
setSize(640, 480);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//initialize form components
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception e) {
//e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void initComponents() {
jPanel9 = new javax.swing.JPanel();
btnScan = new javax.swing.JButton();
btnAttach = new javax.swing.JButton();
btnAttach.setText("Attach");
btnAttach.setToolTipText("Attach image(s)");
btnAttach.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAttachActionPerformed(evt);
}
});
jPanel9.add(btnAttach);
}
private void btnAttachActionPerformed(java.awt.event.ActionEvent evt) {
LoadData loader = new LoadData();
loader.addPropertyChangeListener(this);
loader.execute();
}
private class LoadData extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
String filePath = prefs.get("filepath", "");
JFileChooser chooser = new JFileChooser(filePath);
File selection[];
String imgFile = "";
chooser.setCurrentDirectory(lastPath);
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
if (chooser.showOpenDialog(rootPane) == JFileChooser.APPROVE_OPTION) {
selection = chooser.getSelectedFiles();
for (int i = 0; i < selection.length; i++) {
imageList.add(selection[i].getPath());
}
finish();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment