troubleshooting Question

Alignment problem of HTML in JEditorPane

Avatar of manyam537
manyam537 asked on
Java
2 Comments1 Solution344 ViewsLast Modified:
Hello,

I have some alignment problem in displaying the results in JEditorPane  using html. But I am not getting displayed properly.

Here is the program:

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Color;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.*;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class SwingTest1 extends JFrame{
  private JEditorPane text;
  private Font textFont;    

  SwingTest1(String title){
    setTitle(title);
    textFont = new Font("Verdana",Font.PLAIN,12);
    text = new JEditorPane("text/html", "");
    text.setFont(textFont);
    text.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
    text.setEditable(false);
    text.addHyperlinkListener(new Hyperactive());
   
     String[] url = {"file://C:/page1.pdf", "file://C:/page2.pdf"};
     String[] column = {"Specification:", "Publication Date:", "Inventors:", "Country:", "Owner:", "Priority Date:", "Belongs to Case:", "Family Members:", "Keywords:", "IPC:", "Title:"};
   
    String title1 =" Measurement Procedure";
      String inventor = "Kultner Franz";
      String owner = "Micronas Gmbh";
      String pri_date ="23-09-98";
      String be_case ="I-123";
      String keywords ="Digital Sensor Hall Digital Sensor Hall Digital Sensor Hall Digital Sensor Hall Digital Sensor Hall ";
      String fam_numbers ="US12345A1 DE34567B2 US12345A1 DE34567B2 US12345A1 DE34567B2 US12345A1 DE34567B2 US12345A1 DE34567B2";
      String IPC ="H04N/445, G11H/456";
      String pub_date = "23-09-1998";
      String country = "EP";
      String ID = "DE12345A1";
   
      
      
    String s = "<html><body><table width =100% border = 0 >";
    for(int k=0; k < 10; k++) {
            for(int i = 0; i < column.length; i=i+2) {
        if(i<10) {
          int j = i+1;

            if(i==0) {
              s += "<tr><td width = 5%><font color = green><u>" + column[i].substring(0,2) +"</u>"+ column[i].substring(2) + "</font><td width = 30%><a href = \""+url[0]+"\">"  + ID + "</a><td width = width = 10%><font color = green><u>" + column[j].substring(0,2) +"</u>"+ column[j].substring(2) + "</font><td>" + pub_date;
            } else if(i==2) {
              s += "<tr><td width = 5%><font color = green><u>" + column[i].substring(0,2) +"</u>"+ column[i].substring(2) + "</font><td width = 30%>" + inventor + "<td width = 10%><font color = green><u>" + column[j].substring(0,2) +"</u>"+ column[j].substring(2) + "</font><td>" + country;
            }
            else if(i==4) {
              s += "<tr><td width = 5%><font color = green><u>" + column[i].substring(0,2) +"</u>"+ column[i].substring(2) + "</font><td width = 30%>" + owner + "<td width = 10%><font color = green><u>" + column[j].substring(0,2) +"</u>"+ column[j].substring(2) + "</font><td>" + pri_date;
            }
            else if(i==6) {
              s += "<tr><td width = 5%><font color = green><u>" + column[i].substring(0,2) +"</u>"+ column[i].substring(2) + "</font><td width = 30%>" + be_case + "<td width = 10%><font color = green><u>" + column[j].substring(0,2) +"</u>"+ column[j].substring(2) + "</font><td>" + fam_numbers;
            }
            else {
              s += "<tr><td width = 5%><font color = green><u>" + column[i].substring(0,2) +"</u>"+ column[i].substring(2) + "</font><td width = 30%>" + keywords + "<td width = 10%><font color = green><u>" + column[j].substring(0,2) +"</u>"+ column[j].substring(2) + "</font><td>" + IPC;
            }


        } else {
          s += "<tr><td width = 5%><font color = green><u>" + column[i].substring(0,2) +"</u>"+ column[i].substring(2)+ "</font><td width = 30%>"  +  title1;
        }
      }
      s += "<tr width = 100%><hr></tr>";
    }
    s += "</table></body></html>";

    text.setText(s);
    this.getContentPane().add(new JScrollPane(text));
    this.pack();
    this.setLocationRelativeTo( null ) ;
    this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
  public static void main(String[] args){
    SwingTest1 test1 = new SwingTest1("Swing Test");
    test1.setVisible(true);
  }
  class Hyperactive implements HyperlinkListener {
    public void hyperlinkUpdate(HyperlinkEvent e) {
      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        try {
          String link = e.getURL().toExternalForm();
          System.out.println("The link value is"+link);
          Runtime.getRuntime().exec("cmd /c start "+link);
        } catch (IOException ioe) {
          System.out.println(ioe);
        }
      }
    }
  }
}



Actually, what I want is display in a table with four columns. First column has to occupy say 10% of width and secon 35% of width , third say 10% and fourth also 35%. And at last I want a row to end that record.

So please some one make it correct so that alignment is good.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros