Jump to content

Adding an action listener to JFormattedTextField


Recommended Posts

Posted
		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?

Posted

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());

  • Like 1
Posted
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));

72eba450cd54143b8b733bad018ef4c4.gif(It was what I was looking for btw thanks alot^-^)

Posted
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.

 

Posted
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));

72eba450cd54143b8b733bad018ef4c4.gif(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

Posted
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.

Posted
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

  • Like 1
Posted
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?

Posted (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 by d0zza
Posted

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
 

  • Like 3
Posted
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*

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...