Xtra 31 Share Posted July 3, 2020 (edited) So I'm trying to change out 'm' in this for loop for (int i = 0; i <= m; i++) { if (i % 2 == 0) { JLabel lblItem = new JLabel(); panel.add(lblItem); lblItem.setIcon(new ImageIcon(getPic(i))); lblItem.setHorizontalAlignment(SwingConstants.CENTER); lblItem.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent arg0) { ctx.log(itemCount + ""); } }); } } 'm' changes if I do m = 5 before the for loop (obviously), but not if I remove it and use 'm' from my comboBox actionListener. The log output is correct and gives out the the corresponding variable from the array String[] itemStrings = {"option", "another option", "third option"}; JComboBox shopComboBox = new JComboBox(itemStrings); int[] myNums = {8, 12, 16}; shopComboBox.addActionListener(l -> { if (shopComboBox.getSelectedIndex() >-1 && shopComboBox.getSelectedIndex() < myNums.length){ idx = shopComboBox.getSelectedIndex();option numberBasedOnOption = myNums[idx]; m = numberBasedOnOption; ctx.log("" + m); } }); Thanks Edited July 3, 2020 by Xtra Link to comment Share on other sites More sharing options...
Pandemic 2434 Share Posted July 3, 2020 How are you declaring 'm'? You could try making it volatile, as inside the action listener could (should?) be the Event Dispatch Thread instead of your script's thread, and then your script is reading from a cache line instead of memory. Xtra 1 Link to comment Share on other sites More sharing options...
Xtra 31 Author Share Posted July 3, 2020 (edited) 8 hours ago, Pandemic said: How are you declaring 'm'? You could try making it volatile, as inside the action listener could (should?) be the Event Dispatch Thread instead of your script's thread, and then your script is reading from a cache line instead of memory. I've tried private, public, static, final, volatile seems to produce the Jlabels in my gridlayout where the others do not How would I go abut making the items and gridlayout update on comboBox change? Edited July 3, 2020 by Xtra Link to comment Share on other sites More sharing options...
Xtra 31 Author Share Posted July 3, 2020 Fixed, put the for loop inside the comboBox actionEvent Thank you @Pandemic & @Succulent Link to comment Share on other sites More sharing options...
Succulent 18 Share Posted July 3, 2020 30 minutes ago, Xtra said: Fixed, put the for loop inside the comboBox actionEvent Thank you @Pandemic & @Succulent no problem pal Xtra 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now