Jump to content

Starting the script with a "start" button


Ragboys is back

Recommended Posts

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

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. 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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