0

I am having some serious trouble.

My program that I have to write is a GUI that essentially asks for:

  • A Course (ex: cpsc130)
  • The name (ex: computer programming 2)
  • The amount of credits you receive for the class (ex 3)
  • And your grade (ex A, B).

I'm not very good with ActionListeners, escentially I have no idea what I'm doing with it. I have to make an ArrayList for the information

  • One ArrayList<Course> (instance variable) e.g. courseList, to store the courses you add;
  • Four inner ActionListener classes, with each of them implements its method of actionPerformed. Specically,
  • AddCourseListener: read the inputs and create a course object, add the course object into the courseList, and append this into the output area;
  • CalGPAListener: read all course credits and grades from the courseList, add them up and compute the overall GPA. You are assuming that A is 4, B is 3, C is 2, D is 1, and E is 0. GPA = Sum(creditpoint)=totalCredits. For instance (Figure 1), you have taken 3 courses: 130 (3 credits, grade of A), 131 ( 3 credits, grade of B), and 370 (4 credits, grade of B), then your GPA = (3*4+3*3+4*3)/(3+3+4) = 3.3.
  • ResetInputListener: reset all input elds;
  • ResetOutputListener: reset output area.

Those are all the ActionListeners I need. I will show you my code so far. When compiled it will show all the Buttons and TextAreas, the ActionListeners are the only thing I need help with.

import javax.swing.*;
import java.awt.event.*;
import java.util.*;


public class DegreeWorksFrame extends JFrame  {
    private JLabel courseCodeLabel;
    private JTextField courseCodeField;

    private JLabel courseNameLabel;
    private JTextField courseNameField;

    private JLabel courseCreditLabel;
    private JTextField courseCreditField;

    private JLabel courseGradeLabel;
    private JTextField courseGradeField;

    private JTextArea resultArea;

    private double sum =0;    
    private double totalCredits=0;

    private String code = "";
    private String name = "";
    private String credit = "";
    private String grade = "";

    private String heading = ("Code\tName\tCredit\tGrade" + "\n");
    private ArrayList<Course> courseList;

    private JButton AddCourse;
    private JButton CalculateGPA;
    private JButton ResetInput;
    private JButton ResetOutput;

    private static final int AREA_ROWS = 15;
    private static final int AREA_COLUMNS = 35;

    private final int FRAME_HEIGHT =400;
    private final int FRAME_WIDTH = 500;

    final int FIELD_WIDTH = 30;

    public DegreeWorksFrame() {
        resultArea = new JTextArea(AREA_ROWS, AREA_COLUMNS);
        resultArea.setText(heading);
        resultArea.setEditable(false);

        createTextField1();
        createTextField2();
        createTextField3();
        createTextField4();

        createButtonAddCourse();
        createButtonCalculateGPA();
        createButtonResetInput();
        createButtonResetOutput();

        createPanel();

        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    }

    private void createTextField1() {
        courseCodeLabel = new JLabel("Enter Course Code: ");
        courseCodeField = new JTextField(FIELD_WIDTH);
        courseCodeField.setText(code);
    }

    private void createTextField2() {
        courseNameLabel = new JLabel("Enter Course Name: ");

        courseNameField = new JTextField(FIELD_WIDTH);
        courseNameField.setText(name);
    }

    private void createTextField3() {
        courseCreditLabel = new JLabel("Enter Course Credit: ");

        courseCreditField = new JTextField(FIELD_WIDTH);
        courseCreditField.setText(credit);
    }

    private void createTextField4() {
        courseGradeLabel = new JLabel("Enter Course Grade: ");

        courseGradeField = new JTextField(FIELD_WIDTH);
        courseGradeField.setText(grade);
    }


    private void createButtonAddCourse() {
        AddCourse = new JButton ("Add Courses: ");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }

    class addCourseListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            courseList = new ArrayList();
            String receiveList = userList.get
        }
    }

    private void createButtonCalculateGPA() {
        CalculateGPA = new JButton ("Calculate GPA");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }

    class calculateGPAListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

        }
    }

    private void createButtonResetInput() {
        ResetInput = new JButton ("Reset Input");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }


    class resetInputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

        }
    }

    private void createButtonResetOutput() {
        ResetOutput = new JButton ("Reset Output");
        ActionListener listener = new addCourseListener();
        AddCourse.addActionListener(listener);
    }

    class resetOutputListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {

        }
    }

    private void createPanel() {
        JPanel panel = new JPanel();
        panel.add(courseCodeLabel);
        panel.add(courseCodeField);
        panel.add(courseNameLabel);
        panel.add(courseNameField);
        panel.add(courseCreditLabel);
        panel.add(courseCreditField);
        panel.add(courseGradeLabel);
        panel.add(courseGradeField);

        //buttons
        panel.add(AddCourse);
        panel.add(CalculateGPA);
        panel.add(ResetInput);
        panel.add(ResetOutput);

        //Scroll bar
        JScrollPane scrollPane = new JScrollPane(resultArea);
        panel.add(scrollPane);
        add(panel);
    }
}
dic19
  • 17,821
  • 6
  • 40
  • 69
  • What is your actual issue/question? – LadyBernkastel Apr 10 '14 at 10:43
  • I cannot figure out how to get the information to display in the resultArea – TheShadyHobo Apr 10 '14 at 10:46
  • Nor do i really understand how to implement the actionlistener. – TheShadyHobo Apr 10 '14 at 10:46
  • I can't see where the resultArea is given any of the information to show, other than the heading? Could that be why? ActionListeners are usually used on user clicks/interaction, best thing to do is [look online](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html). – LadyBernkastel Apr 10 '14 at 10:53
  • I saw something about appending or ammending something? I'm trying to add them to an array list and print them out in the result area though and i have no idea how to do that. I have another class "Course" which has getter and setters for code, name, credit, and grade, and im not really sure how to access them either. – TheShadyHobo Apr 10 '14 at 11:02

1 Answers1

2

Side-note: Note you're adding multiple listeners to your AddCourse button which is not correct. You need to add the listeners to the proper buttons.

As stated in this answer a good approach to implement listeners is by using Anonymous Inner classes (see the linked answer for a better explanation about why).

For instance:

private void createButtonAddCourse() {
    AddCourse = new JButton ("Add Courses: ");
    AddCourse.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Create a new Course object and add it to the ArrayList here
        }
    });
}

...

private void createButtonCalculateGPA() {
    CalculateGPA = new JButton ("Calculate GPA");
    CalculateGPA.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Iterate over the ArrayList and calculate the overall GPA for each course
        }
    });
}

Suggested readings

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Ah that was from copying and pasting the code for the Buttons. So how do i create a new object? and then would i do courseList.add(code)? and so on for the array list? furthermore would i append it to the resultarea? – TheShadyHobo Apr 10 '14 at 11:17
  • *So how do i create a new object?* This is pretty much basic in POO and your instructor must have explained you how to do this. *and then would i do courseList.add(code)? and so on for the array list?* Correct. *furthermore would i append it to the resultarea?* Yes, if you need to. @TheShadyHobo – dic19 Apr 10 '14 at 15:43