aspose file tools*
The moose likes JSF and the fly likes Changing a component's  properties from a bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Changing a component Watch "Changing a component New topic
Author

Changing a component's properties from a bean

Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
How do you change a component's properties from a bean?

What I want to do: Use a listbox's onchange to hide and show panelGrids.


Thank you in advance (atleast for reading this post),
-Dan
[ July 19, 2005: Message edited by: Daniel Prene ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

There are 2 ways. Both work well. Both will happen in the change listener method you've defined for the listbox.

1. create boolean properties for the rendered attribute of the panels. On the change event make these boolean properties true/false accordingly. Example:





2. Bind the html component with a faces component in the backing bean and then modify the components rendered property. Example:





I don't know how syntactically correct some of the code is as I am not on my normal PC today. However, the concept is correct. Hope that helps.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

FYI - works the same for panelGrid or any component for that matter. My fingers just typed panelGroup.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Thank you. You're solution works wonderfully... but is there a way to negate having to add the name in three diferent places everytime a new item/group is coded? We want the front-end guys out of the back-end code and vesa-versa
Thank you once again,
-Dan
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I coded the following bean in an atempt to solve these problems, but am still clueless on how it fits in:
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Daniel Prene:
Thank you. You're solution works wonderfully... but is there a way to negate having to add the name in three diferent places everytime a new item/group is coded? We want the front-end guys out of the back-end code and vesa-versa
Thank you once again,
-Dan


No, not really. Technically, the front end guys should also be coding the backing beans. Your so called backend guys should be coding all the grunt work that the backing beans use (utility classes, services, DAO, etc).
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
so there's no way of the rendered variable using the value returned by checkRender?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Daniel Prene:
so there's no way of the rendered variable using the value returned by checkRender?


Not sure what you mean by that.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Do the valid equivelent to this:



^using the code I posted earlyer

The above is just a concept sketch of what I want to do.
[ July 19, 2005: Message edited by: Daniel Prene ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

No, you can't do that. It's because of how JSF and the JavaBean spec work together.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Gah! That's rather discouraging... I had hoped JSF would support encapsulation better then this... Alright, thank you endlessly though, you helped me unbeleivably today.

Your humble servent,
-D.P.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Erhm. I guess what I wanted to do wasn't imposible:





Although, I'm having trouble with white space and such... I've also tried subview and panelgroup, but so far panelgrid has been the cleanest (I just don't add <br>) I'm guessing that the html is rendered before faces? but the white space apears even if another component is rendered... Any thoughts?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I forgot about just using an expression. Good job!

As far as the whitespaces, not sure. Where are the white spaces in the HTML? Can you maybe post a screenshot of what you are talking about after the page gets rendered?
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Is it possible to change the subject of these posts to somthing more appropriate?
[ July 20, 2005: Message edited by: Daniel Prene ]
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
I'll start with my panelGroup atempt...

problem: The <br>s from within the panelGroup move to above every panelGroup

included code for panelGroup foo:



html output:
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Try doing this instead for your br tags:



The verbatim tag is used so that JSF knows you want this specific HTML code positioned where you coded them. Otherwise, JSF takes straight HTML and just kind of puts all together in one place. Goofy, but noone said JSF is perfect.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
Thank you once again oh great master of knowledge. I think I may combine all that I've learned here and form a tutorial...

My next goal is to move the panelGroups completely out of the form code... may sound funky but that's what the boss wants... Wish me luck... I'm going to need it! Another question though... is what he wants even possible?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Daniel Prene:
Thank you once again oh great master of knowledge. I think I may combine all that I've learned here and form a tutorial...

My next goal is to move the panelGroups completely out of the form code... may sound funky but that's what the boss wants... Wish me luck... I'm going to need it! Another question though... is what he wants even possible?


Sure, you don't ever have to use panelGroups or panelGrids. It sure makes laying things out easier. But they aren't required for anything.
Daniel Prene
Ranch Hand

Joined: Jul 15, 2005
Posts: 241
but we still need to be able to turn chunks of code on or off when an item is selected
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Daniel Prene:
but we still need to be able to turn chunks of code on or off when an item is selected


That is correct. And now you would have to use the rendered property on each component. You just asked if it was possible.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Changing a component's properties from a bean