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
  • Gui Class not passing variables


    crazykid080

    Recommended Posts

    Not sure what I'm doing wrong, I am passing  a reference to my main class to set variables but it doesn't seem to be working.
    Snippets:
     

    SwingUtilities.invokeLater(() -> new MainGui(this));
    ------------
    doneButton.addActionListener(e -> {
                reference.setItemList(itemList);
                reference.isRunning = true;
            });
    -------
    public int onLoop() {
            if (isRunning) {
              //Do stuff
            }

    I tested this by changing isRunning to a debug method and other debug logging and it seems to be doing nothing.

    Link to comment
    Share on other sites

    There seems to be nothing wrong with the code you posted, but you're not showing us it all. I assume your code looks somewhat like the following

    public class Main {
    
        public boolean isRunning;
    
        public void onStart() {
            SwingUtilities.invokeLater(() -> {
                new MainGui(this);
            });
        }
    
        public int onLoop() {
            if (isRunning) {
    
            }
            return 0;
        }
    
    }
    
    public class MainGui {
    
        public MainGui(Main reference) {
            JFrame frame = new JFrame();
            JButton doneButton = new JButton();
            doneButton.addActionListener(l -> {
                reference.isRunning = true;
            });
            frame.getContentPane().add(doneButton);
            frame.setVisible(true);
        }
    
    }
    Link to comment
    Share on other sites

    Yeah let me expand what I'm doing a bit more because I went in depth more. 
     

    public void onStart() {
            //unrelated setup
            SwingUtilities.invokeLater(() -> new MainGui(this));
            //more unrelated setup
        }

    After that I have the MainGui class (built by IntelliJ gui builder
     

        public MainGui(Starter reference) {
            this.reference = reference;
            
            doneButton.addActionListener(e -> {
                //Checks for what to add
                this.done(itemList);
            });
          //frame setup  
          JFrame frame = new JFrame("MainGui");
            frame.setContentPane(this.mainPanel);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void done(List<String> x) {
            this.reference.setItemList(x);
            this.reference.isRunning = true;
        }

    After that it goes into onLoop and checks for the isRunning boolean. If I'm missing anything relevant please let me know

    Link to comment
    Share on other sites

    This looks pretty similar to how I'm going about it so I'm not sure why it's not working.

    Throw a log("onLoop") in your onloop or something to make sure that's even being called. I'm guessing Starter is your main class and contains onLoop?

     

    Also, just a wee note, get rid of this:

    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    Since if someone closes your GUI it'll close out DB completely.

    Link to comment
    Share on other sites

    41 minutes ago, holic said:

    This looks pretty similar to how I'm going about it so I'm not sure why it's not working.

    Throw a log("onLoop") in your onloop or something to make sure that's even being called. I'm guessing Starter is your main class and contains onLoop?

     

    Also, just a wee note, get rid of this:

    
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    Since if someone closes your GUI it'll close out DB completely.

    Yeah I did a log for onLoop with the isRunning variable and it's definitely looping, isRunning just isn't being set to true, and as for the default operation, EXIT_ON_CLOSE would be the one that exits out.

    Source: My own mistake using EXIT_ON_CLOSE that was fixed after realizing 

    Link to comment
    Share on other sites

    26 minutes ago, crazykid080 said:

    Yeah I did a log for onLoop with the isRunning variable and it's definitely looping, isRunning just isn't being set to true, and as for the default operation, EXIT_ON_CLOSE would be the one that exits out.

    Source: My own mistake using EXIT_ON_CLOSE that was fixed after realizing 

    Oh whoops you're correct. Ignore me. ;)

    Not sure what the issue is with your code though, might be helpful to post more of it?

    Link to comment
    Share on other sites

    Yeah I think I'll have to. 
    So this is part of my main class

    public boolean isRunning;
    @Override
        public void onStart() {
            worlds = Worlds.all(world -> Worlds.getNormalizedWorlds().contains(world) && Worlds.noMinimumLevel().contains(world) && world.isF2P());
            localPlayer = getLocalPlayer();
            index = 0;
            utils = new BotUtils(localPlayer);
            SwingUtilities.invokeLater(() -> new MainGui(this));
        }
    
        @Override
        public int onLoop() {
            log(this.isRunning);
            if (this.isRunning) {
              //code that doesn't get to run
            }

    This is the code I wrote for the gui in MainGui

    List<String> staffList;
        private Starter reference;
    
    public MainGui(Starter reference) {
            this.reference = reference;
    
            doneButton.addActionListener(e -> {
                if (staffCB.isSelected()) staffList.add("Staff");
                if (magicStaffCB.isSelected()) staffList.add("Magic staff");
                if (airStaffCB.isSelected()) staffList.add("Staff of air");
                if (waterStaffCB.isSelected()) staffList.add("Staff of water");
                if (earthStaffCB.isSelected()) staffList.add("Staff of earth");
                if (fireStaffCB.isSelected()) staffList.add("Staff of fire");
                this.done(staffList);
            });
            JFrame frame = new JFrame("MainGui");
            frame.setContentPane(this.mainPanel);
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    private void done(List<String> x) {
            this.reference.setStaffList(x);
            this.reference.isRunning = true;
        }

    (It honestly makes me feel validated knowing that this is something stumping more than just me and isn't just something I overlooked)

    Link to comment
    Share on other sites

    28 minutes ago, holic said:

    Is this working? I still don't see any issues. 😕

    I will check, but considering isRunning isn't being set I doubt it. Result: Yep. Also null

     

    28 minutes ago, Pixel_Life said:

    Are you returning a value in the onLoop?

    I should be but I will check this as well. Result: Yes I am

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • 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.