Jump to content

Attempting to convert from An Option Box to A Text Field...Issues Though


creationx

Recommended Posts

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

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/

  • Like 2
Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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 by Chicken Wing
  • Like 1
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...