Skip to main content

opening URL with JFileChooser

2 replies [Last post]
benoyo
Offline
Joined: 2007-06-27

Hi, How do I open a URL using JFileChooser?

I have been working on an Applet for opening and saving ".stm" (Stella) and ".mdl" (Vensim) files from a fixed location on local disk C and the code is ok. I have failed to transform the JFileChooser to open from and save to a URL so as to perform these actions over our Intranet. How can I make modifications to link to a defined URL rather then C? Thanks in Anticipation
Benedict.
Below is the full code.

//applet to open model files

import javax.swing.JApplet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.*;
import javax.swing.plaf.metal.*;
import java.lang.Runtime;

public class ModelLoader extends JApplet implements ActionListener
{

//declare Variables
ModelFilter fModelFilter = new ModelFilter();
ModelFilter2 fModelFilter2 = new ModelFilter2();
private File fFile = new File ("default.doc");
private JButton b1,b2,b3,b4;
private JPanel x;
private final int APPLET_WIDTH = 400;
private final int APPLET_HEIGHT = 140;
private JFileChooser ob ;

//init() method
public void init()
{
// initialise objects
x = new JPanel() ;
x.setLayout (new FlowLayout());
b1 = new JButton ("View Stella Documents");
b1.addActionListener (this);
//b1.setBounds(5,382,99,25);
b1.setFont(new Font("Dialog", Font.PLAIN, 12));
x.add(b1);
//x.add(Box.createRigidArea (new Dimension (0, 10)));
b2 = new JButton ("View Vesim Documents");
b2.addActionListener (this);
//b2.setBounds(5,382,99,25);
b2.setFont(new Font("Dialog", Font.PLAIN, 12));
x.add(b2);
b3 = new JButton ("Save Stella Documents");
b3.addActionListener (this);
b3.setBounds(112,382,99,25);
b3.setFont(new Font("Dialog", Font.PLAIN, 12));
x.add(b3);
b4 = new JButton ("Save Vesim Documents");
b4.addActionListener (this);
b4.setBounds(413,382,105,25);
b4.setFont(new Font("Dialog", Font.PLAIN, 12));
x.add(b4);

getContentPane().add(x);
setSize(APPLET_WIDTH, APPLET_HEIGHT);
}

public void actionPerformed ( ActionEvent event ) {

if (event.getSource().equals(b1)) {

openStellaFile();
}

else if (event.getSource().equals(b2)) {

openVensimFile();
}
else if (event.getSource().equals(b3)) {

SaveStellaFile();
}

else if (event.getSource().equals(b4)) {

SaveVensimFile();
}
}

public void openStellaFile() {

ob = new JFileChooser ("C:/ob/models");
//ob = new JFileChooser ("http://10.10.1.77/Models");

// Choose only files, not directories
ob.setFileSelectionMode ( JFileChooser.FILES_ONLY);

// Set filter for Java source files.
ob.setFileFilter (fModelFilter);
ob.setAcceptAllFileFilterUsed(false);
// Now open chooser
int result = ob.showDialog (ModelLoader.this,"Load");

if (result == JFileChooser.CANCEL_OPTION) {
ob.cancelSelection();
} else if (result == JFileChooser.APPROVE_OPTION) {

fFile = ob.getSelectedFile ();
fFile.setReadOnly();
// Invoke the readFile method in this class
//ob.setSelectedFile(fFile);
try //try statement
{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + fFile);
} catch (Exception e) //catch any exceptions here
{
System.out.println("Error" + e ); //print the error
}
}

} // openFile

