Jump to content

How do I make a GUI for my script?


Prozen

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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

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