Question: Here are my code: (which about the student list + major) Please help me when user typing their names and major and then submit it.

Here are my code: (which about the student list + major) Please help me when user typing their names and major and then submit it. i want to save those name. When reopen it their names still there on GUI swing. To make sure the code is run through; please show me the result! Im using netbeans right now! Please help me to check all my errors and help me to resolve it! If I missed something on the topic please help me to create it more for me. I think they require 3 screens. The topic is underneath!

My code below is error.

package controller;

import model.StudentDAO; import view.MainGui;

public class MainController { public static void main(String[]args) { StudentDAO studentDAO = new StudentDAO(); new MainController(studentDAO); } private StudentController studentController; private MainGui mainGui; public MainController(StudentDAO studentDAO) { this.mainGui = new MainGui(new StudentController(studentDAO, new MainGui())); this.mainGui.setVisible(true); } }

package controller;

import model.Student; import model.StudentDAO; import view.MainGui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import view.NameInputGui;

public class StudentController implements ActionListener { private StudentDAO studentDAO; private MainGui mainGui; public StudentController(StudentDAO studentDAO, MainGui mainGui) { this.studentDAO = studentDAO; this.mainGui = mainGui; mainGui.setStudentController(this); updateView(); }

private void updateView() { List students = studentDAO.getStudentList(); mainGui.displayStudents(students); }

public void addStudent(Student student) { studentDAO.addStudent(student); updateView(); } public void updateStudent(Student oldStudent, Student newStudent) { studentDAO.updateStudent(oldStudent, newStudent); updateView(); } public void deleteStudent(Student student) { studentDAO.deleteStudent(student); updateView(); } public void openNameInputGui() { NameInputGui nameInputGui = new NameInputGui(mainGui, (ActionListener) this); nameInputGui.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == mainGui.getGetNameButton()) { openNameInputGui(); } }

} package model;

public class Student { private String name; private String major; public Student(String name, String major) { this.name = name; this.major = major; } public String getName() { return name; } public String getMajor() { return major; } @Override public String toString() { return "Name: " + name + ", Major: " + major; } } package model;

import java.util.ArrayList; import java.util.List;

public class StudentDAO { private List studentList = new ArrayList<>(); public void addStudent(Student student) { studentList.add(student); } public void updateStudent(Student oldStudent, Student newStudent) { studentList.remove(oldStudent); studentList.add(newStudent); } public void deleteStudent(Student student) { studentList.remove(student); } public List getStudentList() { return new ArrayList<>(studentList); } } package view;

import controller.StudentController; import model.Student; import javax.swing.*; import java.awt.*; import java.awt.event.ActionListener; import java.util.List;

public class MainGui extends javax.swing.JFrame { private JTextArea textArea; private JComboBox studentComboBox; private JButton getNameButton; private DefaultComboBoxModel studentComboBoxModel; private StudentController studentController; public MainGui(StudentController studentController) { this.studentController = studentController; initComponents(); } private void initComponents() { JPanel mainPanel = new JPanel(); textArea = new JTextArea(10,30); studentComboBoxModel = new DefaultComboBoxModel<>(); studentComboBox = new JComboBox<>(studentComboBoxModel); getNameButton = new JButton("Get Name"); // Create LayOut for input panel mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); mainPanel.add(new JLabel("Name:")); mainPanel.add(studentComboBox); mainPanel.add(getNameButton); mainPanel.add(new JScrollPane(textArea)); // Add main panel to the frame add(mainPanel); // Create the frame's properties setTitle("Student List"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); getNameButton.addActionListener(e -> studentController.openNameInputGui()); } public void addGetNameButtonListener(ActionListener listener) { getNameButton.addActionListener(listener); } public void setStudentController(StudentController studentController) { this.studentController = studentController; } public void addStudentToComboBox(String studentInfo) { studentComboBoxModel.addElement(studentInfo); } public void openNameInputGui() { NameInputGui nameInputGui = new NameInputGui(this, (ActionListener) studentController); nameInputGui.setVisible(true); } public void displayStudents(List students) { studentComboBoxModel.removeAllElements(); textArea.setText(""); for (Student student : students) { textArea.append(student.toString() + " "); addStudentToComboBox(student.toString()); } }

}

package view;

import controller.StudentController; import model.Student; import javax.swing.*; import java.awt.event.*;

public class NameInputGui extends javax.swing.JFrame { private JTextField nameField; private JTextField majorField; private JButton submitButton; private MainGui mainGui; private StudentController studentController; public NameInputGui(MainGui mainGui, ActionListener actionListener) { this.mainGui = mainGui; this.studentController = studentController; initComponents(); } private void initComponents() { JPanel inputPanel = new JPanel(); nameField = new JTextField(20); majorField = new JTextField(20); submitButton = new JButton("Submit"); inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS)); inputPanel.add(new JLabel("Name:")); inputPanel.add(nameField); inputPanel.add(new JLabel("Major:")); inputPanel.add(majorField); inputPanel.add(submitButton); // Add inputPanel to the frame add(inputPanel); // Create the frame's properties setTitle("Name Input"); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); pack(); setLocationRelativeTo(null); submitButton.addActionListener(e -> submitNameInput()); } private void submitNameInput() { String name = nameField.getText(); String major = majorField.getText(); Student newStudent = new Student(name, major); if(studentController != null) { studentController.addStudent(newStudent); } dispose(); } } [Created a GUI front-ended, persistent back-ended application.

Try to follow the three-tier design pattern - keep the code related to the back end in it's own classes/package (the model), the frames in their own package (the view), and the code to translate between them in it's own package (the Controller). Pass POJOs (Plain Old Java Object) to the View from the controller. Use the controller classes to pull data from the back end and convert it into Java objects. The model will be opened/closed/updated with methods from the Controller. In other words, the Controller classes should be your go-between to send data from the back-end to the front-end. For the Controller classes, follow the books DAO design pattern.

The design / subject of the program is up to you, but it needs to have at least three screens (one main screen that will spawn others) and provide CRUD functionality - the ability to CREATE new data to add to the back end, READ data from the back end, UPDATE existing values, and DELETE values - for at least two different types of objects.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!