Jump to content

GUI not functioning but not giving errors in IDE.


lpbrown

Recommended Posts

Hey guys. Finished a very solid smelting script and am just trying to get a GUI working. the script is great so far when I just hard code in the optons and then compile it, but id like the options to be selectable.

 

Here is my onstart:

 public void onStart() throws InterruptedException {
    	GUI mygui = new GUI();
    	mygui.setVisible(true);
    	while(mygui.isVisible()) {sleep(100);}
    	
    	barselection = mygui.comboBox.getSelectedIndex();
    	location = mygui.comboBox_1.getSelectedIndex();
    	
    }

and here is the gui im using. I made it with eclipse's JFrame builder

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;


public class GUI extends JFrame {

	private JPanel contentPane;
	public JComboBox comboBox;
	public JComboBox comboBox_1;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GUI frame = new GUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 446, 160);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JButton btnStartSmithing = new JButton("Start Smithing");
		btnStartSmithing.setBounds(5, 5, 424, 23);
		btnStartSmithing.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) { 
			setVisible(false);
			}
		});
		contentPane.setLayout(null);
		contentPane.add(btnStartSmithing);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Bronze", "Iron", "Silver", "Steel", "Gold", "Mithril", "Adamant", "Rune", "Cannonballs"}));
		comboBox.setBounds(5, 39, 200, 75);
		contentPane.add(comboBox);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Al Kharid", "Edgeville"}));
		comboBox_1.setBounds(215, 39, 209, 75);
		contentPane.add(comboBox_1);
		
		
		
	}

}

However, when I run the script the gui will pop up, but no information from the combobox's will be provided to the script. How can I fix this?

Link to comment
Share on other sites

Hey guys. Finished a very solid smelting script and am just trying to get a GUI working. the script is great so far when I just hard code in the optons and then compile it, but id like the options to be selectable.

Here is my onstart:

public void onStart() throws InterruptedException {    	GUI mygui = new GUI();    	mygui.setVisible(true);    	while(mygui.isVisible()) {sleep(100);}    	    	barselection = mygui.comboBox.getSelectedIndex();    	location = mygui.comboBox_1.getSelectedIndex();    	    }
and here is the gui im using. I made it with eclipse's JFrame builder
import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import javax.swing.JComboBox;import javax.swing.DefaultComboBoxModel;public class GUI extends JFrame {	private JPanel contentPane;	public JComboBox comboBox;	public JComboBox comboBox_1;	/**	 * Launch the application.	 */	public static void main(String[] args) {		EventQueue.invokeLater(new Runnable() {			public void run() {				try {					GUI frame = new GUI();					frame.setVisible(true);				} catch (Exception e) {					e.printStackTrace();				}			}		});	}	/**	 * Create the frame.	 */	public GUI() {		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		setBounds(100, 100, 446, 160);		contentPane = new JPanel();		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));		setContentPane(contentPane);				JButton btnStartSmithing = new JButton("Start Smithing");		btnStartSmithing.setBounds(5, 5, 424, 23);		btnStartSmithing.addActionListener(new ActionListener() {			public void actionPerformed(ActionEvent e) { 			setVisible(false);			}		});		contentPane.setLayout(null);		contentPane.add(btnStartSmithing);				JComboBox comboBox = new JComboBox();		comboBox.setModel(new DefaultComboBoxModel(new String[] {"Bronze", "Iron", "Silver", "Steel", "Gold", "Mithril", "Adamant", "Rune", "Cannonballs"}));		comboBox.setBounds(5, 39, 200, 75);		contentPane.add(comboBox);				JComboBox comboBox_1 = new JComboBox();		comboBox_1.setModel(new DefaultComboBoxModel(new String[] {"Al Kharid", "Edgeville"}));		comboBox_1.setBounds(215, 39, 209, 75);		contentPane.add(comboBox_1);							}}
However, when I run the script the gui will pop up, but no information from the combobox's will be provided to the script. How can I fix this?
That's strange.. I never used Eclipse GUI builder, but GUI builders usually take advantage of GroupLayout, not absolute positioning, for consistency reasons.

Could you take a picture of your results? I'm not sure what your problem is. Are the combo boxes not showing up in your frame? Or are the results from the check boxes aren't the ones you expected? Please be more specific

Link to comment
Share on other sites

That's strange.. I never used Eclipse GUI builder, but GUI builders usually take advantage of GroupLayout, not absolute positioning, for consistency reasons.

Could you take a picture of your results? I'm not sure what your problem is. Are the combo boxes not showing up in your frame? Or are the results from the check boxes aren't the ones you expected? Please be more specific

 

http://i.imgur.com/MGpDBwG.png gui initializes as expected and the combo boxes function when you drop them down etc.

 

 

upon hitting the start button, I get this error in the log of the osbot client (although it is not a fatal error):

 

the script then proceeds to loop through my failsafe and end as no values have been assigned to what bar we are smithing, or location we are smithing at.

