creationx Posted February 11, 2016 Share Posted February 11, 2016 (edited) Ok, so here is my current GUI code for my Curse Bot private void createGUI(){ final int GUI_WIDTH = 350, GUI_HEIGHT = 85; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2); final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2); gui = new JFrame("XCurseCustom V(0.3)"); gui.setBounds(gX, gY, GUI_WIDTH, GUI_HEIGHT); gui.setResizable(false); JPanel panel = new JPanel(); gui.add(panel); JLabel label = new JLabel("Select A Spell and A Monster!"); // Create a label label.setForeground(Color.WHITE); // Set text color to white panel.add(label); // Add it to the JPanel JTextField selectYourMonster = new JTextField(20); selectYourMonster.addActionListener(e -> monsterSelect = selectYourMonster.getText().toString()); panel.add(selectYourMonster); JButton startButton = new JButton("Start"); startButton.addActionListener(e -> { started = true; gui.setVisible(false); }); panel.add(startButton); gui.setVisible(true); } I have also declared private String monsterSelect = ""; and have the action code set to NPC NPC = npcs.closest(monsterSelect); Yet when I go to start the bot it acts like there was no input in the box (even if I put something in there). Now if I change the declaration to say "Monk of Zamorak" It will then go after the Monk of Zamorak. So what I'm noticing is that somewhere my code is messed up and it's not storing the data from the GUI. I've looked a lot online and on these forums and can't really find a proper answer. edit: Also, to those who are taking the time to answer, PLEASE explain the process and why my method was incorrect and why yours is correct. I'm really really interesting in learning this and would love to get in-depth analysis on what is wrong. Edited February 11, 2016 by creationx Quote Link to comment Share on other sites More sharing options...
Vilius Posted February 11, 2016 Share Posted February 11, 2016 You could try this: startButton.addActionListener(e -> { started = true; monsterSelect = selectYourMonster.getText().toString(); gui.setVisible(false); }); And if you are doing this to stop your script: public void onStart(){ gui.setVisible(true); while(started != true){ Thread.sleep(); } } Don't do it, do it like I show in this tutorial: http://osbot.org/forum/topic/91963-using-synchronized-to-your-advantage/ 2 Quote Link to comment Share on other sites More sharing options...
FrostBug Posted February 11, 2016 Share Posted February 11, 2016 Ok, so here is my current GUI code for my Curse Bot private void createGUI(){ final int GUI_WIDTH = 350, GUI_HEIGHT = 85; Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); final int gX = (int) (screenSize.getWidth() / 2) - (GUI_WIDTH / 2); final int gY = (int) (screenSize.getHeight() / 2) - (GUI_HEIGHT / 2); gui = new JFrame("XCurseCustom V(0.3)"); gui.setBounds(gX, gY, GUI_WIDTH, GUI_HEIGHT); gui.setResizable(false); JPanel panel = new JPanel(); gui.add(panel); JLabel label = new JLabel("Select A Spell and A Monster!"); // Create a label label.setForeground(Color.WHITE); // Set text color to white panel.add(label); // Add it to the JPanel JTextField selectYourMonster = new JTextField(20); selectYourMonster.addActionListener(e -> monsterSelect = selectYourMonster.getText().toString()); panel.add(selectYourMonster); JButton startButton = new JButton("Start"); startButton.addActionListener(e -> { started = true; gui.setVisible(false); }); panel.add(startButton); gui.setVisible(true); } I have also declared private String monsterSelect = ""; and have the action code set to NPC NPC = npcs.closest(monsterSelect); Yet when I go to start the bot it acts like there was no input in the box (even if I put something in there). Now if I change the declaration to say "Monk of Zamorak" It will then go after the Monk of Zamorak. So what I'm noticing is that somewhere my code is messed up and it's not storing the data from the GUI. I've looked a lot online and on these forums and can't really find a proper answer. edit: Also, to those who are taking the time to answer, PLEASE explain the process and why my method was incorrect and why yours is correct. I'm really really interesting in learning this and would love to get in-depth analysis on what is wrong. Action listeners on text fields only trigger when you hit ENTER. Some approaches you can use are either to make the textfield a class member, and use its value directly in the closest filter; or use an event on the button instead. Though in reality you should probably redesign the whole thing, and separate the user interface code from the main script class. 1 Quote Link to comment Share on other sites More sharing options...
creationx Posted February 11, 2016 Author Share Posted February 11, 2016 Thank you for your advice everyone! I still have tons to learn, but I appreciate the help. Quote Link to comment Share on other sites More sharing options...
FrostBug Posted February 11, 2016 Share Posted February 11, 2016 Thank you for your advice everyone! I still have tons to learn, but I appreciate the help. Also, alternatively to getting the screen size, setting null as the relative location will put the frame in the center of the screen. frame.setLocationRelativeTo(null) 1 Quote Link to comment Share on other sites More sharing options...
Chicken Wing Posted February 11, 2016 Share Posted February 11, 2016 (edited) Action listeners on text fields only trigger when you hit ENTER. Some approaches you can use are either to make the textfield a class member, and use its value directly in the closest filter; or use an event on the button instead. Though in reality you should probably redesign the whole thing, and separate the user interface code from the main script class. Or you could just add a document listener instead of an action listener? https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html edit: think this works selectYourMonster.getDocument().addDocumentListener(new DocumentListener() { @Override public void insertUpdate(DocumentEvent e) { onUpdate(); } @Override public void removeUpdate(DocumentEvent e) { onUpdate(); } @Override public void changedUpdate(DocumentEvent e) { onUpdate(); } private void onUpdate(){ monsterSelect = selectYourMonster.getText(); } }); Edited February 11, 2016 by Chicken Wing 1 Quote Link to comment Share on other sites More sharing options...