0

I have two classes: one called Student and the other one called Course. I would like to make a simulation for a simple registration system.

My Student class part has the following form:

class Student
{
    private String id,
    private Course[] listOfCourses;
    private int numCourse;
    //accesing methods
    public registration(Course course){
          listOfCourses[numCourse]=course;
          numCourse++;
    }
    public Course[] getCourse(){
          return listOfCourses;
    }
}

and the Course class has the following form:

class Course
{
     String id, String courseName;
     //constructor
     //accesing methods
}

I would like that by pressing a buttom in a form made in Java Swing, to display the contents of the courses registered by one specific student into a jTable. I have tried the following, but with no results at all:

Student e=new Student();
Course d[]=new Course[4];   
d=e.getCourses();    //to receive the array of Courses from the Student class
for (int i=0;i<d.length;i++){
      jTable2.setValueAt(estLista[i].getName(), i, 0);
}

how I can do that? I mean there is a way in which I could get the contents of the array, that is stored in the Course class, into the ActionEvent of the button?

4
  • what problem are you having exactly? Commented Apr 25, 2013 at 18:16
  • @MrD, the problem that I have is that I am not receiving the array of Courses in a way that I can put their contents in a Jtable Commented Apr 25, 2013 at 18:17
  • but do you want to have a complete UI for registration? or just a button for registration with hardcoded data inside your application? Commented Apr 25, 2013 at 18:22
  • the behavior of the button is just to print the courses that one student is taking Commented Apr 25, 2013 at 18:24

2 Answers 2

1

From the code you have provided I believe there atleast one reason why you are not getting the courses.. because it is not set in registration process:) (Also the syntax is not correct unless you have a registration class?) This might not be a complete solution but it corrects one of the problem

public void registration(Course course){
     // listOfCourses[numCourse];
        listOfCourses[numCourse]=course;
      numCourse++;
}
Sign up to request clarification or add additional context in comments.

1 Comment

sorry it was one typo that I made, anyway it still does not work
0

Ok, it is not too clear for me yet, but I will put some code and tell me if it helps you.

Note: Not tested

For Student (sorry I prefer to use lists instead of arrays):

public class Student {

    private String id;
    private List<Course> takenCourses;

    public void registration(Course course){
        if (this.takenCourses != null) {
            takenCourses.add(course);
        } else {
            System.err.println("an array has not been specified.");
        }
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public List<Course> getTakenCourses() {
        return takenCourses;
    }
    public void setTakenCourses(List<Course> takenCourses) {
        this.takenCourses = takenCourses;
    }

For course:

public class Course {
    private String id;
    private String name;

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

For your UI, I just created a "simulation" of UI, I assume you have implemented something more complete... I assume you have intialized the components as global variables for your frame or panel or at least you have methods to get them.

public class UIHelper extends JFrame {

    Student student = new Student();
    JButton btnAction;
    JTable myTable;
    public UIHelper() {
        //Methods for creating UI 
        //.
        //.
        //.
        student.setId("stackoverflowed");
        student.setTakenCourses(new ArrayList<Course>());

        btnAction = new JButton("Action!");
        //Methods of the JButton (...)
        btnAction.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //Now just process since student is a global variable (you can set it static as well) but it shouldn't be a good practice at all
                for (Course course : student.getTakenCourses()) {
                    System.out.println(course.getName());
                             //Add the element to your table.
                }
            }
        });
    }

    public static void main(String[] args) {
        //Assume this is your UI
        new UIHelper();
    }

Hope I give you an idea, best regards.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.