954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Removing items from combobox

This is probably very simple but i'm not seeing how to do it.
My GUI for a project reads objects from a flat file (substitution for database) and puts each object as an item in a jComboBox. Then if the user wants to delete one of the objects, he selects the object in the Combo Box and presses the delete button. Here is the code for the delete button action listener:

private void DeleteAssignmentButtonActionPerformed(java.awt.event.ActionEvent evt) {
        AssignmentParser parse = new AssignmentParser();
        Assignment[] existing = parse.getAllAssignments();
        int index = AssignmentToManage.getSelectedIndex();
        Assignment toDelete = existing[index];
        AssignmentToManage.setSelectedIndex(0);
        parse.deleteAssignment(toDelete.getID());
        existing = parse.getAllAssignments();
        for(int i = 0; i < existing.length; i++)
        {
            AssignmentToManage.removeItemAt(i);
            AssignmentToManage.insertItemAt(existing[i].getAssignmentName(), i);
        }
    }

However, when I try to delete, my combo box action listener throws an exception:
run:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at gist.mainUI.AssignmentToManageActionPerformed(mainUI.java:1573)
at gist.mainUI.access$1700(mainUI.java:28)
at gist.mainUI$22.actionPerformed(mainUI.java:742)
then goes up the family tree for combo box. The error is occurring in the action listener for the combo box:

private void AssignmentToManageActionPerformed(java.awt.event.ActionEvent evt)    {                                                   
        AssignmentParser parse = new AssignmentParser();
        Assignment[] existing = parse.getAllAssignments();
        int index = AssignmentToManage.getSelectedIndex();
        Assignment toManage = existing[index];//This line is the error
        int possible = toManage.getTotalPoints();
        AssignmentOutOfLabel.setText("Maximum Points: " + possible);
        
}

Also, it adds additional instances of the last object of the combobox to the bottom(ie if hw1 is the last value in the combobox it adds another hw1)

crenfrow
Newbie Poster
3 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Well, its hard to tell whats wrong with the code you have given, but the error means that index is larget than existing.length().

Try pringing out existing.length() and compare that to what you think it should be. If there is a difference then the problem is with parse.getAllAssignments();. However, is the selectedIndex actually 2? if thats not what you expect then chances are that AssignmentToManage.getSelectedIndex(); is your problem. Can't really tell without the AssignmentParser or AssignmentToManage code...

hanvyj
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 17
Solved Threads: 20
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: