Jump to content

Adding an action listener to JFormattedTextField


Chikan

Recommended Posts

		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?

Link to comment
Share on other sites

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

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^-^)

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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?

Link to comment
Share on other sites

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

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

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*

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