I saw some query in the Sun Java Studio Creator forum about adding components dynamically to a Table component if the table itself is dynamically created. I've already blogged on how to dynamically create a Table component. In this entry, I'll explain how to add a Button component to the dynamic table and then to display the data corresponding to button row when it is clicked. You can down load the complete sample application from here here.
This is a continuation of my previous blog How to create a table component dynamically. So you need to read that blog first.
After following the steps in my previous blog to add the three Table Columns and StaticText as child component, in order to add the button component to the fourth column, add the following code to the createTabe() method before returning the dynamic table.
// Create the fourth table Column
TableColumn tableColumn4 = new TableColumn();
tableColumn4.setId("tableColumn4");
// Add the fourth table Column to the table row group
rowGroup.getChildren().add(tableColumn4);
// Create the button and set its action binding as button1_action
Button button1 = new Button();
button1.setText("Select");
button1.setId("button1");
button1.setAction(getApplication().createMethodBinding
("#{Page1.button1_action}", null));
// Add the Static Text3 to the table column3
tableColumn4.getChildren().add(button1);
Create a new page (Page2.jsp), to display the data after clicking the button inside the Table component. Add three Label components and three StaticText components to display the data (Trip Id, Departure Date and Departure City), from the three column of the button row.
Using the Navigation Editor, create a connection between Page1 and Page2 and leave the connection label as case1. If you change the label, you need to manually modify the button action handler code (see below).
Add three properties tripId (type String), departureDate (type java.util.Date) and departureCity (type String) to the session bean using Session Bean->Add->property in the project node. This is to hold the data when the button is clicked and then to display it in the second page after navigate to it.
Go to Page2 and bind the three
StaticText to the three
Session Bean properties (tripId, departureDate,
departureCity )we created above using the binding dialog.
Note, in the code above, the button's action property is bound to a method called button1_action through createBinding() method JSF Application. So we need to add the method to the backing bean (Page1.java) as shown below.
public String button1_action() {
int tripId = ((Integer)getValue("#{currentRow.value[\'TRIP.TRIPID\']}")).intValue();
Date depdate = (Date) getValue("#{currentRow.value['TRIP.DEPDATE']}");
String depCity = (String)getValue("#{currentRow.value['TRIP.DEPCITY']}");
getSessionBean1().setTripId(tripId);
getSessionBean1().setDepartureDate(depdate);
getSessionBean1().setDepartureCity(depCity);
return "case1";
}
In the above code, the values of the three columns (TRIPID, DEPDATE, DEPCITY) corresponding to the currentRow (button row, set by the table component decode during JSF life cycle) are obtained and set to the Session Bean properties (tripId, departureDate, departureCity) and the navigation case label case1 is returned. Since case1 is the connection label from Page1 to Page2 as we specified in the Navigation Editor, when the button is clicked, the navigation to Page2 happens. Since in Page2, we already bound the three Static Text to the three Session Bean properties (just set before navigation by the button handler), the data from the button row will be displayed here.