Hi I'm having trouble using the Java JFileChooser
and was wondering if anyone could help me out. It's probably something really simple but I just cant spot whats wrong.
The JFileChooser
window opens fine when I click my import button and I can navigate to any field but I just cant read them into my JTextFields
.
Heres my JFileChooser
method:
public void importFile() {
JFileChooser chooser = new JFileChooser();//A
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //a
try {
BufferedReader file_in = new BufferedReader(
new FileReader(chooser.getSelectedFile().getPath()));
int i = 0;
String name = "",hnumber = "", mnumber = "", address = "";
while (((fileLines = file_in.readLine()) != null)) {
if (fileLines.length() > 0) {
i++;
if (i == 1) {
name = fileLines;
} else if (i == 2) {
hnumber = fileLines;
} else if (i == 3) {
mnumber = fileLines;
} else if (i == 4) {
address = fileLines;
String[] nameArray = name.split(" ");
Contact c = new Contact (nameArray[1], nameArray[0],
hnumber, mnumber, address);
contactList.add(c);
index = 0;
}
}
}
for (int j = 0; j < contactList.size(); j++) {
System.out.print(contactList.get(j).getname());
System.out.print(" ");
System.out.println(contactList.get(j).getmnumber());
System.out.println(contactList.get(j).gethnumber());
System.out.println(contactList.get(j).getaddress());
System.out.println(contactList.get(j).getsurname());
System.out.println(" ");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Any help would be great, thanks.