package GUI; import javax.swing.*; import java.awt.*; import java.net.*; import java.io.*; import java.util.*; public class GuiMaintain extends javax.swing.JFrame { public DefaultListModel ListModel = new DefaultListModel(); public ArrayList editRecord = new ArrayList(); public GuiMaintain() { initComponents(); //maintainLoad(); jList1.setModel(ListModel); } @SuppressWarnings("unchecked") // private void initComponents() { btnEdit = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jList1 = new javax.swing.JList(); setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); setTitle("Maintain Documents"); btnEdit.setText("Edit"); btnEdit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnEditActionPerformed(evt); } }); jScrollPane1.setViewportView(jList1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap(719, Short.MAX_VALUE) .addComponent(btnEdit) .addContainerGap()) .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btnEdit) .addGap(6, 6, 6)) ); java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); setBounds((screenSize.width-796)/2, (screenSize.height-493)/2, 796, 493); }// private void btnEditActionPerformed(java.awt.event.ActionEvent evt) { if (jList1.getSelectedIndex() != -1) { Functionality.Documents documents = new Functionality.Documents(); documents.setIndex(jList1.getSelectedIndex()); GuiEdit editForm = new GuiEdit(); editForm.setVisible(true); String splittedRecord[] = editRecord.get(documents.getIndex()).toString().split("\\|"); editForm.txtTitle.setText(splittedRecord[0]); editForm.txtSummary.setText(splittedRecord[1]); editForm.txtPath.setText(splittedRecord[3]); editForm.txtRefNo.setText(splittedRecord[2]); } } public void maintainLoad() { try { Scanner getRecords; URL url = getClass().getResource("/Information/records.txt"); getRecords = new Scanner(new File(url.getPath())); for (int a = 0; getRecords.hasNextLine(); a++) { String line = getRecords.nextLine(); String splittedText[] = line.split("\\|"); if (splittedText.length == 4) { editRecord.add(a, line); ListModel.add(ListModel.size(), "Title: " + splittedText[0] + " - " + "Summary: " + splittedText[1] + " - " + "Ref no: " + splittedText[2] + " - " + "Path: " + splittedText[3]); } } getRecords.close(); } catch (IOException e) { System.out.println("Error"); } } public void editArrayUpdate(String title, String summary, String refNo, String path) { try { URL url = getClass().getResource("/Information/records.txt"); Functionality.Documents documents = new Functionality.Documents(); //re-load empty readUpdate arraylist with records and add the one that needed to be implemented. Scanner readUpdate; readUpdate = new Scanner(new File(url.getPath())); for (int i = 0; readUpdate.hasNextLine(); i++) { if(i == documents.getIndex()) { editRecord.add(documents.getIndex(), title + "|" + summary + "|" + refNo + "|" + path); } else { editRecord.add(readUpdate.nextLine()); } } readUpdate.close(); //update text file ..... Formatter writeNew = new Formatter(url.getPath()); for (int i = 0; i < editRecord.size(); i++) { writeNew.format(editRecord.get(i).toString() + "%n"); } writeNew.close(); //update list... Scanner Update; Update = new Scanner(new File(url.getPath())); for (int i = 0; Update.hasNextLine(); i++) { String line = Update.nextLine(); String splittedText[] = line.split("\\|"); ListModel.add(i, "Title: " + splittedText[0] + " - " + "Summary: " + splittedText[1] + " - " + "Ref no: " + splittedText[2] + " - " + "Path: " + splittedText[3]); } Update.close(); } catch (Exception e) { System.out.println(e.getMessage()); } } // Variables declaration - do not modify private javax.swing.JButton btnEdit; public javax.swing.JList jList1; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration }