Nifty- Event system matching problem

Hi all,

I am trying to catch the ButtonCickedEvent of one of my buttons via the event system. After creating the button I am changing the id regarding my needs. After setting the id, the button’s id is something like “MyButtonID-Content-” + random_string_value (like “MyButtonID-Content-Iron”). (The string only has characters.) Well, sounds easy, but I am unable to match this id via regex in the event subscriber. Here is my function:

@NiftyEventSubscriber(pattern = “MyButtonID-Content-”+ “?[a-zA-Z]*”)
public void onButtonTestClicked(final String id, final ButtonClickedEvent event) {

If I am not changing the id after creation, the regex is matching the button and everything is working. I hope someone can help me. :frowning:

I don’t think it will work if you change the ID’s after the fact. What are you trying to accomplish?

For example I have a list of buttons. Each button represent an object. If I hit a button I want to do something with the object behind the button. To know which object I get, I need to parse a parameter somewhere. I know there are two ways. One to attach the (for example) id of the object to the onClick() method via parameter. The second way is to attach the id to the element id of the button. I would prefer the first way, but the objects behind the buttons are changing each time a open the specific panel. And I am unable to change the onClick definition after creation.

I already implemented the second way into a solution for my checkboxes. This allows me to create a random list and still getting the right object. Different was, that the value which had to be parsed was an integer.

Regarding changing the id, I checked my current screen after changing the id and the object was =! null.

Are you adding the buttons in java or do you have an xml setup? You could always just keep track of which button represents what in a hashmap and then use that hashmap to figure out what is what. Otherwise if you change the buttons I would just recreate it all or set a onClick method on your controller and pass what it represents and just change that when the button changes.

I added them via java. Here’s a more detailed discription of my problem:

I have the objects:

I want to attach each name to the buttons like
ButtonID-Content-flower etc.

Next time I open the window the list could be something like

Which means that I have to remap the buttons somehow. As you can see the onClick method via parsing a string wouldn’t solve the problem. And recreating is also something I want to avoid.

I could use a hashmap (button id, object id) like you said which leads to an easy remapping. But I still don’t get it why it’s working with my checkboxes and not with the buttons…

“Otherwise if you change the buttons I would just recreate it all or set a onClick method on your controller and pass what it represents and just change that when the button changes.” - What do you mean by this? Is there a way to change the onClick() method of an element after it’s creation?

May I ask why? How often does this list which controls the buttons change?

It’s possible that the list is changing every time the panel is opening. That depends on a value which determines the list’s content. This value can be changed by the user at any time.

Recreating would make more sense since you only need to do it when you open the window and the length of the list changes. I have a couple of screens I regenerate every second or so that has hundreds of elements without a huge impact on fps. Regenerating a few buttons when you open a window would be trivial. That being said, if you still want to go about doing it this way you can change the onClick method in the java code. Look into the following:


Using the element interactions you can change all of the on click methods of an element.

Thanks, I will give it a try. :smile: