Chambo Posted October 7, 2016 Share Posted October 7, 2016 Hello again guys lol needing some help! My code is a mess now after i've followed a couple VERY different tutorials... So could you guys walk me through how to get my variables from gui class? Below are some of the variables i'm needing and also please walk me through this!! Thanks again in advance guys! Variable : Type String mule : Textbox Boolean useMule : Checkbox String moneyMethod : ComboBox Boolean useAntiBan : Checkbox String muleLoc : ComboBox Oh by the way i'm using WindowBuilder for Eclipse! Not sure if that changes anything :P Quote Link to comment Share on other sites More sharing options...
Team Cape Posted October 7, 2016 Share Posted October 7, 2016 (edited) Note: I wrote this up in like 5 minutes, so don't take it word for word, but this is the general idea: public class MyScript extends Script { private boolean eat = false; private final GUI gui = new GUI(); @@Override public void onStart() throws InterruptedException { gui.createGUI(); while(!gui.getStarted()) { sleep(random(500, 1000)); } eat = gui.shouldEat(); } } public class GUI { private boolean started = false; private boolean shouldEat = false; private final JFrame frame = new JFrame(); //you should probably attach a content pane to this, but i didnt because its only an example public void createGUI() { JButton startScript = new JButton(); startScript.addActionListener(new ActionListener() { @@Override public void buttonPressed(ActionEvent e) { //i cant remember the name for this. could use the other format, but i think this shows it better frame.setVisible(false); frame.dispose(); started = true; } }); frame.add(startScript); JButton eat = new JButton(); //add specifics for eat frame.add(eat); frame.setVisible(true); } public boolean getStarted() { return started; } public boolean shouldEat() { return shouldEat; } } Edited October 7, 2016 by Imateamcape Quote Link to comment Share on other sites More sharing options...
Chambo Posted October 7, 2016 Author Share Posted October 7, 2016 (edited) @@Imateamcape So I have 2 separate classes. (main.java & gui.java) gui.java import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JTextField; import javax.swing.JTabbedPane; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JCheckBox; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JTextPane; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class gui extends JFrame { /** * */ private static final long serialVersionUID = -8181573131827704618L; /** * */ public main main; public JPanel contentPane; public JTextField xmuleName; public boolean useAntiBan; public String moneyMethod; public String muleName; public boolean useMule; public String muleLoc; /** * 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. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public gui() { setTitle("Chambo's Elite Clay Miner"); setBounds(100, 100, 289, 358); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane.setBounds(0, 0, 273, 319); contentPane.add(tabbedPane); JPanel settings = new JPanel(); tabbedPane.addTab("Settings", null, settings, null); settings.setLayout(null); JLabel lblMuleName = new JLabel("Mule Name:"); lblMuleName.setBounds(10, 11, 65, 14); settings.add(lblMuleName); xmuleName = new JTextField(); xmuleName.setBounds(81, 8, 177, 20); settings.add(xmuleName); xmuleName.setColumns(10); JLabel lblUseMule = new JLabel("Use Mule?:"); lblUseMule.setBounds(10, 46, 65, 14); settings.add(lblUseMule); JCheckBox xuseMule = new JCheckBox("Check this box to use a Mule"); xuseMule.setSelected(true); xuseMule.setBounds(81, 42, 177, 23); settings.add(xuseMule); JLabel lblMethod = new JLabel("Method:"); lblMethod.setBounds(10, 78, 65, 14); settings.add(lblMethod); JComboBox xmoneyMethod = new JComboBox(); xmoneyMethod.setModel(new DefaultComboBoxModel(new String[] {"Mine Clay", "Spin Flax", "Collect/Tan Cowhides"})); xmoneyMethod.setBounds(81, 75, 177, 20); settings.add(xmoneyMethod); JLabel lblAntiban = new JLabel("Anti-Ban?:"); lblAntiban.setBounds(10, 116, 65, 14); settings.add(lblAntiban); JCheckBox xuseAntiBan = new JCheckBox("Check this box to use Anti-Ban"); xuseAntiBan.setBounds(81, 112, 177, 23); settings.add(xuseAntiBan); JLabel lblMuleLocation = new JLabel("Mule Location:"); lblMuleLocation.setBounds(10, 151, 76, 14); settings.add(lblMuleLocation); JComboBox xmuleLoc = new JComboBox(); xmuleLoc.setModel(new DefaultComboBoxModel(new String[] {"Varrock East", "Varrock West", "Grand Exchange", "Lumbridge"})); xmuleLoc.setBounds(91, 148, 167, 20); settings.add(xmuleLoc); JButton startButton = new JButton("Start"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { muleName = xmuleName.getText(); useMule = xuseMule.isSelected(); moneyMethod = xmoneyMethod.getSelectedItem().toString(); useAntiBan = xuseAntiBan.isSelected(); muleLoc = xmuleLoc.getSelectedItem().toString(); setVisible(false); main.started = true; } }); startButton.setBounds(65, 257, 137, 23); settings.add(startButton); JPanel information = new JPanel(); tabbedPane.addTab("Information", null, information, null); information.setLayout(null); JTextPane txtpnThanksForChoosing = new JTextPane(); txtpnThanksForChoosing.setText("Thanks for choosing my script! I've put a lot of time & effort into this so please enjoy it and if there are any bugs/problems please let me know!\r\n\r\nMule Name: Put you Mule's username here so that the slave will trade the mule when he logs in!\r\n\r\nUse Mule: Keep this checked if you want to use the Mule System! If not it's fine! It will just bank all the items!\r\n\r\nMethod: Select which Money Making Method you would like to use!\r\n\r\nAntiban: Select this to enable Anti-ban and help your account throw off Jagex a little!\r\n\r\nMule Location: This is the location that your Slave will walk to, to trade your Mule."); txtpnThanksForChoosing.setBounds(10, 11, 248, 269); information.add(txtpnThanksForChoosing); } } And this is a part of my onStart() in main.java public void onStart() { //Other variables are here gui g = new gui(); g.setVisible(true); while(g.isVisible()){} mule = g.muleName; useMule = g.useMule; moneyMethod = g.moneyMethod; useAntiBan = g.useAntiBan; muleLoc = g.muleLoc; } (NOTE: When this isn't commented out the script won't start currently it is commented out) Any help for this? Edited October 7, 2016 by Chambo Quote Link to comment Share on other sites More sharing options...
Auron Posted October 8, 2016 Share Posted October 8, 2016 (edited) I've often wondered what the 'best' way is to do this.Certainly an easy way is to pass your 'main' object (from main.java) to the GUI class so it can communicate and perhaps say it's started or something. So in your GUI class, create a constructor like: private Main main; public GUI(Main m){ this.main = m; } and, so in your Main class, when you create the GUI object: Gui gui = new Gui(this); And also a setter method for started in Main public void setStarted(boolean b){ this.started = b; } Then in your button listener in your GUI class you can main.setStarted(true); That's a simple way of doing it. You could perhaps have a custom 'Environment' class which stores data like int antibanLikelihood; or boolean started; And you can pass this to the GUI and read off it when you want the information. Hope you understood this. (also, capitalise your class names :p) Edited October 8, 2016 by Auron Quote Link to comment Share on other sites More sharing options...
House Posted October 8, 2016 Share Posted October 8, 2016 remove the main() method from your gui and create an instance in your script class. you can then reference the gui from there and its variables. Quote Link to comment Share on other sites More sharing options...
Alek Posted October 8, 2016 Share Posted October 8, 2016 You can have getters in your GUI class, then check it in your Main class. Main: if(GUI.clickedStart()) { var1 = GUI.getVar1(); var2= GUI.getVar2(); GUI.dispose();} Quote Link to comment Share on other sites More sharing options...