Untitled
public
Dec 10, 2024
Never
16
1 package filehandlingoop; 2 3 import javax.swing.*; 4 import javax.swing.table.DefaultTableModel; 5 import java.awt.*; 6 import java.awt.event.ActionEvent; 7 import java.awt.event.ActionListener; 8 import java.io.*; 9 import java.util.Scanner; 10 11 public class FileHandlingOOP { 12 13 static JTextField filename; 14 static JTextField nameField; 15 static JTextField idNumberField; 16 static JComboBox<String> courseComboBox; 17 static JRadioButton male; 18 static JRadioButton female; 19 static JSpinner yearLevel; 20 static DefaultTableModel deftable; 21 static ButtonGroup genderGroup; 22 static JTextField statusField; 23 static JTable table; 24 static JComboBox<String> data; 25 static JTextField searchField; 26 static JButton saveUpdate; 27 28 public static void main(String[] args) { 29 JFrame frame = new JFrame(); 30 frame.setLayout(new BorderLayout()); 31 32 frame.setTitle("File handling"); 33 frame.setSize(900, 600); 34 frame.setResizable(false); 35 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 36 37 // Top Panel 38 JLabel titleLabel = new JLabel("FILE HANDLING", JLabel.CENTER); 39 titleLabel.setFont(new Font("Times New Roman", Font.BOLD, 25)); 40 41 JPanel panelTop = new JPanel(); 42 panelTop.setPreferredSize(new Dimension(900, 70)); 43 panelTop.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 44 panelTop.add(titleLabel, BorderLayout.CENTER); 45 46 // Left Panel 47 filename = new JTextField(); 48 filename.setPreferredSize(new Dimension(130, 40)); 49 50 JButton save = new JButton("SAVE"); 51 save.addActionListener(new SaveButtonListener()); 52 save.setPreferredSize(new Dimension(90, 30)); 53 54 JButton read = new JButton("READ"); 55 read.addActionListener(new ReadButtonListener()); 56 read.setPreferredSize(new Dimension(90, 30)); 57 58 JButton update = new JButton("UPDATE"); 59 update.setPreferredSize(new Dimension(90, 30)); 60 update.addActionListener(new UpdateButtonListener()); 61 62 saveUpdate = new JButton("Save Updated File"); 63 saveUpdate.setPreferredSize(new Dimension(90, 30)); 64 65 data = new JComboBox<>(); 66 data.setPreferredSize(new Dimension(90, 30)); 67 68 JPanel panelLeft = new JPanel(); 69 panelLeft.setPreferredSize(new Dimension(150, 460)); 70 panelLeft.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 71 panelLeft.add(new JLabel("Write a Filename")); 72 panelLeft.add(filename); 73 panelLeft.add(save); 74 panelLeft.add(read); 75 panelLeft.add(update); 76 panelLeft.add(saveUpdate); 77 78 // Center Panel 79 JPanel panelCenter = new JPanel(); 80 panelCenter.setLayout(null); 81 panelCenter.setPreferredSize(new Dimension(350, 460)); 82 panelCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 83 84 JLabel manageStudentLabel = new JLabel("MANAGE STUDENT"); 85 manageStudentLabel.setBounds(80, 20, 150, 20); 86 panelCenter.add(manageStudentLabel); 87 88 JLabel nameLabel = new JLabel("Name"); 89 nameLabel.setBounds(10, 60, 100, 20); 90 panelCenter.add(nameLabel); 91 92 nameField = new JTextField(); 93 nameField.setBounds(100, 60, 150, 20); 94 panelCenter.add(nameField); 95 96 JLabel lrnLabel = new JLabel("ID Number"); 97 lrnLabel.setBounds(10, 110, 100, 20); 98 panelCenter.add(lrnLabel); 99 100 idNumberField = new JTextField(); 101 idNumberField.setBounds(100, 110, 150, 20); 102 panelCenter.add(idNumberField); 103 104 JLabel courseLabel = new JLabel("Course"); 105 courseLabel.setBounds(10, 160, 100, 20); 106 panelCenter.add(courseLabel); 107 108 String[] courses = {"BSIT", "BITM", "BSCE", "BSMATH"}; 109 courseComboBox = new JComboBox<>(courses); 110 courseComboBox.setBounds(100, 160, 150, 20); 111 panelCenter.add(courseComboBox); 112 113 JLabel genderLabel = new JLabel("Gender"); 114 genderLabel.setBounds(10, 210, 100, 20); 115 panelCenter.add(genderLabel); 116 117 male = new JRadioButton("M"); 118 male.setBounds(100, 210, 40, 20); 119 female = new JRadioButton("F"); 120 female.setBounds(150, 210, 40, 20); 121 122 genderGroup = new ButtonGroup(); 123 genderGroup.add(male); 124 genderGroup.add(female); 125 126 panelCenter.add(male); 127 panelCenter.add(female); 128 129 JLabel yearLevelLabel = new JLabel("Year Level"); 130 yearLevelLabel.setBounds(10, 260, 100, 20); 131 panelCenter.add(yearLevelLabel); 132 133 String[] yearLevels = {"1st Year", "2nd Year", "3rd Year", "4th Year"}; 134 yearLevel = new JSpinner(new SpinnerListModel(yearLevels)); 135 yearLevel.setBounds(100, 260, 150, 25); 136 panelCenter.add(yearLevel); 137 138 JLabel statusLabel = new JLabel("Status"); 139 statusLabel.setBounds(10, 320, 100, 20); 140 panelCenter.add(statusLabel); 141 142 statusField = new JTextField(); 143 statusField.setBounds(100, 320, 150, 20); 144 panelCenter.add(statusField); 145 146 //============================== 147 JLabel searchLabel = new JLabel("Search:"); 148 searchLabel.setBounds(10, 20, 50, 20); 149 150 searchField = new JTextField(); 151 searchField.setBounds(70, 20, 175, 20); 152 153 JButton searchButton = new JButton("Search"); 154 searchButton.setBounds(250, 20, 75, 20); 155 156 // Right Panel 157 String[] columnNames = {"Name", "ID Number", "Course", "Gender", "Year", "Status"}; 158 deftable = new DefaultTableModel(columnNames, 0); 159 table = new JTable(deftable); 160 JScrollPane tableScrollPane = new JScrollPane(table); 161 tableScrollPane.setBounds(10, 85, 330, 260); 162 163 JPanel panelRight = new JPanel(); 164 panelRight.setLayout(null); 165 panelRight.setPreferredSize(new Dimension(400, 460)); 166 panelRight.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 167 panelRight.add(tableScrollPane); 168 panelRight.add(searchLabel); 169 panelRight.add(searchField); 170 panelRight.add(searchButton); 171 172 // Bottom Panel 173 JPanel panelBottom = new JPanel(); 174 panelBottom.setPreferredSize(new Dimension(900, 70)); 175 panelBottom.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 176 177 // Add panels to frame 178 frame.add(panelTop, BorderLayout.NORTH); 179 frame.add(panelLeft, BorderLayout.WEST); 180 frame.add(panelCenter, BorderLayout.CENTER); 181 frame.add(panelRight, BorderLayout.EAST); 182 frame.add(panelBottom, BorderLayout.SOUTH); 183 184 frame.setVisible(true); 185 } 186 187 public static class SaveButtonListener implements ActionListener { 188 189 @Override 190 public void actionPerformed(ActionEvent e) { 191 try { 192 String file = "studentData.csv"; 193 File myFile = new File(file); 194 195 BufferedWriter writer = new BufferedWriter(new FileWriter(myFile, true)); 196 197 writer.write(nameField.getText() + ","); 198 writer.write(idNumberField.getText() + ","); 199 writer.write((String) courseComboBox.getSelectedItem() + ","); 200 writer.write((male.isSelected() ? "Male" : (female.isSelected() ? "Female" : ""))); 201 writer.write(","); 202 writer.write((String) yearLevel.getValue()); 203 writer.write(","); 204 writer.write(statusField.getText()); 205 writer.write("\n"); 206 writer.close(); 207 208 JOptionPane.showMessageDialog(null, "File Created/Updated: " + file, "Success", JOptionPane.INFORMATION_MESSAGE); 209 210 nameField.setText(""); 211 idNumberField.setText(""); 212 genderGroup.clearSelection(); 213 courseComboBox.setSelectedIndex(0); 214 yearLevel.setValue("1st Year"); 215 statusField.setText(""); 216 217 } catch (IOException ex) { 218 JOptionPane.showMessageDialog(null, "An error occurred", "Error", JOptionPane.ERROR_MESSAGE); 219 ex.printStackTrace(); 220 } 221 } 222 } 223 224 public static class ReadButtonListener implements ActionListener { 225 226 @Override 227 public void actionPerformed(ActionEvent e) { 228 229 try { 230 String file = "studentData.csv"; 231 232 File myFile = new File(file); 233 BufferedReader reader = new BufferedReader(new FileReader(myFile)); 234 Scanner myReader = new Scanner(reader); 235 while (myReader.hasNextLine()) { 236 String data = myReader.nextLine(); 237 String[] row = data.split(","); 238 deftable.addRow(row); 239 } 240 myReader.close(); 241 } catch (IOException ex) { 242 JOptionPane.showMessageDialog(null, "File Not Found", "Error", JOptionPane.ERROR_MESSAGE); 243 } 244 } 245 } 246 247 public static class UpdateButtonListener implements ActionListener { 248 249 @Override 250 public void actionPerformed(ActionEvent e) { 251 String id = idNumberField.getText(); 252 253 // Find the row corresponding to the ID number and update it 254 for (int i = 0; i < deftable.getRowCount(); i++) { 255 String currentId = (String) deftable.getValueAt(i, 1); // ID is at index 1 256 if (currentId.equals(id)) { 257 deftable.setValueAt(nameField.getText(), i, 0); // Name is at index 0 258 deftable.setValueAt(courseComboBox.getSelectedItem(), i, 2); // Course at index 2 259 deftable.setValueAt(male.isSelected() ? "Male" : "Female", i, 3); // Gender at index 3 260 deftable.setValueAt(yearLevel.getValue(), i, 4); // Year Level at index 4 261 deftable.setValueAt(statusField.getText(), i, 5); // Status at index 5 262 break; 263 } 264 } 265 } 266 } 267 }