Jump to content

GUI Variables won't go over to main class?


Chambo

Recommended Posts

Hello again guys lol needing some help! My code is a mess now after i've followed a couple VERY different tutorials... So could you guys walk me through how to get my variables from gui class? Below are some of the variables i'm needing and also please walk me through this!! :) Thanks again in advance guys!

 

Variable : Type

 

String mule : Textbox
Boolean useMule : Checkbox
String moneyMethod : ComboBox
Boolean useAntiBan : Checkbox
String muleLoc : ComboBox
 
Oh by the way i'm using WindowBuilder for Eclipse! :) Not sure if that changes anything :P

 

Link to comment
Share on other sites

Note: I wrote this up in like 5 minutes, so don't take it word for word, but this is the general idea:

 

public class MyScript extends Script {

    private boolean eat = false;

    private final GUI gui = new GUI();

    

     @@Override

     public void onStart() throws InterruptedException {

         gui.createGUI();

 

        while(!gui.getStarted()) {

           sleep(random(500, 1000));

        }

        eat = gui.shouldEat();

    }

}

 

 

public class GUI {

   private boolean started = false;

   private boolean shouldEat = false;

 

   private final JFrame frame = new JFrame(); //you should probably attach a content pane to this, but i didnt because its only an example

 

   public void createGUI() {

 

      JButton startScript = new JButton();

      startScript.addActionListener(new ActionListener() {

            @@Override

            public void buttonPressed(ActionEvent e) { //i cant remember the name for this. could use the other format, but i think this shows it better

                  frame.setVisible(false);

                  frame.dispose();

                  started = true;

            }

     });

      frame.add(startScript);

 

      JButton eat = new JButton();

         //add specifics for eat

      frame.add(eat);

 

       frame.setVisible(true);

    }

 

   public boolean getStarted() {

       return started;

   }

   public boolean shouldEat() {

      return shouldEat;

  }

}

 

Edited by Imateamcape
Link to comment
Share on other sites

@@Imateamcape

 

So I have 2 separate classes. (main.java & gui.java)

 

gui.java

  Reveal hidden contents

 

And this is a part of my onStart() in main.java

  Reveal hidden contents

 

(NOTE: When this isn't commented out the script won't start currently it is commented out)

 

Any help for this?

Edited by Chambo
Link to comment
Share on other sites

I've often wondered what the 'best' way is to do this.
Certainly an easy way is to pass your 'main' object (from main.java) to the GUI class so it can communicate and perhaps say it's started or something.

 

So in your GUI class, create a constructor like:

private Main main;

public GUI(Main m){
    this.main = m;
}

and, so in your Main class, when you create the GUI object:
 

Gui gui = new Gui(this);

And also a setter method for started in Main

public void setStarted(boolean b){
    this.started = b;
}

Then in your button listener in your GUI class you can

main.setStarted(true);

That's a simple way of doing it. You could perhaps have a custom 'Environment' class which stores data like int antibanLikelihood; or boolean started; And you can pass this to the GUI and read off it when you want the information.

 

Hope you understood this. (also, capitalise your class names :p)

Edited by Auron
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...