Question

I am trying to add a JComboBox to my student program with the options of: Freshman, Sophomore, Junior, or Senior. When the Senior option is chosen, it should output something along the lines of, "Student has been in school for four years" or Junior, "Student has been in school for three years". etc...I'm searched the forums and done some Googling, but I can't a similar example to what I'm trying to do here. I will post the code below. Any help would be greatly appreciated. Thanks!

Student Class:

public class Student {
    protected String name;
    protected String address;
    protected double balance;
    protected String major;

    // Constructs fields
    public Student(String name, String address, String major, double balance) {
        this.name = name;
        this.address = address;
        this.major = major;
        this.balance = balance;
    }

    public String setName() {
        return name;
    }

    public String setAddress() {
        return address;
    }

    public double setBalance() {
        return balance;
    }

    public String setMajor() {
        return major;

    }

    public String setStudentInformation() {
        return "\n\tName: " + name + "\n\tAddress: " + address + "\n\tMajor: " + major + "\n\tBalance: " + balance;
    }

    public String toString() {
        return setStudentInformation();
    }
}

Manager Class:

public class Manager {
    private static Manager thisManager;

    public Student[] students = new Student[50];
    public int studentCounter = 0;

    public GradStudent[] gradStudents = new GradStudent[50];
    public int gradCounter = 0;

    // Private Constructor (Singleton instance method)
    public Manager() {

    }

    // Instance method (Singleton)
    public static Manager instance() {
        if (thisManager == null) {
            return thisManager = new Manager();
        } else {
            return thisManager;
        }
    }

    public Student createStudent(String name, String address, String major,
            double amount) {
        Student c1 = new Student(name, address, major, amount);
        storeStudent(c1);
        return c1;
    }

    private void storeStudent(Student c1) {
        students[studentCounter++] = c1;
    }

    public GradStudent createGradStudent(String name, String address,
            String major, double amount) {
        GradStudent e1 = new GradStudent(name, address, major, amount);
        storeGradStudent(e1);
        return e1;
    }

    private void storeGradStudent(GradStudent g1) {
        gradStudents[gradCounter++] = g1;
    }

    public String listStudents() {
        String temp = "\n\nStudent List\n";

        for (int i = 0; i < studentCounter; i++) {
            temp += "Student: " + students[i].setName() + "\n";
        }

        return temp;
    }

    public String listGrads() {
        String temp = "\n\nGraduate Student List\n";

        for (int i = 0; i < gradCounter; i++) {
            temp += "Graduate Student: " + gradStudents[i].setName() + "\n";
        }

        return temp;
    }

    public String aveBalance() {
        String temp = "\n\nStudent Average Balance: ";
        double sum = 0;

        for (int i = 0; i < studentCounter; i++) {
            sum += students[i].setBalance();
        }

        for (int i = 0; i < gradCounter; i++) {
            sum += gradStudents[i].setBalance();
        }

        return temp + sum / (studentCounter + gradCounter) + "\n";
    }

    public String listCsci() {
        String temp = "\n\nComputer Science Student List\n";

        for (int i = 0; i < studentCounter; i++) {
            if (students[i].setMajor().equals("Computer Science")) {
                temp += "Student: " + students[i].setName() + "\n";
            }
        }

        for (int i = 0; i < gradCounter; i++) {
            if (gradStudents[i].setMajor().equals("Computer Science")) {
                temp += "Graduate Student: " + gradStudents[i].setName() + "\n";
            }
        }

        return temp;
    }
}

GUI Class:

import javax.swing.*;

import java.awt.event.*;
import java.awt.*;
import java.util.Enumeration;

public class GUI extends JFrame implements ActionListener {
    // RadioButtons & ButtonGroup
    private JRadioButton jrbStudent = new JRadioButton("Student");
    private JRadioButton jrbGraduate = new JRadioButton("Graduate Student");
    private ButtonGroup group = new ButtonGroup();

    // JTextFields
    private JTextField name = new JTextField(20);
    private JTextField address = new JTextField(20);
    private JTextField balance = new JTextField(20);
    private JTextField major = new JTextField(20);

    // Submit Button
    private JButton jbtSubmit = new JButton("Submit");

    // echoStudent output area
    private JTextArea echoStudent = new JTextArea(5, 20);

    // Specific Buttons
    private JButton printStudentNames = new JButton("Print Student's Names");
    private JButton printGradStudentNames = new JButton(
            "Print Graduate Student's Names");
    private JButton calcBalance = new JButton(
            "Calculate Average Balance of All Students");
    private JButton compSciMajor = new JButton(
            "Displays Computer Science Major Students");

