У меня есть приложение Java с Jbutton для выбора локального файла, а затем представляю файл с различными типами диаграмм. Я хочу, чтобы получить файл, возвращаемый JFileChooser и обработать его (разделение, добавить значения в некоторых списках ...)Получить значение файла из jfilechooser
Это JButton ActionListener:
JButton theButton = new JButton("Choose the file to represent");
theButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
theFile = fileChooser.getSelectedFile();
ReadGCFile.getValue(theFile);
}
}
});
desktopPane.add(theButton);
theButton.setVisible(true);
theButton.setBounds(20, 100, 250, 20);
getContentPane().add(theButton);
Здесь я хочу обработать файл:
public static void readGCList(List<String> gcArrayList,
List<String> gcStringList, List<String> gcDateList)
throws NumberFormatException, IOException, ParseException {
File newFile = null;
String line = "";
String[] tokens = null;
FileReader fr = null;
BufferedReader bufReader = null;
try {
fr = new FileReader(GDRT.theFile);
bufReader = new BufferedReader(fr);
while ((line = bufReader.readLine()) != null) {
line = line.replace(",", ".");
tokens = line.split(";");
gcDateList.add(tokens[0]);
gcStringList.add(tokens[1]);
gcArrayList.add(tokens[2]);
gcArrayList.add(tokens[3]);
gcArrayList.add(tokens[4]);
}
} catch (FileNotFoundException es) {
System.out.println("The file was not found.");
} catch (NullPointerException e) {
System.out.println("No files were chosen !");
}
catch (IOException e) {
bufReader.close();
}
И я сделал метод GetValue, в котором я не знаю, что надеть:
public static void getValue(File myFile) {
System.out.println(myFile.getName());
}
объяснить «выскочило» - может поделиться код, в котором вы добавляете эту кнопку на свой пользовательский интерфейс? – Jan
Я подробно рассмотрел ниже. ty – DCalin
Я отредактировал его. Ты можешь помочь мне с этим ? – DCalin