0

I am designing a web browser..It Is displaying the web pages but now the problem is....it is not parsing the page properly (i.e when i browse for "Google" page with my web browser,it is displaying but with a blue background and not aligned properly too)....can anyone help me by saying how to parse(cascading style sheet) in HTML with java swing,,A part of code is displayed below

class Page extends JPanel implements Action Listener
{
JPanel head;
 JTextField addrbar;
JEditorPane webpage;
JScrollPane scr;
JLabel statusbar;
JButton homebtn,refreshbtn,gobtn,hisbtn,bookbtn,shbtn;    
public Page()
{        
    setLayout(new BorderLayout());
    head=new JPanel(); 
    head.setLayout(new FlowLayout());

    homebtn=new JButton("Home");
homebtn.addActionListener(this);
head.add(homebtn);

addrbar=new JTextField(100);
addrbar.addActionListener(this);
    head.add(addrbar);   

    gobtn=new JButton("Go");
    gobtn.addActionListener(this);
    head.add(gobtn);

hisbtn=new JButton("History");
System.out.println("action");
hisbtn.addActionListener(this);
head.add(hisbtn);

bookbtn=new JButton(new ImageIcon("Z://Browser/Source/Resources/Images/bkk.jpg"));
    bookbtn.addActionListener(this);
    head.add(bookbtn);

shbtn=new JButton("Show Bookmarks");
shbtn.addActionListener(this);
head.add(shbtn);

     refreshbtn=new JButton("Refresh");
     head.add(refreshbtn);

     statusbar=new JLabel();   

    try{                      
        URL url = new URL("http://www.google.com");
    PrintWriter ps=new PrintWriter(new FileWriter("Z://Browser/Source/history.txt",true));
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy_HH:mm:ss");
    Date date1 = new Date();



  //  ps.print(url +"_"+date.getDate()+"_"+(date.getMonth()+1)+"#");
    ps.print(url +"_"+dateFormat.format(date1)+"#");
    ps.close();             
    addrbar.setText(url.toString());
        webpage=new JEditorPane(url);
        webpage.setEditable(false);
    webpage.addHyperlinkListener(new SimpleLinkListener(webpage,addrbar,statusbar));
        scr=new JScrollPane(webpage, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(head,BorderLayout.NORTH);
        add(scr,BorderLayout.CENTER);
    add(statusbar,BorderLayout.SOUTH);
        System.out.println(getSize());
      }
    catch(Exception e){}
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    `JEditorPane` only has limited CSS support and HTML 3.2 (I think). My suggestion is to find a different editor – MadProgrammer May 22 '14 at 04:25
  • 2
    Perhaps look to the Java-FX [`WebView`](http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebView.html).. – Andrew Thompson May 22 '14 at 04:25
  • 1
    @MadProgrammer You think right. But then, only a *sub-set* of the HTML 3.2 elements (e.g. no applet or JS support). While a simple web browser could well do without applet support, 'no JS' is a killer.. – Andrew Thompson May 22 '14 at 04:26
  • `bookbtn=new JButton(new ImageIcon("Z://Browser/Source/Resources/Images/bkk.jpg"));` By the time of deployment, that resource will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson May 22 '14 at 04:31
  • 1
    @AndrewThompson I'd have to say this would be one of this reasons I'd consider JavaFX over Swing (I'm an old dog ;)) – MadProgrammer May 22 '14 at 04:44
  • @MadProgrammer For my part, it would be that and the media player. It's 'stick with what I know' combined with an healthy cynicism as to what Oracle considers a core API (I'll be convinced only when it makes it into the JSE Java Docs & the Java Tutorial) that holds me back from paying more attention to it.. – Andrew Thompson May 22 '14 at 05:24
  • @AndrewThompson For my part, JavaFX doesn't know what it wants to be...flash competitor, production user interface, 3D environment...?? I like the idea, but each of those are competing with each other...better to have a core API from which those elements can be plugged in - IMHO – MadProgrammer May 22 '14 at 05:27
  • Use one of ready to use html/css rendering engines. For example CssBox or Flying Saucer. http://stackoverflow.com/a/10081084/1240328 – Gosha U. May 22 '14 at 05:49
  • give me some example to parse css. – user3655074 May 22 '14 at 05:52
  • how to use that CssBox with java swing – user3655074 May 22 '14 at 05:54

0 Answers0