Jump to content

Starting the script with a "start" button


Recommended Posts

Posted (edited)

I've been working on my first GUI but I have NO idea on how to start my script.

I'm a little mind-kcuf'd right now since I'm working with two classes, the GUI and the Main where everything happens.

I know I should be using a listener for my button "start" but then I have no idea what should be inside the action-listener body. Should I call the onLoop() method? If so, how?

I have searched a lot of GUI tutorials on this forums & google in general but cant seem to find anything. 

Would love to get any help possible.

 

Edited by Ragboys is back
Posted
private void openGUI(){
    GUI gui = new GUI(this);
    try{
        while(gui.isGuiActive()){ //1
            sleep(500);
        }
    }
    catch (InterruptedException e){
        log(e.toString());
    }
}
confirmButton.addActionListener(e -> {
        guiActive = false; //2
        frame.dispose();
    }
});

One implementation is under onStart() you could put some code like above. 

1. In your GUI class a boolean value (that is retrieved via gui.isGuiActive()) denotes whether the user is finished with the gui (pressed start). Until then, wait. 

2. you denote that a user is finished with the Gui by flipping said boolean value to false when your start button is pressed (in your listener for your start button). 

Basically you are delaying the conclusion of onStart() until the user is finished with the GUI. Then you proceed to onLoop() afterwards. 

Posted
11 minutes ago, PayPalMeRSGP said:

private void openGUI(){
    GUI gui = new GUI(this);
    try{
        while(gui.isGuiActive()){ //1
            sleep(500);
        }
    }
    catch (InterruptedException e){
        log(e.toString());
    }
}

confirmButton.addActionListener(e -> {
        guiActive = false; //2
        frame.dispose();
    }
});

One implementation is under onStart() you could put some code like above. 

1. In your GUI class a boolean value (that is retrieved via gui.isGuiActive()) denotes whether the user is finished with the gui (pressed start). Until then, wait. 

2. you denote that a user is finished with the Gui by flipping said boolean value to false when your start button is pressed (in your listener for your start button). 

Basically you are delaying the conclusion of onStart() until the user is finished with the GUI. Then you proceed to onLoop() afterwards. 

Small, but important addition:

Your button should also set the GUI to not be visible. This is done by frame.setVisible(false) in the context of the code that @PayPalMeRSGP wrote

Posted
5 hours ago, d0zza said:

Have a global boolean called started that you set to be true inside the action-listener body. Then have an if (started) check in your onLoop.

I recommend you not using this. If the script list is not refreshed, the static fields will keep their values. So as soon as someone stops the script and starts it again without refreshing the list, it will start instantly and probably crash.

Posted
35 minutes ago, Canidae said:

I recommend you not using this. If the script list is not refreshed, the static fields will keep their values. So as soon as someone stops the script and starts it again without refreshing the list, it will start instantly and probably crash.

I never said it should be static?

  • Like 1
Posted (edited)
8 hours ago, d0zza said:

I never said it should be static?

Java doesn't really have global variables. So as soon as someone refers to a "global variable" in Java, people will interpret it as a static variable because they share some of their properties.

Edited by Canidae
Posted (edited)
7 hours ago, Canidae said:

Java doesn't really have global variables. So as soon as someone refers to a "global variable" in Java, people will interpret it as a static variable because they share some of their properties.

Yes but scripting for osbot does have global variables.

Edited by d0zza
Posted (edited)
3 minutes ago, d0zza said:

You can have global variables in a script

What do you mean with global variables then? They don't exist in Java and you also said your meaning of global variables aren't (public) static variables. What is it then? The only thing you can do in Java is having public classes with a public static variable in it to make it look like it's some sort of global variable.

 

Edited by Canidae
Posted
3 minutes ago, Canidae said:

What do you mean with global variables then? They don't exist in Java and you also said your meaning of global variables aren't (public) static variables. What is it then? The only thing you can do in Java is having public classes with a public static variable in it to make it look like it's some sort of global variable.

 

Open up any script you've written, then go inside the script manifest but not inside any method, type int foo = 1; and there's your global variable.

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...