This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes Problem Selecting DataTable Row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Problem Selecting DataTable Row" Watch "Problem Selecting DataTable Row" New topic

Problem Selecting DataTable Row

john cartman

Joined: Jun 29, 2011
Posts: 6
Hi, all
this is my first post on coderanch, or any java forum for that matter.
Ok, so im pretty new to the java scene. ive been working on some projects for personal practice, and im stuck on one
I have built an h:datatable that gets populated from an array in the backing beans
the table works alot like an inbox, it displays from, subject, date, etc

what i cant figure out how to do is have a row be selected on click, and then display the body of the message in a textarea.

like i said, i'm a super noob at coding, i have a couple of books that ive been using for help, but i cant seem to find this solution in them

can anyone help me out?

idk if this is important but im using jdeveloper
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218
Unfortunately the basic reference implementation of JSF that you are probably using does not
easily support such a simple selection mechanism. There are ways of doing what you want, and
a good pointer is to search for BalusC. Although the advice from BalusC is pioneering and
always excellent in this case it is unlikely to easily help you.

What I would suggest is that, in addition to the basic JSF core functionality, you consider using
a component library. There are several options including ICEFaces, RichFaces and PrimeFaces.
I would recommend looking at PrimeFaces. Download the primefaces jar file and add it to your
classpath. Then add the "p:" namespace declaration to your .xhtml files as per the documentation.
Then go ahead an try the p:dataTable selectionMode="single" option.
john cartman

Joined: Jun 29, 2011
Posts: 6
hi brendan, thanks for the help, i literally spent an entire day googling this issue with no luck, all i kept finding was how to do it with those libraries you mentioned. however im not sure whether i can use those libraries, since idk if they will create any kind of compatibility issues with certain browsers and what not.

ill give it a shot and post back later letting you know how it went
Brendan Healey
Ranch Hand

Joined: May 12, 2009
Posts: 218

Primefaces runs on jQuery which is designed specifically to avoid browser compatibility issues.
In reality you won't get far without a component library unless you're prepared to do it all
yourself, which would be a massive case of re-inventing the wheel. JSF comes with only a very
basic set of components, the biggest and most obvious omission being the lack of ability to
select a row from a datatable. Well, I say that but BalusC solved this problem years ago:

Unless you've reached a fairly high level of competence in javascript and JSF internals I
wouldn't really recommend going the DIY route to be honest.

Primefaces 3 should be out within the next few months and I've high hopes that it will take the
high ground in the component lib space. In the meantime you can download v2.2.1 from here:

You just need to install the one jar file in your classpath and you can be up and running in
minutes. I don't work for primefaces or anything but I went through an evaluation process
of the available frameworks and this was my choice.

Dieter Quickfend

Joined: Aug 06, 2010
Posts: 503

If your requirement is simple, you can always just generate a commandlink in the table, use CSS to make the record feel like a button, and submit the record ID the backing bean, which then populates a bean with the message data. If that bean is available and not empty, you generate a textarea with the body in.

I do agree that it's much easier with a component library though. The greatest strength of JSF is that you basically let people do the pretty stuff for you - server and client side.

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
john cartman

Joined: Jun 29, 2011
Posts: 6
ok, so im using adf components now, and i can easily select the rows in the table, however now my js doesnt work since the attribute onclick, onchange, etc dont work anymore.

i need to be able to use the checkbox in the header to check/uncheck all the other checkboxes in that column
i need my delete button to only be activated if 1 or more checkboxes are checked, and deactivated if none are checked
and i need to make it so when i click on a row, the message body is displayed in some sort of text area below the table

all through beans if possible

should i post that in a different section?

here is the code for the button and table

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950

Actually, from the sound of it, all of what you're trying to do can be done with the basic core JSF without the actual need for PrimeFaces, ADF or any other extension tagset.

However, if you want to replace JUST the text in a textarea when you click on the row corresponding to that email, you need AJAX. Otherwise the whole page gets refreshed. For JSF 2, AJAX is part of the core. For JSF 1.x you would need an AJAX-aware extension tag library.

This sort of functionality is pretty simple. For example, if you make the email title into a commandLink object, the associated action processor can easily determine which row of the dataTable's underlying datamodel was selected. Then you use that information to retrieve the email body text and slap it into the property that supplies the value for the textArea control. When the page is refreshed (or the textarea is re-rendered by AJAX), the updated body text will magically be displayed.

OK it's not really magic, but it's pretty cool.

Customer surveys are for companies who didn't pay proper attention to begin with.
I agree. Here's the link:
subject: Problem Selecting DataTable Row
Similar Threads
Forte 3.0 for Java: A newbies question
CORBA Holders
poll - text editor vs development env.
java on os x (very beginning)
Tile constraints