Question

A partner and I are working on a program (yes, for homework) that is comprised of three GUI frames; the first is a simple menu with JButtons, the second is an enrollment screen that gives options (such as Passport Type, Passport Number, etc) for a user to fill out with a Jbutton to "save" the info to a .txt file, and the third is to "load" said .txt file by printing its contents within a text area in the GUI frame.

Our problem is that we can't seem to get the info to save and load from a .txt file.

We're pretty sure the file isn't being created, thus, nothing is being written to it. However we're not getting any errors thrown when it runs so we're confused at where we went wrong. If someone could take a look at it and give us some pointers, that would help immensely.

Code is below:

public class test 
{

 public static void main(String[] args) throws IOException
 {
         // File file = new File("enroll.txt"); used to create the file
         final JFrame mainMenu = new JFrame("Error");
            final int WINDOW_WIDTH = 600;
            final int WINDOW_HEIGHT = 400;
            FlowLayout layout = new FlowLayout();
            mainMenu.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            mainMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainMenu.setLocationRelativeTo(null);
            mainMenu.setLayout(layout);
            Button load = new Button("Load Enrollment");
            Button enroll = new Button ("New Enrollment");
            mainMenu.add(load);
            mainMenu.add(enroll);
            mainMenu.setVisible(true);

         final JFrame loadEn = new JFrame("Load");
            loadEn.setVisible(false);
            loadEn.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            loadEn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            loadEn.setLocationRelativeTo(null);
            loadEn.setLayout(layout);
            JLabel file = new JLabel("File:");
            file.setVisible(true);
            final JTextField fileEnter = new JTextField(10);
            fileEnter.setEditable(true);
            fileEnter.setVisible(true);
            Button run = new Button("Run");
            Button menu = new Button("Main Menu");
            final JTextArea print = new JTextArea(500, 300);
            loadEn.add(file);
            loadEn.add(fileEnter);
            loadEn.add(run);
            loadEn.add(menu);
            loadEn.add(print);

         final JFrame newEnroll = new JFrame("New Enrollment");
            newEnroll.setVisible(false);
            newEnroll.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
            newEnroll.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            newEnroll.setLocationRelativeTo(null);
            newEnroll.setLayout(layout);
            JLabel passType = new JLabel("Passport Type");
            JLabel passNo = new JLabel("Passport Number");
            JLabel firstName = new JLabel("First Name");
            JLabel lastName = new JLabel("Last Name");
            JLabel country = new JLabel("Country");
            JLabel placeOB = new JLabel("Place of Birth");
            JLabel visaType = new JLabel("Visa Type");
            JLabel visaNo = new JLabel("Visa Number");
            JLabel remarks = new JLabel("Remarks");
            Button save = new Button("Save");
            final JTextField _passNo = new JTextField(10);
            final JTextField _firstName = new JTextField(10);
            final JTextField _lastName = new JTextField(10);
            final JTextField _placeOB = new JTextField(10);
            final JTextField _visaNo = new JTextField(10);
            final JTextField _remarks = new JTextField(10);
            final JTextField _passType = new JTextField(10);
            final JTextField _country = new JTextField(10);
            final JTextField _visaType = new JTextField(10);
            newEnroll.add(passType);
            newEnroll.add(_passType);
            newEnroll.add(visaType);
            newEnroll.add(_visaType);
            newEnroll.add(passNo);
            newEnroll.add(_passNo);
            newEnroll.add(visaNo);
            newEnroll.add(_visaNo);
            newEnroll.add(firstName);
            newEnroll.add(_firstName);
            newEnroll.add(remarks);
            newEnroll.add(_remarks);
            newEnroll.add(lastName);
            newEnroll.add(_lastName);
            newEnroll.add(country);
            newEnroll.add(_country);
            newEnroll.add(placeOB);
            newEnroll.add(_placeOB);
            newEnroll.add(menu);
            newEnroll.add(save);

         load.addActionListener(new ActionListener () 
         {
             @Override
             public void actionPerformed(ActionEvent e) 
             {
                 loadEn.setVisible(true);
                 mainMenu.setVisible(false);
                 newEnroll.setVisible(false);
             } });

         menu.addActionListener(new ActionListener () 
         {
             @Override
             public void actionPerformed(ActionEvent e1) 
             {
                 loadEn.setVisible(false);
                 mainMenu.setVisible(true);
                 newEnroll.setVisible(false);
             } });

         enroll.addActionListener(new ActionListener () 
         {
             @Override
             public void actionPerformed(ActionEvent e2) 
             {
                 loadEn.setVisible(false);
                 mainMenu.setVisible(false);
                 newEnroll.setVisible(true);
             } });

         save.addActionListener(new ActionListener()
         {
             @Override
             public void actionPerformed(ActionEvent e4)
             {
                 PrintWriter fw = null ;
                 try 
                 {
                     fw = new PrintWriter("enroll.txt", "UTF-8");
                     BufferedWriter bw = new BufferedWriter(fw);
                     bw.write(_passNo.getText());
                     bw.newLine();
                     bw.write(_firstName.getText());
                     bw.newLine();
                     bw.write(_lastName.getText());
                     bw.newLine();
                     bw.write(_placeOB.getText());
                     bw.newLine();
                     bw.write(_visaNo.getText());
                     bw.newLine();
                     bw.write(_remarks.getText());
                     bw.newLine();
                     bw.write(_passType.getText());
                     bw.newLine();
                     bw.write(_country.getText());
                     bw.newLine();
                     bw.write(_visaType.getText());
                 } catch (IOException e1) 
                   {
                        e1.printStackTrace();
                   }
                   finally
                   {
                       fw.close();
                   }
             }
         });

         run.addActionListener(new ActionListener() 
         {
             public void actionPerformed(ActionEvent e3)
             {
                 FileReader fr = null;
                 try 
                 {
                     fr = new FileReader("enroll.txt");
                 } catch (FileNotFoundException e2) 
                   {
                     e2.printStackTrace();
                   }
                 try 
                 {
                     fr = new FileReader("enroll.txt");
                 } catch (FileNotFoundException e1) 
                   {
                     e1.printStackTrace();
                   }

                 BufferedReader br = new BufferedReader(fr);
                 try 
                 {
                     print.read(br, null);
                 } catch (IOException e1) 
                   {
                     e1.printStackTrace();
                   }
                   catch(Exception e)
                   {
                       System.out.println(e);
                   } finally
                     {
                       try 
                       {
                           br.close();
                       } catch (IOException e) 
                         {
                           e.printStackTrace();
                         }
                       try 
                       {
                           fr.close();
                       } catch (IOException e) 
                         {
                           e.printStackTrace();
                         }
                     }

             }
         });
 }
  }
Was it helpful?

Solution

Make two changes

First - writing

In save.addActionListener you close your Printwriter with fw.close();. The Printerwriter is used by your BufferedWriter bw = new BufferedWriter(fw);. You have to call bw.close() instead of fw.close()

Second - reading

The choice of your Layout is not the best. Your JTextArea is much to large. Your file is loaded but you can't see it. If you use final JTextArea print = new JTextArea(30, 30); instead of final JTextArea print = new JTextArea(500, 500); everythings works fine. Remember: The parameters in JTextArea are rows and columns - not pixel.

Hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top