public void openVensimFile() {

ob = new JFileChooser ("C:/ob/models");
//ob = new JFileChooser("http://10.10.1.77/Models");

// Choose only files, not directories
ob.setFileSelectionMode ( JFileChooser.FILES_ONLY);

// Start in current directory
//ob.setCurrentDirectory (new File ("."));

// Set filter for Java source files.
ob.setFileFilter (fModelFilter2);
ob.setAcceptAllFileFilterUsed(false);
//ob.isDirectorySelectionEnabled(false);
// Now open chooser
int result = ob.showDialog (ModelLoader.this,"Load");

if (result == JFileChooser.CANCEL_OPTION) {
ob.cancelSelection();
} else if (result == JFileChooser.APPROVE_OPTION) {

fFile = ob.getSelectedFile ();
// Invoke the readFile method in this class
//ob.setSelectedFile(fFile);
try //try statement
{
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + fFile); //open the file

} catch (Exception e) //catch any exceptions here
{
System.out.println("Error" + e ); //print the error
}
}

} // openStellFile

public void SaveStellaFile() {

ob = new JFileChooser ();

// Choose only files, not directories
ob.setFileSelectionMode ( JFileChooser.FILES_AND_DIRECTORIES);

// Set filter for Java source files.
ob.setFileFilter (fModelFilter);
ob.setAcceptAllFileFilterUsed(false);
// Now open chooser
int result = ob.showDialog (ModelLoader.this,"Upload");

if (result == JFileChooser.CANCEL_OPTION) {
ob.cancelSelection();
} else if (result == JFileChooser.APPROVE_OPTION) {

fFile = ob.getSelectedFile ();

copy(fFile, new File("C:\\ob\\models" + File.separator + fFile.getName()));
System.out.println("Copying done");
//System.exit(0);
}

} // SaveFile

public void SaveVensimFile() {

ob = new JFileChooser ();

// Choose only files, not directories
ob.setFileSelectionMode ( JFileChooser.FILES_AND_DIRECTORIES);

// Start in current directory
//fc.setCurrentDirectory (new File ("."));

// Set filter for Java source files.
ob.setFileFilter (fModelFilter2);
ob.setAcceptAllFileFilterUsed(false);
// Now open chooser
int result = ob.showDialog (ModelLoader.this,"Upload");

if (result == JFileChooser.CANCEL_OPTION) {
ob.cancelSelection();
} else if (result == JFileChooser.APPROVE_OPTION) {

fFile = ob.getSelectedFile ();

copy(fFile, new File("C:\\ob\\models" + File.separator + fFile.getName()));

}

} // SaveFile

//Copies the file
private void copy(File in, File out){

if (out.exists ()) {
int response = JOptionPane.showConfirmDialog (null,
"Overwrite existing file?","Confirm Overwrite",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.OK_OPTION ){
try{
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);

int read;
byte[] buffer = new byte[65536];
while((read = fis.read(buffer)) != -1){
fos.write(buffer, 0, read);
}
}catch(Exception e){
System.err.println("Exception occured:");
e.printStackTrace();
}
}

}
else
{
try{
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);

int read;
byte[] buffer = new byte[65536];
while((read = fis.read(buffer)) != -1){
fos.write(buffer, 0, read);
}
}catch(Exception e){
System.err.println("Exception occured:");
e.printStackTrace();
}
}

}
// creating the model filter classes
class ModelFilter extends javax.swing.filechooser.FileFilter
{

public boolean accept (File f) {
return f.getName ().toLowerCase ().endsWith (".stm")||f.getName ().toLowerCase ().endsWith (".itm");
}

public String getDescription (){

return "STELLA Doc (*.stm) or iThink doc (*.itm)";
}

}

class ModelFilter2 extends javax.swing.filechooser.FileFilter
{
public boolean accept (File f) {
return f.getName ().toLowerCase ().endsWith (".mdl")||f.getName ().toLowerCase ().endsWith (".vmf");
//return f.getName ().toLowerCase ().endsWith (".mdl");
}

public String getDescription (){
return "Vensim Document (*.mdl) or (*.vmf)";

}

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

Unfortunately, this is far from straightforward. There was an [url=http://forums.java.net/jive/thread.jspa?threadID=25145&tstart=105]earlier discussion[/url] in the Swing & AWT forum on this.

benoyo
Offline
Joined: 2007-06-27

I agree there was a discussion on this subject, let me revisit it again. Will keep the forum posted on any improvements.