Jump to content

Invoke GUI Checkbox in main class


Sebastian

Recommended Posts

Hello everyone!

 

So, i have just finished the layout of my GUI for my bot. 

My school taught me that i need to make 2 seperate classes. 1 main class and a GUI class. 

Now i have the gui class complete.

 

I have made 1 CheckBox in the GUI class. Now i want to invoke the CheckBox in my onLoop() in the main class.

 

How do i do this? i'm really struggled...

	public int onLoop() throws InterruptedException {
		switch (getState()) {
		case CHOP:
	
			Entity Willow = objects.closest("Willow");
			if (Willow != null && !myPlayer().isAnimating())  {
				Willow.interact("Chop down");
				log("Exiting case dropping......");
				log("Entering case chop");
				log("Chopping Willow logs");
			}
			else{
				sleep(random(500,700));
			}
			break;
		case DROP:
			inventory.dropAllExcept(1357, 1359);
			log("Case dropping.");
			break;
		case WAIT:
			sleep(random(500, 700));
			break;
		}
		return random(200, 300);
	}
		JCheckBox powerChop = new JCheckBox("Powerchop");
		powerChop.setBounds(0, 40, 110, 20);
        jPanel.add(powerChop);

Hope that you guys can help me!

 

Sincerely your's,

 

 

Sebastian.

Edited by Sebastian
Link to comment
Share on other sites


Assuming you built it correctly already in your GUI Class, this is how mine is.

 

Where your private longs are

​ JCheckBox checkbox = new JCheckBox("WorldHop", true); 

In onStart

  checkbox.setSelected(true);
        checkbox.setSelected(false);

In your onLoop


                                    getWorldHop(gui.getWorldHop());

Private void

  public void getWorldHop(JCheckBox event) throws InterruptedException {
        if (World_Hop) {
        
           //Code of what to do
        }
    }

In your start button area you have created

public void setStartScript(boolean startScript, boolean World_Hop) {
        this.startScript = startScript;
        this.World_Hop = World_Hop;
    
    }
Edited by Juggles
Link to comment
Share on other sites

@@Juggles 

 

This is my GUI class:

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class gui {
	
	
	public static void Window(main m) {
		JFrame jFrame = new JFrame(m.getAuthor() + " " + m.getName() + " v." + m.getVersion());

		// set size
		jFrame.setSize(300, 200);
		jFrame.setResizable(false);
		// Add panel
		JPanel jPanel = new JPanel(null);
		jFrame.add(jPanel);
		// add label
		JLabel jLabel = new JLabel("Select a tree");
		jLabel.setBounds(10, 10, 95, 20);
		jPanel.add(jLabel);

		// list
		JComboBox<String> list = new JComboBox<String>(new String[] { "Tree", "Oak", "Willow", "Yew" });
		// sets a string in the main class
		list.addActionListener(e -> m.tree = (String) list.getSelectedItem());
		list.setBounds(115, 10, 60, 20);
		// add list to jPanel
		jPanel.add(list);
		
		JCheckBox powerChop = new JCheckBox("Powerchop");
		powerChop.setBounds(0, 40, 110, 20);
        jPanel.add(powerChop);
		
		
		/**
		 * spinner SpinnerModel model = new SpinnerNumberModel(5, 5, 30, 1);
		 * JSpinner spinner = new JSpinner(model); spinner.addChangeListener(e
		 * -> m.CustomIntInTheMainClass = (int) spinner.getValue());
		 * spinner.setBounds(115, 30, 60, 20); // add spinner to jPanel
		 * jPanel.add(spinner);
		 */
		// button
		JButton startButton = new JButton("Start");
		startButton.addActionListener(e -> {
			synchronized (m.lock) {
				m.lock.notify();
			}
			jFrame.setVisible(false);
		});
		startButton.setBounds(0, 80, 50, 20);
		// add button to jPanel
		jPanel.add(startButton);

		jFrame.setVisible(true);
	}
}

and this is my main class. What i want is that i can use powerChop in my mainclass in my onloop. I somehow need to tell the main class that powerChop exists.

public int onLoop() throws InterruptedException {
		switch (getState()) {
		case CHOP:
			if(powerChop.isSelected()) {
				
			}
			else {
				
			}
			Entity Willow = objects.closest("Willow");
			if (Willow != null && !myPlayer().isAnimating())  {
				Willow.interact("Chop down");
				log("Exiting case dropping......");
				log("Entering case chop");
				log("Chopping Willow logs");
			}
			else{
				sleep(random(500,700));
			}
			break;
		case DROP:
			inventory.dropAllExcept(1357, 1359);
			log("Case dropping.");
			break;
		case WAIT:
			sleep(random(500, 700));
			break;
		}
		return random(200, 300);
	}

Like this if else statement. 

 

