0

I am developing an application that uses JEditorPane to display data from a database.

The EditorPane is suppose to append resultSet in HTML format. My Problem is that the JEditorPane wipes away the first resultSet and only displays the last resultSet.

Below is excerpt of my code:

   while(rs.next()){
    String htmlComent = "<"html>"<b>
    + "<"head><"title><"/title>"</>
    + "<"body>"<b>
    + "<"hr>"
    + "<"b align='left'> Subject"+"    : "+rs.getString(1)+"<"/b><"br>"
    + "<"b align='left'> Institution"+": "+rs.getString(2)+"<"/b><"br>"
    + "<"b align='left'> Date" +"      "+": "+rs.getString(3)+"</b><br>"
    + "<"b align='left'> Minuted To "+": "+rs.getString(4)+"</b><"br>"
    + "<"b align='left'> Minuted by "+": "+rs.getString(5)+"<"/b><"br>"
    + "<"hr>"
    + "<"p align = 'left' ><"B>Comment<"/B><"/p>"

    +  rs.getString(6)
    + "<"/body>"
    + "<"/head>"
    + "<"/html>";
     append(htmlComent+ "\n" );

..............................

/**
 *  This method appends text to a JEditorPane
 */
public void append(String s) {
    try {
      HTMLEditorKit kit = new HTMLEditorKit();
      HTMLDocument doc2 = new HTMLDocument();
      editorPane.setEditorKit(kit);
      editorPane.setDocument(doc2);
      HTMLDocument doc = (HTMLDocument)editorPane.getDocument();
      kit.insertHTML(doc, doc2.getLength(), s, 0, 0, null);
      //kit.insertHTML(doc, doc2.getLength(),, s, 0, 0, null);
   } catch(BadLocationException |IOException exc) {
      JOptionPane.showMessageDialog(this, exc.getMessage());
   }
}

..........................

Could anybody tell me where I am going wrong?

Ajean
  • 5,528
  • 14
  • 46
  • 69

3 Answers3

0
 String htmlComent = "<html><b> <head><title></title></head><body><b>";

while(rs.next())
{
     htmlComent=htmlcoment +
     "<"hr>"
    + "<"b align='left'> Subject"+"    : "+rs.getString(1)+"<"/b><"br>"
    + "<"b align='left'> Institution"+": "+rs.getString(2)+"<"/b><"br>"
    + "<"b align='left'> Date" +"      "+": "+rs.getString(3)+"</b><br>"
    + "<"b align='left'> Minuted To "+": "+rs.getString(4)+"</b><"br>"
    + "<"b align='left'> Minuted by "+": "+rs.getString(5)+"<"/b><"br>"
    + "<"hr>"
    + "<"p align = 'left' ><"B>Comment<"/B><"/p>"
    +  rs.getString(6) +"</br>";
    }

htmlcoment=htmlcomment+ "</body></html>";

your string append should go like this so tht all your data in resultset will get append in one string then you can use this string wherever you want

Rupesh Terase
  • 440
  • 3
  • 14
0

initialize the htmlComent string before the while loop, build the string within the loop and call the append method outside the loop.

nanopart
  • 98
  • 1
  • 6
0

The problem is in your append() method and in your String htmlContent..

You should start your htmlContent before the while, like this:

StringBuilder htmlContent = new StringBuilder();
htmlContent.append("<html>");
htmlContent.append("<head>");
htmlContent.append("<title>Your title goes Here</title>");
htmlContent.append("</head>");
htmlContent.append("<body>");

while (rs.next()) {
   htmlContent.append("<hr>");
   htmlContent.append("<b align='left'> Subject    : "+rs.getString(1)+"</b><br>");
   htmlContent.append("<b align='left'> Institution: "+rs.getString(2)+"</b><br>");
   htmlContent.append("<b align='left'> Date      : "+rs.getString(3)+"</b><br>");
   htmlContent.append("<b align='left'> Minuted To : "+rs.getString(4)+"</b><br>");
   htmlContent.append("<b align='left'> Minuted by : "+rs.getString(5)+"</b><br>");
   htmlContent.append("</hr>");
   htmlContent.append("<p align = 'left' ><B>Comment</B></p>");
   htmlContent.append(rs.getString(6));
}

htmlContent.append("</body>");
htmlContent.append("</html>");

//Now you can append your text to the HtmlEditorKit
append(htmlContent.toString());

You should add the header before the iteration, because what you want is to create a single HTML page to show all results. In your code given, You were creating a new Html page to every iteration of the resultSet, that's why it was showing only the last result.

That being said, what We're doing now is to create a single header and, for each iteration, creating the <hr> content

and AFTER the iteration, you close the <body> and <html> tags and append it to your HtmlEditorKit =)

Gabriel Câmara
  • 1,249
  • 15
  • 22