0

I'm attempting to get an absolute file path in a web browser. I've learned that it is not possible using plain HTML and javascript, and that a java applet is the best route. Unfortunately, my knowledge of java is rudimentary at best. So far I have this java code:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.Color;
/*
   <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" 
           width=150 height=100 
 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0"> 
     <PARAM NAME="code" value="FileApplet.class">
   </OBJECT>
 */

public class fileabs extends JApplet
{
   private JTextField tfCount;
   final JFileChooser fc = new JFileChooser();

  public void init() {
      setBackground(Color.WHITE);
        JPanel p = new JPanel( new FlowLayout(FlowLayout.CENTER, 15, 15));
        p.add(new JLabel("Select File: "));
        tfCount = new JTextField(50);
        tfCount.setEditable(false);
        p.add(tfCount);
        JButton b2 = new JButton("Browse...");
        p.add(b2);
        b2.addActionListener( new ActionListener(){
            public void actionPerformed(ActionEvent ae) {
               tfCount.setText("dsds");
                int returnVal = fc.showOpenDialog(fileabs.this);
                tfCount.setText(fc.getSelectedFile().getAbsolutePath());
            }
        } );

        // p.add(label);
        add(p);
    }

  public String getFilePath() {
    return tfCount.getText();
  }
}

From what I've read at http://jdk6.java.net/plugin2/liveconnect/#JS_TO_JAVA, I can call applet methods from javascript, so I came up with this test web page:

<html>
    <head>
    </head>
    <body>
        <applet id="fileabs" archive="fileabs.jar" code="fileabs" width="960" height="60"></applet>
        <a href="#;" onclick="test()">Test</a>
    <script>
        test = function() {
            alert(fileabs.getFilePath());
        };
    </script>
    </body>
</html>

However, in the firebug console I get:

TypeError: fileabs.getFilePath is not a function

I feel like I'm missing something obvious. Can anyone help me figure out what's wrong with what I have here?

M Saavedra
  • 71
  • 1
  • 6

2 Answers2

2

You need to get a reference to the applet DOM element first. Try alert(document.getElementById('fileabs').getFilePath());

Scott Heaberlin
  • 3,364
  • 1
  • 23
  • 22
  • 1
    The way the HTML is set up, it will still fail. The reason is that script is called immediately after the applet element is declared, but before the applet is necessarily fully loaded and active. +1 in any case. – Andrew Thompson Apr 16 '13 at 03:01
  • The code structure is based directly on the LiveConnect tutorial I posted a link to above. Though it looks strange to me too, it really does work correctly without using getElementById().As for @Andrew Thompson 's comment, that is not an issue; the only thing in the script tag is a regular function, which doesn't get called until Clicking the "Test" link. By that time, the applet has been loaded and the function can access it. – M Saavedra Apr 18 '13 at 17:29
2

The code works as written. The issue turned out to be a cached version of the applet that didn't have the method I was calling.

M Saavedra
  • 71
  • 1
  • 6