mardi 3 mars 2015

Having trouble with Java's FileChooser



I am working on a science research project and instead of using Excel I wanted to analyze data with my own code. I save a '.txt' file with 10,000 data points inside separated by tabs and hard returns. Now, I am trying to use this snippet of code to better select the file for review, but it never actually get the file for me to send using getFile(), my question is: Why does it loop forever and never get the file for use? And a preemptive thanks to everyone here helping me since I started programming! This site is great.



code import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class SimpleFileChooser extends JFrame {
public File sf;
public SimpleFileChooser() {
super("File Selector");
setSize(350, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JButton openButton = new JButton("Open");
final JLabel statusbar = new JLabel("Select a File");
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
int option = chooser.showOpenDialog(SimpleFileChooser.this);
if (option == JFileChooser.APPROVE_OPTION) {
sf = chooser.getSelectedFile();
}
}
});
c.add(openButton);
c.add(statusbar);
}

public File getFile(){
return sf;
}


}




Aucun commentaire:

Enregistrer un commentaire