    // ScrollPane
    private JScrollPane scrollPane = new JScrollPane(echoStudent);

    // Fill and create JComboBox
    private String[] studentYear = { "Freshman", "Sophomore", "Junior", "Senior" };
    private JComboBox<String> jcbo = new JComboBox<String>(studentYear);



    public GUI() {
        super("College Student Information Interface");

        // Creates Panels
        JPanel bottomPanel = new JPanel();
        JPanel topLeftPanel = new JPanel();
        JPanel topRightPanel = new JPanel();

        // Adds Labels, Fields, and Buttons to the topRightPanel
        topRightPanel.setLayout(new GridLayout(7, 2, 5, 5));
        topRightPanel.add(new JLabel("Name:"));
        topRightPanel.add(name);
        topRightPanel.add(new JLabel("Address:"));
        topRightPanel.add(address);
        topRightPanel.add(new JLabel("Major:"));
        topRightPanel.add(major);
        topRightPanel.add(new JLabel("Balance:"));
        topRightPanel.add(balance);
        topRightPanel.add(new JLabel("Submit:"));
        topRightPanel.add(jbtSubmit);
        topRightPanel.add(printStudentNames);
        topRightPanel.add(printGradStudentNames);
        topRightPanel.add(calcBalance);
        topRightPanel.add(compSciMajor);

        // Handles the radioButton group and adds ActionListeners
        jrbStudent.setSelected(true);
        group.add(jrbStudent);
        group.add(jrbGraduate);
        jrbStudent.addActionListener(this);
        jrbGraduate.addActionListener(this);

        // topLeftPanel includes student type with student/gradStudent radio
        // buttons & freshman/sophomore/junior/senior in a combo box
        topLeftPanel.setBorder(BorderFactory
                .createTitledBorder("Which Type of Student Are You?"));
        topLeftPanel.add(jrbStudent);
        topLeftPanel.add(jrbGraduate);
        topLeftPanel.add(jcbo);

        // Adds topLeftPanel and topRightPanel to bottomPanel
        bottomPanel.add(topLeftPanel);
        bottomPanel.add(topRightPanel);

        // Places bottomPanel and scrollPane
        getContentPane().add(BorderLayout.NORTH, bottomPanel);
        getContentPane().add(BorderLayout.CENTER, scrollPane);

        // Adds ActionListeners to the buttons
        jbtSubmit.addActionListener(this);
        printStudentNames.addActionListener(this);
        printGradStudentNames.addActionListener(this);
        calcBalance.addActionListener(this);
        compSciMajor.addActionListener(this);
        echoStudent.setEditable(false);

        // Interface Settings
        setSize(1200, 700);
        setVisible(true);

    }

    // ActionEvent methods to handle interface events
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == jbtSubmit) {
            if (jrbStudent.isSelected()) {
                Student s1 = Manager.instance().createStudent(name.getText(),
                        address.getText(), major.getText(),
                        Double.parseDouble(balance.getText()));
                echoStudent.append("\n\nCreated Student: " + s1.toString());
            }

            else if (jrbGraduate.isSelected()) {
                GradStudent g1 = Manager.instance().createGradStudent(
                        name.getText(), address.getText(), major.getText(),
                        Double.parseDouble(balance.getText()));
                echoStudent.append("\n\nCreated Graduate Student: "
                        + g1.toString());

            }
        } else if (event.getSource() == printStudentNames) {
            echoStudent.append(Manager.instance().listStudents());
        } else if (event.getSource() == printGradStudentNames) {
            echoStudent.append(Manager.instance().listGrads());
        } else if (event.getSource() == calcBalance) {
            echoStudent.append(Manager.instance().aveBalance());
        } else if (event.getSource() == compSciMajor) {
            echoStudent.append(Manager.instance().listCsci());
        } else if (event.getSource() == jcbo) {

        }
    }

    public static void main(String[] args) {
        new GUI();

    }

}
Was it helpful?

Solution

You can acheive it like this. First of all adding actionListener to the JComboBox jcbo in the GUI() constructor.

jcbo.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
String selected=""+jcbo.getSelectedItem();
if(selected.equals("Senior"))
   System.out.println("Student has been in school for four years")
else if(selected.equals("Junior"))
   System.out.println("Student has been in school for three years");
    }
});

Like this you can do for all options.The code itself is self explanatory. Even if is it.Iam giving you a short explaination. When JComboBox item is select the above code will be executed.If the selected one is

  1. Senior :- Prints Student has been in school for four years.
  2. Junior :- Prints Student has been in school for three years.

I think it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top