lpbrown Posted June 15, 2015 Share Posted June 15, 2015 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? Quote Link to comment Share on other sites More sharing options...
fixthissite Posted June 15, 2015 Share Posted June 15, 2015 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 builderimport 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 Quote Link to comment Share on other sites More sharing options...
lpbrown Posted June 15, 2015 Author Share Posted June 15, 2015 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! Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 15, 2015 Share Posted June 15, 2015 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. 2 Quote Link to comment Share on other sites More sharing options...
lpbrown Posted June 15, 2015 Author Share Posted June 15, 2015 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. Quote Link to comment Share on other sites More sharing options...
fixthissite Posted June 15, 2015 Share Posted June 15, 2015 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 1 Quote Link to comment Share on other sites More sharing options...
lpbrown Posted June 16, 2015 Author Share Posted June 16, 2015 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. Quote Link to comment Share on other sites More sharing options...
Wealthy Posted June 16, 2015 Share Posted June 16, 2015 cant wait to see it released xd gl Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 16, 2015 Share Posted June 16, 2015 If you're still having problems, to make it work you can always send the values from the jButton action listener instead. That will work. 1 Quote Link to comment Share on other sites More sharing options...
lpbrown Posted June 16, 2015 Author Share Posted June 16, 2015 If you're still having problems, to make it work you can always send the values from the jButton action listener instead. That will work. Might try this in the future but as for now ive had enough headache with this GUI haha. 1 Quote Link to comment Share on other sites More sharing options...
FrostBug Posted June 16, 2015 Share Posted June 16, 2015 (edited) 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 June 16, 2015 by FrostBug 2 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted June 16, 2015 Share Posted June 16, 2015 (edited) 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 Remove JComboBox in front on the initialization should fix If you don't understand why this happends look up some more about the "scoop" of variables. Khaleesi Edited June 16, 2015 by Khaleesi Quote Link to comment Share on other sites More sharing options...
Apaec Posted June 16, 2015 Share Posted June 16, 2015 True, onStart don't show errors, only a simple message you got an error in it Khaleesi Speaking of this, it would be great if onStart did display errors instead of just 'error in onstart' has this been suggested to the devs? i'm sure it has... xd 1 Quote Link to comment Share on other sites More sharing options...