EDIT @Manner That just shows how to make a GUI in the main class. I don't want that because that is confusing. I like seperated classes because it's way better working for me ;d

Edited by Sebastian
Link to comment
Share on other sites

Hello everyone!

 

So, i have just finished the layout of my GUI for my bot. 

My school taught me that i need to make 2 seperate classes. 1 main class and a GUI class. 

Now i have the gui class complete.

 

I have made 1 CheckBox in the GUI class. Now i want to invoke the CheckBox in my onLoop() in the main class.

 

How do i do this? i'm really struggled...

 

Sebastian.

You have many options available here.

One way would be to pass a reference to your "CheckBox" to your GUI class so you can use it from there.

For example:

in your gui class

public GUI(Script s) {
  this.s=s;
}
private Script s;

in your main class where you create the GUI:

GUI g = new GUI(this);

then anywhere inside your gui class, you can work like you are inside the main class, like so:

s.yourCheckBox.doSomething();

Another option would to be to simply put the GUI class inside of the main class. Like so:

public class Main extends Script {
  CheckBox cb = ..
  private class GUI {
    void someMethod() {
      cb.doSomething();
    }
  }
}

Which way you use just depends on how you plan on modifying the script in the future and what is the most sustainable. 

Make sense?

For elite haxors only: http://docs.oracle.com/javase/tutorial/java/javaOO/index.html

Edited by colby__
  • Like 1
Link to comment
Share on other sites

Hi @@colby__ , i have tried everything you said in your post but i can't get it working.. Can you maybe help me via Teamviewer or something? 

 

I want it to be like this. 

public int onLoop() throws InterruptedException {
		switch (getState()) {
		case CHOP:
			Entity Willow = objects.closest("Willow");
			if (Willow != null && !myPlayer().isAnimating())  {
				Willow.interact("Chop down");
				log("Exiting case dropping......");
				log("Entering case chop");
				log("Chopping Willow logs");
			}
			else{
				sleep(random(500,700));
			}
			break;
		case DROP:
					if(powerChop.isSelected()) {
						inventory.dropAllExcept(1357, 1359);
						log("Case dropping.");
					}
					else {
						//bank
					}
			break;
		case WAIT:
			sleep(random(500, 700));
			break;
		}
		return random(200, 300);
	}

Damn this is getting really frustrating.. Hope ya can help me lol

Edited by Sebastian
Link to comment
Share on other sites

public interface Observer {
	public void update();
}

public interface Observable {
	public void registerObserver(Observer observer);

	public void sendUpdate();
}

@ScriptManifest(name = "LeScript", author = "Token", version = 2.0, info = "", logo = "")
public class Main extends Script implements Observer {

	UI ui;
	boolean checkBoxValue;
		
	@ Override
	public void update() {
		checkBoxValue = ui.getCheckBoxValue();
	}
		
	@ Override
	public void onStart() {
		ui = new UI();
	}

	@ Override
	public int onLoop() throws InterruptedException {
		return 69;
	}

}

@SuppressWarnings("serial")
public class UI extends JFrame implements Observable {

	ArrayList<Observer> observers = new ArrayList<Observer>();

	@ Override
	public void registerObserver(Observer observer) {
		observers.add(observer);
	}

	@ Override
	public void sendUpdate() {
		observers.forEach(o -> o.update());
	}
		
	JCheckBox checkBox;
	JPanel contentPane;
		
	public boolean getCheckBoxValue() {
		return checkBox.isSelected();
	}

	UI() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("LeTitle");
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setBounds(100, 100, 582, 401);
		contentPane.setLayout(null);
			
		checkBox = new JCheckBox("Checkbox");
		checkBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sendUpdate();
			}
		});
		checkBox.setBounds(181, 52, 97, 23);
		contentPane.add(checkBox);
			
			
		setVisible(true);
	}

}

That's an implementation of the Observer pattern which is used whenever it comes to programming user interfaces, also internally by the swing/awt/javafx libraries and pretty much any other library

You can test it like this

public static void main(String[] args) {
	UI ui = new UI();
	Observer obs = new Observer() {
		@ Override
		public void update() {
			System.out.println("updated");
		}
	};
	ui.registerObserver(obs);
}

PS: put each class in a separate module

Edited by Token
  • Like 1
Link to comment
Share on other sites

I fixed it. Thanks for all the replies. It really got me to the point where i am now.

 

This is how i fixed it:

 

First i made a public boolean in the main class:

public boolean powerChop = false;

Then in the GUI class i made an actionlistener:

powerChop.addActionListener(e -> m.powerChop = powerChop.isSelected());

Then i changed the onloop Drop case with if/else statement:


		case DROP:
			if(powerChop == true) {
				inventory.dropAllExcept(1357, 1359);
				log("Case dropping.");
			}
			break;

Now i can finally go on with banking options!

 

 

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