Two JComboBoxes made from a class create stackoverflow

Hello,

This is my first post so please let me know if I should change te qustion.

use for URcap:
I am working on making a URcap that allows for easier installation and ease of programing for opening and closing a gripper.

problem:
In the installation menu the user should select if one IO should be used or two.
after that one or two JComboboxes apear and the user selects which IO should be used (see picture below)

To get it to work I made a IOcomboBox class witch can be used to create a JComboBox and set some value’s like what te label text should be.

in this two objects from that class are created and added to te view port.

when one of the two changes the selected IO a itemlistener is called that calls a function in the installationContribution that checkes from witch object the call came from and updates the other like so:

//inside the IOcomboBOx class
public Box createIOComboBox(final HelloWorldInstallationNodeContribution installationNodeContribution, String labelName ) {
		this.installationNodeContribution = installationNodeContribution;
		
		Box box = Box.createHorizontalBox();
		box.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		
		JLabel label = new JLabel(labelName);
		
		ioComboBox.setPreferredSize(new Dimension(104, 30));
		ioComboBox.setMaximumSize(new Dimension(250, 30));
		
		ioComboBox.addItemListener(new ItemListener() {
			
			@Override
			public void itemStateChanged(ItemEvent e) {//
				if(e.getID() == ItemEvent.ITEM_STATE_CHANGED && e.getStateChange() == ItemEvent.SELECTED) {						
					
					selectedIO =selectedItemIndex();
					//System.out.println("hoi");
					installationNodeContribution.updateCheckBox(ID, selectedIO);	//here the contribution gets called				
				}				
			}
		});
		
		box.add(label);
		box.add(ioComboBox);
		
		return box;
	}
//the updateCheckBox() function inside the contribution
	void updateCheckBox(int ID, int selectedIndex) {
		if(ID==1 && DEFAULT_OUTPUT1 !=selectedIndex) {//checkbox1 so update checkbox 2
			model.set(OUTPUT_KEY1, selectedIndex);
			System.out.println("removing: "+ selectedIndex + " from box2");
			view.updateComboBox2(selectedIndex);		
		}
		
		if(ID==2 && DEFAULT_OUTPUT2 !=selectedIndex) {//checkbox2 so update checkbox 1
			model.set(OUTPUT_KEY2, selectedIndex);
			System.out.println("removing: "+ selectedIndex + " from box1");

			view.updateComboBox1(selectedIndex);		
		}
	}

This works for one JComboBox but when two are used and after both changed the selected IO ones it generates an stack overflow error.

log:

found at index: 1 //index starts at 0 so its the second IO
removing: 1 from box2
box has: 0 selected
cleared list              //this ist he debug prints after comboBox one is updated
found at index: 8         //the next prints loop until the error after also selecting an IO in the second comboBox
removing: 8 from box1
box has: 1 selected
cleared list
found at index: 1
removing: 1 from box2
box has: 7 selected
cleared list
found at index: 8
removing: 8 from box1
box has: 1 selected
cleared list
found at index: 1
removing: 1 from box2
box has: 7 selected
cleared list 
//goes on and on 
//error
01:45:53.057 ERROR - Unhandled exception in URCap 'Dialogscript'
                     java.lang.StackOverflowError
                       @HelloWorldInstallationNodeContribution.updateCheckBox():37
                       @IOComboBox$1.itemStateChanged():75
                       @JComboBox.fireItemStateChanged():1223
                       @JComboBox.selectedItemChanged():1280
                       @JComboBox.contentsChanged():1330
                       @AbstractListModel.fireContentsChanged():118
                       @DefaultComboBoxModel.setSelectedItem():93
                       @JComboBox.setSelectedItem():576
                       @JComboBox.setSelectedIndex():622
                       @IOComboBox.removeItemAtIndex():49 {thread: AWT-EventQueue-0 , loggerClass: com.ur.urcap.osgi.ConsoleLogger}
01:45:53.098 ERROR - RobotState messages are queued up: 62 {thread: RobotState - PostMan , loggerClass: com.ur.monitor.RobotState$PostMan}

picture of the aftermath:

so as you can see the comboBoxes update correctly appart from the part that it gets stuck in a loop

for any that can take the time to help thanks in advance.

Remo

view.updateComboBox2(selectedIndex); might be triggering itemStateChanged which then calls updateCheckBox which calls view.updateComboBox2(selectedIndex); might be triggering itemStateChanged which then…yeah it might be an infinite loop

1 Like

it indeed was and i feel like an idot by not fixing it correctly. (its al good now)