Hi guys,

I'd really appreciate it if someone could look over my code and tell me why my JComboBox isn't working. I've got 3 Classes, 2 for the GUI, one for the standalone which the array is contained in.

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;

public class BHSSDatabase
   public static void main (String[] args)
  //Create and set up the window.
  JFrame frame = new JFrame("The BHSS Database");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  //Add content to the window.
  frame.add(new BHSSDatabasePanel(), BorderLayout.CENTER);
  //Display the window.

Here's another class.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import java.awt.event.KeyEvent;
import java.lang.String;

public class BHSSDatabasePanel extends JPanel
  private JLabel mainDisplay, line1, studNameLabel, osisLabel, studentListLabel, teacherNameLabel, teacherListLabel, classNameLabel,
    classListLabel, studentOsisLabel;
  private JPanel tabbedPanel;
  private JComponent panel1, panel2, panel3, panel4; //<<<---- What exactly does this do?
  private JTextField studentNameInput, osisInput, teacherNameInput, classNameInput;
  private JComboBox studentNameList, studentNameList2, teacherNameList, classNameList;
  private JButton button1, button2, button3, button4, button5, button6, button7;
  public BHSSDatabasePanel()
    tabbedPanel = new JPanel();
    mainDisplay = new JLabel("Welcome to the BHSS Database");
//How do I get this to display only on the "Home" panel?
    line1 = new JLabel("Please choose the tab which best suits your needs");
// tabbedPanel.addTab("Home", panel1);
    //How do I get this to display in seperate panels?
    //tabbedPanel.addTab("Add/Remove A Person", panel2);
    studNameLabel = new JLabel("Input the student's name which you wish to add.");
    studentNameInput = new JTextField(20);
    osisLabel = new JLabel("Input the student's OSIS.");
    osisInput = new JTextField(9);
    button1 = new JButton("Add A Student!");
    //Create a JButton "Add Student!"
    studentListLabel = new JLabel("Select the student who you wish to delete.");
    String[] hgfjhg = StudentClass.students;
    studentNameList = new JComboBox(hgfjhg); //<--- //*************How do display the array. Code isn't working. Tried looking at API***********
    button2 = new JButton("Drop this student!");
    //JComboBox containing the array of student's name.***
    //JButton "Delete Student!"
    teacherNameLabel = new JLabel("Enter the teacher's name which you wish to add.");
    teacherNameInput = new JTextField(20);
    button3 = new JButton("Hire this teacher!");
    teacherListLabel = new JLabel("Select the teacher which you wish to remove.");
    teacherNameList = new JComboBox();
    button4 = new JButton("Fire This Teacher!");
    //JComboBox here containing teacher's names.
    //tabbedPanel.addTab("Add/Remove A Class", panel3);
    studentOsisLabel = new JLabel("Select the student who's program you wish to modify.");
    studentNameList2 = new JComboBox();
    button5 = new JButton("Modify this Student.");
    classNameLabel = new JLabel("Input the name of the class to add."); //To the specified student's OSIS
    classNameInput = new JTextField(10);
    button6 = new JButton("Add this class!");
    classListLabel = new JLabel("Select the class which you wish to remove.");
    classNameList = new JComboBox();
    button7 = new JButton("Drop this course!");
    //Panel 1 Components
    //Creates the tabbed panel
    //Should be in panel 2, HOW? (People Panel)
    //studentNameInput.addActionListener(this); <-- Creates error @ compilation.
    //How to use actionListener?
    //If explainattion is bad, revert to Car class to implement popup windows. Ugh.
    add(button1); //Add this student!
    add(button2); //Delete this student!
    add(button3); //Add this teacher!
    add(button4); //Delete this teacher!
    //Panel 3 (Add/Remove a Class)
    add(button5);//Modify this student!
    add(button6); //Add this class!
    add(button7); //Drop this class!
    //create the action listeners! nigga.
    //Panel 4 (Add/Calculate Grades)
    //Display the student's grades
    //Get Information on student, teacher Only.
    setPreferredSize(new Dimension(345, 550));
//Refer to farenheitPanel class for example.


And the final class, which holds the array.

import java.lang.String;

public class StudentClass
  private String name, address;
  private int osis, numOfCuts;
  public static String [] students;
  public StudentClass()
    name = "";
    address = "";
    osis = 0;
    numOfCuts = 0;

    students = new String[5];
    students[0] = "ASHDJKAHd";
    students[1] = "jon";
    students[2] = "josh";
    students[3] = "bill";
    students[4] = "lol";
    //students[5] = "hi";
  public void setName(String name1)
    name = name1;
  public String getName()
    return name;
  public void setAddress(String address1)
    address = address1;
  public String getAddress()
    return address;
  public void setOSIS(int osis1)
    osis = osis1;
  public int getOSIS()
    return osis;
  public void setNumOfCuts(int numOfCuts1)
    numOfCuts = numOfCuts1;
  public int getNumOfCuts()
    return numOfCuts;
  public static double paradigm(double exam1, double exam2, double exam3, double exam4, double finalExam, double hwCompleted, double hwTotal)
    double avg = 0.0;
    avg = (((exam1 + exam2 + exam3 + exam4)/(4)) * .7 + (finalExam * .15) + ((hwCompleted/hwTotal)*.15)*100);
    return avg;
  public static double paradigm(double exam1, double exam2, double exam3, double finalExam, double projectGrade, double projectTotalPoints, double hwCompleted, double hwTotal)
    double avg = 0.0;
    avg = (((exam1 + exam2 + exam3)/3)*.35 + (finalExam*.15) + ((projectGrade/projectTotalPoints)*.35) + ((hwCompleted/hwTotal)*.15));
    return avg;
  public void addStudent()


Another question which I have is, for this program I want to be able to add and delete students through the user input (in the GUI). I was thinking of making an array of StudentClass objects, but I'm not sure how I'd go about adding new students to the array. Possibly by resizing by creating a temp array?

And finally, after going over the ActionListener class in the API, I still wasn't able to figure out how to utilize it to perform the tasks which I need them to do (execute methods from other stand-alone classes). In my GUI, there are multiple buttons, and I'm unsure of how to go about that.

Hope you guys can clear some of these things up for me!

Thanks in advance,

String[] hgfjhg = StudentClass.students;
studentNameList = new JComboBox(hgfjhg);

The "students" array in the StudentClass class is declared:

public static String [] students;

It is null!. That is why it is not working. You are giving values to this array inside the constructor. Do you call the constructor?

The above should fix your problem, but the entire logic is wrong.
Keep the StudentClass the way it is and remove the static array.
For that you will need a StudentClass array:

StudentClass [] lotsOfStudents = new StudentClass[5];

But since I assume that you will need to add and remove students better use this:

Vector<StudentClass> listOfStudents = new Vector<StudentClass>();

The above should be declared either as a global variable in the BHSSDatabasePanel panel. Or create a separate class with that as private and with public methods to add and remove.

For the above to work you will need:
> To override the equals method in the StudentClass. (in order for some useful methods of the Vector class to work correct)
> To override the toString method in the StudentClass. Have it return whatever you want. When you will add a StudentClass instance object to the JComboBox, what the toString method of the StudentClass returns, is what will be displayed

Look at the API of the java.util.Vector class and JComboBox class.

The equals and toString methods are inherited by the Object class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.