Reputation: 1028
I'm getting
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
when I'm deleting last Item in JComboBox
. Anyone knows why?
cb = new JComboBox<String>();
bComboDelete.addActionListener(this);
bComboDelete = new JButton("X"); //deletes item from CB
bComboAccept = new JButton("#");// add an item
ArrayList<String> names = new ArrayList<String>(); //get name, runs with sNumbers
ArrayList<String> sNumbers = new ArrayList<String>();//some String numbers ----> e.g. [[1, 2, 3],[4, 5, 6]]
ArrayList<Integer> numbers = new ArrayList<Integer>(); //array to temp hold numbers
//***ending code from ActionListener
else if(s == bComboAccept)
{
sNumbers.add(numbers.toString());
//System.out.println(sNumbers);
names.add(tName.getText());//tName is JTextField
//cb.addItem(tName.getText());
cb.addItem(new String(tName.getText()));
cb.setSelectedItem(new String(tName.getText()));
}
else if(s == bComboDelete)
{
int z = cb.getSelectedIndex();
//System.out.println(z);
names.remove(z);
//System.out.println("Names: "+names);
sNumbers.remove(z);
//System.out.println("sNumbers: "+sNumbers);
cb.removeItem(cb.getSelectedItem());
}
else if(s == cbLista)
{
System.out.println("cb Action listener!\n--------------");
Integer i = cb.getSelectedIndex();
tNames.setText(nazwy.get(i));
tNumbers.setText("");//also TextField
numbers.clear();
numbers=arrayStringToIntegerArrayList(sNumbers.get(i));
tNumbers.setText(numbers.toString().substring(1, numbers.toString().length()-1));
}
Rest of crash code:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.ArrayList.elementData(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Okno.actionPerformed(Okno.java:339) at javax.swing.JComboBox.fireActionEvent(Unknown Source) at javax.swing.JComboBox.contentsChanged(Unknown Source) at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source) at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source) at javax.swing.DefaultComboBoxModel.removeElementAt(Unknown Source) at javax.swing.DefaultComboBoxModel.removeElement(Unknown Source) at javax.swing.JComboBox.removeItem(Unknown Source) at Okno.actionPerformed(Okno.java:328) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Upvotes: 0
Views: 375
Reputation: 1028
It seems I fixed this. Now code is like this:
I'm too lazy to rename the var to eng. Please ignore System out.
so:
nazwy = names
oceny = numbers
sOceny = sNumbers
cbLista = cb
tNazwa = tNames
tOceny = tNumbers
//----
else if(s == bComboDelete)
{
Object o = cbLista.getSelectedItem();
int z = nazwy.indexOf(o);
if(z>-1)
{
sOceny.remove(z);
nazwy.remove(z);
cbLista.removeItemAt(z);
System.out.println("Oceny w bloku delete(if)"+oceny.toString());
}
System.out.println("Oceny w bloku delete(poza if)"+oceny.toString());
}
else if(s == cbLista)
{
Object o = cbLista.getSelectedItem();
int z = nazwy.indexOf(o);
if(z>-1)
{
tNazwa.setText(nazwy.get(z));
oceny.clear();
oceny=arrayStringToIntegerArrayList(sOceny.get(z));
tOceny.setText(oceny.toString().substring(1, oceny.toString().length()-1));
System.out.println("Oceny w bloku CB"+oceny.toString());
}
else
{
tNazwa.setText("");
tOceny.setText("");
oceny.clear();
}
}
Upvotes: 0
Reputation: 9041
Try adding the following modifications...
else if(s == bComboDelete)
{
int z = cb.getSelectedIndex();
// -1 means that no item is selected
if (z > -1) {
//System.out.println(z);
names.remove(z);
//System.out.println("Names: "+names);
sNumbers.remove(z);
//System.out.println("sNumbers: "+sNumbers);
cb.removeItem(cb.getSelectedItem());
}
}
else if(s == cbLista)
{
System.out.println("cb Action listener!\n--------------");
Integer i = cb.getSelectedIndex();
// -1 means that no item is selected
if (i > -1) {
tNames.setText(nazwy.get(i));
tNumbers.setText("");//also TextField
numbers.clear();
numbers=arrayStringToIntegerArrayList(sNumbers.get(i));
tNumbers.setText(numbers.toString().substring(1, numbers.toString().length()-1));
}
}
Upvotes: 0