views:

353

answers:

2

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.

A: 

You should use a List or a StringBuilder for ease of getting the lines. And do you get any error(s) as result? Debugging would really help to see where your program is breaking.

Here is something I put together for you real quick:

public void importFile() {
    JFileChooser chooser = new JFileChooser();//A
    if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //a
        try {
            FileReader fr = new FileReader(chooser.getSelectedFile().getPath());
            BufferedReader file_in = new BufferedReader(fr);
            List lines = new List();
            String line = new String("");
            while ((line = file_in.readLine()) != null) {
                list.add(line);
                if (list.size() >= 3) {
                    String[] nameArray = ((String)list.get(0)).split(" ");
                    Contact c = new Contact (nameArray[1], nameArray[0], 
                            (String)list.get(1), (String)list.get(2), 
                            (String)list.get(3));
                    contactList.add(c);
                }
                System.out.println(list.get(list.size()-1)); // Debug
            }
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

I didn't compile it so may have some typos or of such...

Nadeem
A: 

It imports into a array list called "contactList" which you can see is on the 5th line from the bottom. So it doesn't go straight into the JTextFields but either way I can't get it to work.

Dale