Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
  • For loop variable help


    Xtra
     Share

    Recommended Posts

    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 by Xtra
    Link to comment
    Share on other sites

    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

    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?
     

    df5857bf57036c12a755355490c0060f.png

    Edited by Xtra
    Link to comment
    Share on other sites

    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 account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.