permaculture playing cards*
The moose likes JSP and the fly likes Changing line alternate line color using tags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Changing line alternate line color using tags" Watch "Changing line alternate line color using tags" New topic
Author

Changing line alternate line color using tags

Tony Walters
Ranch Hand

Joined: Feb 13, 2003
Posts: 54
Does anyone have an example of how you would use custom tags to change the line color in a table of results, so that its a bit clearer where one line ends and another begins.
I don't know if i've made myself very clear. For example, if i had a table with 10 lines of information i want the 1st line in yellow (say) then the second in green, then the next in yellow, then green, and so on.
Any help would be much appreciated. Been searching the net for ages without any success.
Marty Hall
Author
Ranch Hand

Joined: Jan 02, 2003
Posts: 111
Does anyone have an example of how you would use custom tags to change the line color in a table of results, so that its a bit clearer where one line ends and another begins.
I don't know if i've made myself very clear. For example, if i had a table with 10 lines of information i want the 1st line in yellow (say) then the second in green, then the next in yellow, then green, and so on.

You already know how to make custom tags that use doAfterBody, call getBodyContent and read/modify their body content, right (if not, see Ch. 14 of http://pdf.coreservlets.com)? If so, you could certain wrap such a tag around a table so that it searches for the TR tag and adds a BGCOLOR attribute.
Sounds like more trouble than it is worth, though. Why not just use a style sheet:

Cheers-
- Marty


Java training and consulting
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 230

I would rather use a simple approach of using a boolean

This sure is elementary but what's the harm?
[ February 13, 2003: Message edited by: Debashish Chakrabarty ]

Debashish
SCJP2, SCWCD 1.4, PMP, ITIL Foundation
Tony Walters
Ranch Hand

Joined: Feb 13, 2003
Posts: 54
Well, I guess they both seem like good suggestions, but neither is suitable. I can't use style sheets because there is only one row (which is iterated over repeatedly) which means there is effectively no even row to give a style to. Also, the code has to be in a custom tag. The rest of the site is written entirely in custom tags and struts.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Regardless of where the rows are generated (on-page, or in tag code) there's probably a loop that is generating the multiple rows in which you can keep track of odd/even rows. How does this prevent you from applying alternate styles to the rows?
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Tony Walters:
Well, I guess they both seem like good suggestions, but neither is suitable. I can't use style sheets because there is only one row (which is iterated over repeatedly) which means there is effectively no even row to give a style to. Also, the code has to be in a custom tag. The rest of the site is written entirely in custom tags and struts.

Why didn't you say so? In that case, check here:
http://husted.com/struts/resources/rowtag.zip
This is a good, working set of tags. I used it myself until I wrote my own set of tags in our own packaging for other reasons.
For other resources, check the struts resources page at http://jakarta.apache.org/struts/userGuide/resources.html
(And I suggest posting struts questions in the framework forum! )


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Tony Walters
Ranch Hand

Joined: Feb 13, 2003
Posts: 54
Thanks for the suggestions everyone. Unfortunately, while they all would work, the spec i am working to dictated that none of them are suitable for this particular application. The tag needs to operate without generating html - so, having had a quick look, Davids tags don't suit as they generate the 'td' tags.
Maybe it can't be done. I thought i had seen an example somewhere but didn't bookmark it.
I'll give the struts forum a go if i get time. I thought posting here was more suitable because custom tags are not strictly to do with struts but JSP.
Thanks again for the suggestions.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
The tag needs to operate without generating html
I may be misinterpreting you here, but HTML is what web pages are made of, and even CSS depends on HTML markup to tell it what to act on. If you want to change how specific parts of a web page look, you have to use different HTML.
If what you really mean is that the <TD> etc. are already generated, then you can do this several ways, but really it's much more of a HTML/JavaScript/CSS issue than one of JSP tags.
My suggestion is to take an example of the existing output, save it to a file, and experiment by editing it manually to see if you can achieve what you want without changing the already fixed bits. When you find somethning you like, you can put the changes you prefer into a tag.
Possible ways of doing it:
  • generate a nested table inside each table cell, with width 100%, no padding or spacing, and a single cell of the appropriate background color
  • wrap your cell contents in something recognizable by CSS (alternation stylesof "span" or whatever) and apply CSS changes to set the color
  • mark your lines some other (non-HTML?) way and install a filter catching the output of the JSP which patches the <TD> when it sees your marker.


  • Others are probably possible. Ask in HTML/JavaScript and I expect you'll get some suggestions!


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    David Hibbs
    Ranch Hand

    Joined: Dec 19, 2002
    Posts: 374
    Originally posted by Tony Walters:
    The tag needs to operate without generating html - so, having had a quick look, Davids tags don't suit as they generate the 'td' tags.
    Maybe it can't be done. I thought i had seen an example somewhere but didn't bookmark it.

    If you saw it, it almost had to be at a site devoted to dhtml and/or javascript. Try http://DynamicDrive.com and http://javascript.internet.com/
    IMHO though it's a bogus requirement for a tag to not generate HTML tags.
     
    jQuery in Action, 2nd edition
     
    subject: Changing line alternate line color using tags