Xtra 31 Share Posted July 3, 2020 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 Link to comment Share on other sites More sharing options...
Pandemic 2675 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. Link to comment Share on other sites More sharing options...
Xtra 31 Author Share Posted July 3, 2020 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? 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 Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.