Chikan Posted December 2, 2017 Share Posted December 2, 2017 NumberFormat format = NumberFormat.getInstance(); NumberFormatter formatter = new NumberFormatter(format); formatter.setValueClass(Integer.class); formatter.setMinimum(1); formatter.setMaximum(99); formatter.setAllowsInvalid(false); formatter.setCommitsOnValidEdit(true); JFormattedTextField eatAtComboBox = new JFormattedTextField(formatter); eatAtComboBox.addActionListener(e -> vars.eatFoodThreshold = (int) eatAtComboBox.getSelectedText()); /*<-- This part won't work, but my question is how can I get the textbox to return an int and to only allow an int. What I tried above doesn't seem to work. Any help appriciated.*/ Essentially I'm trying to create a textbox for my GUI that only accepts 2 int values with a max of 99 and a min of 1, this will be used to set what hp the character will eat at. So, how can I do this to fit what I need? Quote Link to comment Share on other sites More sharing options...
Apaec Posted December 2, 2017 Share Posted December 2, 2017 Have you considered using a JSpinner? This component should host the functionality that you're looking for, and is a more conventional choice when dealing with this kind of input. Best Apa Quote Link to comment Share on other sites More sharing options...
d0zza Posted December 2, 2017 Share Posted December 2, 2017 First, why is your JFormattedTextField called eatAtComboBox? Second, casting a string to an integer like how you're doing won't always do what you expect. Use Integer.parseInt() instead. To do what you want add an actionlistener to your start button and set the variable inside that actionlistener using: eatFoodThreshold = Integer.parseInt(eatAtComboBox.getText()); 1 Quote Link to comment Share on other sites More sharing options...
Chikan Posted December 2, 2017 Author Share Posted December 2, 2017 27 minutes ago, Apaec said: Have you considered using a JSpinner? This component should host the functionality that you're looking for, and is a more conventional choice when dealing with this kind of input. Best Apa I've switched it to a JSpinner, but I'm very unfamiliar with it. Would you be able to help me convert a changelistener to an int? JSpinner eatAtSpinnerBox = new JSpinner(); eatAtSpinnerBox.addChangeListener(e -> vars.eatFoodThreshold = (int) eatAtSpinnerBox.getChangeListeners()); eatAtSpinnerBox.setModel(new SpinnerNumberModel(1, 1, 99, 1)); (It was what I was looking for btw thanks alot^-^) Quote Link to comment Share on other sites More sharing options...
Chikan Posted December 2, 2017 Author Share Posted December 2, 2017 7 minutes ago, d0zza said: First, why is your JFormattedTextField called eatAtComboBox? Second, casting a string to an integer like how you're doing won't always do what you expect. Use Integer.parseInt() instead. To do what you want add an actionlistener to your start button and set the variable inside that actionlistener using: eatFoodThreshold = Integer.parseInt(eatAtComboBox.getText()); It used to be a comboBox, and I changed it without changing the variable name; I just didn't get around to it yet is all. Quote Link to comment Share on other sites More sharing options...
Apaec Posted December 2, 2017 Share Posted December 2, 2017 28 minutes ago, Chikan said: I've switched it to a JSpinner, but I'm very unfamiliar with it. Would you be able to help me convert a changelistener to an int? JSpinner eatAtSpinnerBox = new JSpinner(); eatAtSpinnerBox.addChangeListener(e -> vars.eatFoodThreshold = (int) eatAtSpinnerBox.getChangeListeners()); eatAtSpinnerBox.setModel(new SpinnerNumberModel(1, 1, 99, 1)); (It was what I was looking for btw thanks alot^-^) I'm happy to help - why do you need a change listener though? And what do you mean 'convert changelistener to an int'? Apa Quote Link to comment Share on other sites More sharing options...
Chikan Posted December 2, 2017 Author Share Posted December 2, 2017 Just now, Apaec said: I'm happy to help - why do you need a change listener though? And what do you mean 'convert changelistener to an int'? Apa Sorry if I'm wording it wrong, but how to I get the value of the spinnerBox into the value of what I need what I'm trying right now: JSpinner eatAtSpinnerBox = new JSpinner(); eatAtSpinnerBox.setModel(new SpinnerNumberModel(1, 1, 99, 1)); eatAtSpinnerBox.addChangeListener(e -> vars.eatFoodThreshold = (int) eatAtSpinnerBox.getValue()); I think this is what I was looking for, I'll try it and get back to you. Quote Link to comment Share on other sites More sharing options...
Apaec Posted December 2, 2017 Share Posted December 2, 2017 11 minutes ago, Chikan said: Sorry if I'm wording it wrong, but how to I get the value of the spinnerBox into the value of what I need what I'm trying right now: JSpinner eatAtSpinnerBox = new JSpinner(); eatAtSpinnerBox.setModel(new SpinnerNumberModel(1, 1, 99, 1)); eatAtSpinnerBox.addChangeListener(e -> vars.eatFoodThreshold = (int) eatAtSpinnerBox.getValue()); I think this is what I was looking for, I'll try it and get back to you. spinner.getValue() returns the value; no need for anything complicated like changeListeners etc 1 Quote Link to comment Share on other sites More sharing options...
progamerz Posted December 2, 2017 Share Posted December 2, 2017 17 minutes ago, Chikan said: Sorry if I'm wording it wrong, but how to I get the value of the spinnerBox into the value of what I need what I'm trying right now: JSpinner eatAtSpinnerBox = new JSpinner(); eatAtSpinnerBox.setModel(new SpinnerNumberModel(1, 1, 99, 1)); eatAtSpinnerBox.addChangeListener(e -> vars.eatFoodThreshold = (int) eatAtSpinnerBox.getValue()); I think this is what I was looking for, I'll try it and get back to you. Why don't you just add an actionlistener to the start button where u would set the vars and everything? Quote Link to comment Share on other sites More sharing options...
d0zza Posted December 2, 2017 Share Posted December 2, 2017 (edited) 49 minutes ago, Chikan said: It used to be a comboBox, and I changed it without changing the variable name; I just didn't get around to it yet is all. I've given you literally exactly what you need in my previous post. Why read my first sentence and ignore the rest? 7 minutes ago, progamerz said: Why don't you just add an actionlistener to the start button where u would set the vars and everything? Edited December 2, 2017 by d0zza Quote Link to comment Share on other sites More sharing options...
Chikan Posted December 2, 2017 Author Share Posted December 2, 2017 5 minutes ago, d0zza said: I've given you literally exactly what you need in my previous post. Why read my first sentence and ignore the rest? Because I was fixing it to suit better to what I needed. Thank you for your input though, it is appriciated. I'm using a JSpinner as Apa suggested. 1 Quote Link to comment Share on other sites More sharing options...
Alek Posted December 2, 2017 Share Posted December 2, 2017 I've done this in OSBot before. The easiest way is to let the user type whatever they want. When when a user clicks a "submit" button, then your program parses the text, removes all invalid characters using regex, and processes the result. You can do this or detect invalid characters and give an error message. Edit: Input: v9 3 Output: 93 Or Input: v9 3 Output: Error Message 3 Quote Link to comment Share on other sites More sharing options...
Chikan Posted December 2, 2017 Author Share Posted December 2, 2017 6 minutes ago, Alek said: I've done this in OSBot before. The easiest way is to let the user type whatever they want. When when a user clicks a "submit" button, then your program parses the text, removes all invalid characters using regex, and processes the result. You can do this or detect invalid characters and give an error message. Edit: Input: v9 3 Output: 93 Or Input: v9 3 Output: Error Message Alek senpai has graced me with his pressence *faints* Quote Link to comment Share on other sites More sharing options...