Ok i was able to figure out how to save the data in my jtable as text file but im not sure how i suppose to load it.
I've been trying to figure this out for few day now and i still lost on how to go about it.
this is the code I have for saving my jtable
when i save my code the output in the text file look like this:
First Name: dan
Last Name: ram
Phone Number: (342) 423-4324
Email: [email protected]
Code:savecontact.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser filesave = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT File", ".txt", "text"); filesave.setFileFilter(filter); int returnVal = filesave.showSaveDialog(Main.this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { File file = filesave.getSelectedFile(); PrintWriter os = new PrintWriter(file +".txt"); for (int row = 0; row < table.getRowCount(); row++) { for (int col = 0; col < table.getColumnCount(); col++) { os.print(table.getColumnName(col)); os.print(": "); os.println(table.getValueAt(row, col)); } } os.close(); System.out.println("Done!"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } });
this is my table
Code:table.setModel(new DefaultTableModel( new Object[][] { }, new String[] { "First Name", "Last Name", "Phone Number", "Email" } ));


Reply With Quote