Pergunta

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?

Foi útil?

Solução 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.

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top