File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Back ground color in Table rows, while using iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Back ground color in Table rows, while using iterator" Watch "Back ground color in Table rows, while using iterator" New topic
Author

Back ground color in Table rows, while using iterator

shiva valanukonda
Greenhorn

Joined: Sep 27, 2010
Posts: 25

Hi,
I am writing some code which is going to display data from DB using Hibernate pagination concept. Here , i am facing the issue with bg color for table rows. i want to put different colors (first row should be light blue and second row should be light gray color. Here is my code.

-----------------
Session ses=factory.openSession();
Criteria crit1=ses.createCriteria(registergoods.class);
crit1.setProjection(Projections.rowCount());
List l1=crit1.list();
Iterator it1=l1.iterator();
if(it1.hasNext())
{
Object o=it1.next();
totNumberOfRecords=Integer.parseInt(o.toString());
}
Criteria crit2=ses.createCriteria(registergoods.class);
crit2.addOrder(Order.desc("purchasedon"));
crit2.setFirstResult((pageIndex*numberOfRecordsPerPage)-numberOfRecordsPerPage);
crit2.setMaxResults(numberOfRecordsPerPage);
List l2=crit2.list();
Iterator it2=l2.iterator();


Criteria crit3=ses.createCriteria(registergoods.class);
crit3.setProjection(Projections.sum("amount"));

List l3=crit3.list();
Iterator it3=l3.iterator();
while(it3.hasNext())
{
am=(Integer)it3.next();
Amount=Double.parseDouble(Integer.toString(am));
System.out.println("am is ::"+Amount);
}
Double bal=26870.00-Amount;
System.out.println("Balance is :: "+bal);
pw.println("<table width=100% border=0 bgcolor=#0299FF><tr><td width=20%>");
pw.println("<font color=Green size=4 valign=right> Balance: ");
pw.println("</td></font><td width=35%>"+bal);
pw.println("</td>");
pw.println("<td width=35%><font color=Red size=4 valign=right> Purchased Amount as on Date is");
pw.println("</td></font><td width=10%>"+Amount);
pw.println("</td></tr></table><br>");

pw.println("<center><table border=0 width=80%>");
pw.println("<tr bgcolor=brown><td><b>ID</b></td> <td><b>PRODUCT</b></td> <td> <b>QUANTITY</b></td> <td><b>AMOUNT</b></td> <td><b>PURPOSE</b></td> <td><b>PURCHASEDON</b></td> </tr>");
while(it2.hasNext())
{
registergoods rg=(registergoods)it2.next();
pw.println("<tr>");
pw.println("<td><input type=checkbox name=check value="+rg.getId()+"></td>");
//pw.println("<td>"+rg.getId()+"</td>");
pw.println("<td>"+rg.getProduct()+"</td>");
pw.println("<td>"+rg.getQuantity()+"</td>");
pw.println("<td>"+rg.getAmount()+"</td>");
pw.println("<td>"+rg.getPurpose()+"</td>");
pw.println("<td>"+rg.getPurchasedon()+"</td>");
pw.println("</tr>");

}
pw.println("</table>");
pw.println("<input type=submit value=Delete>");
int noOfPages=totNumberOfRecords/numberOfRecordsPerPage;
if(totNumberOfRecords>(noOfPages*numberOfRecordsPerPage))
{
noOfPages=noOfPages+1;
}
pw.println("<br>");
for(int i=1;i<=noOfPages;i++)
{
String url="srv2?pageindex="+i;
pw.println("<b><a href="+url+">"+ " |"+i+"|</a></b>");
}
pw.println("</center>");
ses.close();
pw.close();


-------------------

The bold one should fetch the data from DB using resultset, I want to use a color for the first iteration and other color for second iteration like that i want to use total 2 colors..
so please help me in this
Thanks in advance.
Shiva javascript:emoticon('');
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Why are you creating HTML in a servlet? You should be grabbing the data and forwarding it off to a JSP to render the view.

Rule #1: No Java code in a JSP!
Rule #2: No HTML in a servlet!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vinod Sekhar
Greenhorn

Joined: Feb 03, 2004
Posts: 5
declare some integer variable and keep incrementing with +1 it in while (it2.hasNext()) loop, for example: int i;

change the pw.println("<tr>"); to ( i%2 == 1) ? pw.println("<tr bgcolor=gray>") : pw.println("<tr bgcolor=yellow>");
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Or to give yourself more scope to change thing in the future, do it with CSS.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Back ground color in Table rows, while using iterator
 
Similar Threads
To call and load javascript in struts jsp.
Code Review - ChatServlet
HTML does not print properly - table width exceeding 100%?
A handy chart for Math class. Check it out.
getting more dynamic in a result page