Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

JFileChooser is not working in an applet

  Asked By: Jaxson    Date: Jan 17    Category: Java    Views: 3635
  

I have a web application and I need to load all files
in a directory. I worte an applet to do that. The applet
can correctly show up, but when I click the Open directory
button nothing happens (no file chooser pops up). I dont
know why. I have attached my applet code below.

--------------------------------------------------------
public class DirectoryLoaderApplet extends JApplet
implements ActionListener

{

protected JButton openButton, loadButton;
protected JTextField directoryfield;
protected JFileChooser fc;
protected boolean chosen = false;

public DirectoryLoaderApplet()
{
}

public void init()
{
setContentPane(makeContentPane());
}

public Container makeContentPane()
{
directoryfield = new JTextField("", 40);
directoryfield.setMargin(new Insets(5, 5, 5, 5));
directoryfield.setEditable(false);
directoryfield.setBackground(Color.white);

//fc = new JFileChooser();
//fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

openButton = new JButton("Open");
openButton.addActionListener(this);

loadButton = new JButton("Load");
loadButton.addActionListener(this);

JPanel pane = new JPanel();
pane.setBackground(Color.red);
pane.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.gridwidth = 2;
gbc.insets = new Insets(3, 0, 5, 0);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
pane.add(directoryfield, gbc);

gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.gridwidth = 1;
gbc.insets = new Insets(0, 0, 0, 5);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
pane.add(openButton, gbc);

gbc.gridx = 1;
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.weighty = 0.0;
gbc.gridwidth = 1;
gbc.insets = new Insets(0, 5, 0, 0);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
pane.add(loadButton, gbc);

pane.setBackground(Color.white);
return pane;
}

public void actionPerformed(ActionEvent e)
{
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//Handle open button action.
if (e.getSource() == openButton)
{
int returnVal = fc.showOpenDialog
(DirectoryLoaderApplet.this);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
directoryfield.setText(file.getPath());
chosen = true;
}
directoryfield.setCaretPosition(directoryfield.getDocument
().getLength());
}
else if (e.getSource() == loadButton)
{
if (chosen)
{
// do the job
File file = fc.getSelectedFile();
System.out.println(file.getPath());
File[] files = file.listFiles();
for (int k = 0; k < files.length; k++)
{
if (!files[k].isDirectory())
System.out.println(files[k].getName());
}

chosen = false;
directoryfield.setText("");
}
}
}
}

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Faiza Mian     Answered On: Jan 17

Your problem lies with java applet  restrictions. You cannot access
files from an applet. This would be a huge security leak if you
could. You have to sign your applet to be able to access files  from
the users computer through an applet.

 
Didn't find what you were looking for? Find more on JFileChooser is not working in an applet Or get search suggestion and latest updates.




Tagged: