Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Adding an action listener to JFormattedTextField

Featured Replies

		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?

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

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

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

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

 

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

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

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

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?

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

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

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
 

  • Author
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

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.