I am trying to use the JList to display a list of students. Now, each student has a first name, last name and course taken. each courses taken by the student has its' own name, level and idNumber. For now, I am just trying to create my own custom JList model for the students. I have the custom model as well as the button event calling it. Unfortunately, when I click the button to display the student info added, the component is NOT fired up!.. I am not sure what I have done wrong regarding creating the component because I can display the information on the console and the list contains everything I have added but just to display it on the component is NOT working. Any help will save me a boring week. I have broken the codes into sub classes, you can add the classes to the same package or create sub packages to insert individual classes.
The student class
public class Student { private String studentName; private String studentID ; public String getStudentName(){ return studentName; } public String getStudentId(){ return studentID; } public void setStudentName(String name){ this.studentName = name; } public void setStudentId(String id){ this.studentID = id; } public Student(String studentName, String studentId){ this.studentName = studentName; this.studentID = studentId; } public Student(){ } @Override public String toString() { return "Student [studentName=" + studentName + ", studentID=" + studentID + "]"; } }
The custom JList model class
import java.util.ArrayList; import java.util.List; import javax.swing.AbstractListModel; public class StudentList<E> extends AbstractListModel<E> { List<Student> studentList = new ArrayList<Student>(); @Override public int getSize() { return studentList.size(); } @Override public E getElementAt(int index) { return (E)studentList.get(index); } /** * adds a student to the JList panel * @param student to be added */ public void add(Student student) { System.out.println("adding"); studentList.add(student); int index0 = studentList.size() - 1; int index1 = index0; fireIntervalAdded(this, index0, index1); System.out.println(studentList.toString()); } }
The controller class
import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.ListSelectionModel; public class Controller { private static StudentList studentList; //the JList custom model private ViewGui view; //the view private Student student; //the model public Controller(ViewGui view, Student student){ this.student = student; this.view = view; view.setVisible(true); this.view.addStudentListener(new AddStudentListener()); } class AddStudentListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("click me!")) { view.setVisible(false); studentList = new StudentList(); // initialize the JList model Student studenta = new Student(); studenta.setStudentName("Lance"); studenta.setStudentId("01"); studentList.add(studenta); // add the student Student studentb = new Student(); studentb.setStudentName("John"); studentb.setStudentId("02"); studentList.add(studentb); // add the student JFrame c = new JFrame(); c.getContentPane(); c.setSize(300, 300); c.setLayout(new BorderLayout()); JPanel panel = new JPanel(); JList<Student> list = new JList<Student>(studentList); panel.add(list); list.setVisibleRowCount(3); list.setFont(new Font("Tahoma", Font.PLAIN, 14)); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBounds(0, 0, 100, 400); c.add(panel, BorderLayout.NORTH); c.setVisible(true); } } } }
The View class
import java.awt.BorderLayout; import java.awt.Container; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class ViewGui extends JFrame{ private static final long serialVersionUID = 1L; public JButton button = new JButton("click me!"); public ViewGui(){ setTitle("Main Window"); setSize(200,200); Container c = getContentPane(); c.setLayout(new BorderLayout()); c.add(button, BorderLayout.NORTH); } public void addStudentListener(ActionListener m){ button.addActionListener(m); } }
The main
public class SwingMainMethod { /** * @param args */ public static void main(String[] args) { Student student = new Student(); ViewGui view = new ViewGui() ; Controller c = new Controller(view,student); } }