Dynamic buttons - Event Listeners

Hi all.

I have this, small I hope, problem. I create a Vector object. It is used to store Button objects. Based on the number of banks I have in a database table, I create a number of tables. Each table must have a commit button which is correctly created and rendered in my page. I use something like:

for(int i=0; i<numOfBanks; i++){

// create required objects

// bla bla bla

// create button

Button button =new Button();

// set some properties

// set id

button.setId("something meanigfull");

getMyButtonVector().addElement(button);

getMyPagesPanel().add(button);

Everything is correctly created and rendered. Now, as you understand I may have more than one commit button in my page. I can probably with no difficulty bind a method to my button as the action processing method of my buttons. But I don't want to act upon every table of my page. I need a way to distinguish which button was pressed so that I can update tha according table and not all of the tables.

Can someone help me please?

If more details are required I can provide them.>

[1465 byte] By [Bledara] at [2007-11-27 4:57:57]
# 1

If you're talking about datatables, just play with DTO's and use HtmlDataTable#getRowData() to retrieve the selected row. I've posted something about this in another topic some time ago: http://forum.java.sun.com/thread.jspa?threadID=5174657 Read it thoroughly.

If you're talking about static tables or panelgrids (which is bad design however in this specific case), then you may find actionListener in conjunction with a f:attribute useful. Also see http://balusc.xs4all.nl/srv/dev-jep-com.html

BalusCa at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Sorry, but I don't understand you. Or maybe somehow you may not understand me.

I just want to catch the button clicked action and successfully distinguish which button was pressed. From the other topics you posted I seem to miss the answer I need.

Is there a way for you to be more specific, please?

Bledara at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
I forgot to say that there are no static component except the form, the view a label (irrelevant) and the panel object I use to render on my ui components.
Bledara at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
First of all, which type of table are you using for display?h:dataTable or h:panelGrid?
BalusCa at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

None of them, or at least not in my knowing. I use this library for the components I need:

import com.sun.rave.web.ui.component.*;

I don't know if in the background one of those tables mentioned by you is used.

_

By the way, I checked the second topic u posted earlier.

I created in my session bean a method named actionProcessor(ActionEvent event) {

blablabalba;

}

I bind this method to my button. As earlier I create the buttons using a for loop. All is created ok. I bind the method like this:

private void createUIDynamically() {

for(int i=0; i<4; i++) {

Button button = new Button();

button.setText("Button: " + String.valueOf(i));

button.setId("Button_" + String.valueOf(i));

getLayoutPanel1().getChildren().add(button);

getSessionBean1().getMyButtons().addElement(button);

MethodBinding mb = FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{SessionBean1.actionProcessor}", new Class[0]);

button.setActionListener(mb);

}

getSessionBean1().setShownBefore(true);

}

The action processor method is this one:

public void actionProcessor(ActionEvent event) {

Object obj = event.getSource();

if(obj instanceof Button) {

Button button = (Button) obj;

System.out.println(button.getId());

}

}

When I click one of the buttons an exception is thrown saying that no actionProcessor() method found. Which logical because my method requires an argument. How do I pass that ActionEvent argument?

If you find me stupid I don't blame you. I feel so lost with these technologies.

Bledara at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
You've definied a no-arg actionProcessor method in the createMethodBinding. Pass new Class[] {ActionEvent.class} through it instead of new Class[0].
BalusCa at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thanks dude. That worked, but I guess you knew that it should work. Thanks again. I hope your duke dollars arrived.
Bledara at 2007-7-12 10:13:42 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...