Jump to content

How do I make a GUI for my script?


Recommended Posts

Posted

I am familiar with java, but I am brand new to scripting and the OSBot API. I looked in the API for some GUI, I could not find anything. So I tried to open up a JFrame using Java Swing on the script start method.. I am looking to just put some checkboxes and some dropdowns for my woodcutting script for Ex: Pick from yews or willows. Thanks!

Posted

Here I made it quickly for you

public class GUI extends JFrame {

	private JPanel contentPane;
	public JButton btnNewButton;
	public JComboBox<String> comboBox;
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 148, 108);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(ae -> setVisible(false));
		btnNewButton.setBounds(20, 42, 89, 23);
		contentPane.add(btnNewButton);
		
		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Willow", "Yew"}));
		comboBox.setBounds(10, 11, 121, 20);
		contentPane.add(comboBox);
	}
}

Also in your onStart

private int tree; //0 -> willow, 1 -> yew
	public void onStart() {
		GUI g = new GUI();
		g.setVisible(true);
		while(g.isVisible()) {}
		tree = g.comboBox.getSelectedIndex();
	}

And btw API has nothing to do with gui

Posted (edited)

Here I made it quickly for you

public class GUI extends JFrame {

	private JPanel contentPane;
	public JButton btnNewButton;
	public JComboBox<String> comboBox;
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 148, 108);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(ae -> setVisible(false));
		btnNewButton.setBounds(20, 42, 89, 23);
		contentPane.add(btnNewButton);
		
		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"Willow", "Yew"}));
		comboBox.setBounds(10, 11, 121, 20);
		contentPane.add(comboBox);
	}
}

Also in your onStart

private int tree; //0 -> willow, 1 -> yew
	public void onStart() {
		GUI g = new GUI();
		g.setVisible(true);
		while(g.isVisible()) {}
		tree = g.comboBox.getSelectedIndex();
	}

And btw API has nothing to do with gui

Don't forget to post your Swing related code to the Event Dispatch Thread! Ensures no inconcistencies with any GUI code that may be executing.

 

As for the busy-waiting, wait/notify (guarded blocks) would be the best solution for what you want to achieve. It ensures a thread isn't continuously running while not executing any code (consuming CPU power).

 

If you choose to busy-wait (maybe you find wait/notify too complex, which if so, message me), you should at least sleep for 1 or 2 millisec between each iteration, to ensure CPU usage isn't at max (You'd be surprised how much 2 ms helps). Hope you found this info beneficial! :) And thanks for the contribution!

Edited by fixthissite
  • Like 1
Posted

Create new class for the GUI (I prefer Eclipse Windowbuilder) and create new JFrame. Then in your onStart() method set the gui visible and while it is visible read the data. And ofcourse a button to hide the gui and start the script.

 

Look up a tutorial for eclipse window builder.

pfft

 

Netbeans UI builder > Eclipse UI builder

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