[INFO][Bot #1][06/15 01:46:23 PM]: Error in script onStart(): Brown Smelter CBALL
[INFO][Bot #1][06/15 01:46:23 PM]: Started script : Brown Smelter CBALL
[INFO][Bot #1][06/15 01:46:23 PM]: 2 bank
[INFO][Bot #1][06/15 01:46:30 PM]: 2 bank
[INFO][Bot #1][06/15 01:46:31 PM]: 2 bank
[INFO][Bot #1][06/15 01:46:33 PM]: 2 bank
[INFO][Bot #1][06/15 01:46:34 PM]: 2 bank
[INFO][Bot #1][06/15 01:46:35 PM]: Terminating script Brown Smelter CBALL...
[INFO][Bot #1][06/15 01:46:35 PM]: Thanks for using brown smither
[INFO][Bot #1][06/15 01:46:35 PM]: Script Brown Smelter CBALL has exited!
Link to comment
Share on other sites

You're using combo.getSelectedIndex(). I believe this returns the index of the current state of the combo box.

 

Instead use combo.getSelectedItem(). You may need to cast it with (String)

 

Apa.

 

I wrote the script to take in the values as integers and make decisions on what to withdraw based on what integers had been stored. (0 = bronze, 1 = iron, etc etc)

 

I rewrote a little bit of it in order to allow for the usage of the getSelectedItem method but this yeilds the same results.

 

Additionally, I remade the GUI with only one combo box from scratch to see if I had maybe messed something up some where, because why not its not that hard. I am still getting the same errors.

Link to comment
Share on other sites

I wrote the script to take in the values as integers and make decisions on what to withdraw based on what integers had been stored. (0 = bronze, 1 = iron, etc etc)

I rewrote a little bit of it in order to allow for the usage of the getSelectedItem method but this yeilds the same results.

Additionally, I remade the GUI with only one combo box from scratch to see if I had maybe messed something up some where, because why not its not that hard. I am still getting the same errors.

So are the values returned both invalid values? That's what you mean by "not providing info to the script"?

Run this through your IDE's debugger. I'd give your JCheckBox an ActionListener (which triggers when the item is changed). Put a breakpoint within it, make sure that when you change combobox item, the thread suspends (allowing you to see what happened up to that point). Open up the Variables view and make sure they are the values you want.

I'm not at a computer, so not much I can do right now. What I CAN do is suggest:

1. Post your Swing code to the EDT

2. Don't use busy waiting

3. Check out my GUI Settungs guide (http://osbot.org/forum/topic/75074-creating-a-settings-ui-specify-settings-when-script-starts/)

4. DEBUG

  • Like 1
Link to comment
Share on other sites

So are the values returned both invalid values? That's what you mean by "not providing info to the script"?

Run this through your IDE's debugger. I'd give your JCheckBox an ActionListener (which triggers when the item is changed). Put a breakpoint within it, make sure that when you change combobox item, the thread suspends (allowing you to see what happened up to that point). Open up the Variables view and make sure they are the values you want.

I'm not at a computer, so not much I can do right now. What I CAN do is suggest:

1. Post your Swing code to the EDT

2. Don't use busy waiting

3. Check out my GUI Settungs guide (http://osbot.org/forum/topic/75074-creating-a-settings-ui-specify-settings-when-script-starts/)

4. DEBUG

 

Thank you for your help. Followed your guide slowly and got it working. You are the best. 

Link to comment
Share on other sites

You've defined 2 global variables in GUI:

	public JComboBox comboBox;
	public JComboBox comboBox_1;

These are un-initialized, thus null.

 

In the constructor, you create 2 new local variables with the same names, but you do not refer the global variables to them.

		JComboBox comboBox = new JComboBox();
		JComboBox comboBox_1 = new JComboBox();

As a result of this, you're getting NPE's thrown in onStart, as you're accessing the uninitialized global variables. onStart however is catching these exceptions for you, so they are never visible to you (other than the message "Error in onStart")

 

Solution:

In the constructor; initialize the global variables rather than creating new local ones.

Edited by FrostBug
  • Like 2
Link to comment
Share on other sites

You've defined 2 global variables in GUI:

	public JComboBox comboBox;
	public JComboBox comboBox_1;

These are un-initialized, thus null.

 

In the constructor, you create 2 new local variables with the same names, but you do not refer the global variables to them.

		JComboBox comboBox = new JComboBox();
		JComboBox comboBox_1 = new JComboBox();

As a result of this, you're getting NPE's thrown in onStart, as you're accessing the uninitialized global variables. onStart however is catching these exceptions for you, so they are never visible to you (other than the message "Error in onStart")

 

Solution:

In the constructor; initialize the global variables rather than creating new local ones.

 

True, onStart don't show errors, only a simple message you got an error in it smile.png

Remove JComboBox in front on the initialization should fix biggrin.png

If you don't understand why this happends look up some more about the "scoop" of variables.

 

Khaleesi

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