2012-02-20 3 views
0

JEditorPane에 쌓여있는 이미지를 렌더링 중입니다. 나는 JEditorPane이 최고로 꽤 바위 같음을 읽었지만, 이것이 나의 HTML 코드 또는 다른 것의 문제라고 기대하고있다.JEditorPane : HTML 레이아웃이 깨졌습니다.

enter image description here

을 어떻게 그것의 JScrollBar (JEditorPane의)에서 찾습니다 : 여기에 내 콘텐츠가 브라우저에서 보이는 방법

enter image description here

HTML 코드 : http://pastebin.com/EixG3WLH

자바 코드 :

File f = new File("index.html"); 
JEditorPane jep = new JEditorPane(f.toURI().toURL()); 
JScrollPane sp = new JScrollPane(jep); 

JFrame frame = new JFrame(); 
frame.add(sp); 
jep.setEditable(false); 

frame.setVisible(true); 
frame.setSize(500, 500); 
frame.setTitle(wpj.getParse().getTitle()); 

JEditorPane에서이 문제를 해결할 수 있다면 FlyingSaucer를 사용하지 않을 것입니다!

답변

2

당신은 할 수 있습니다 ...하지만 정말 간단하지 않습니다 ... 왜냐하면 JEditorPane은 CSS 절대 위치 지정을 가지고 있지 않기 때문에 어떤 요소가 위치를 가졌는지를 인식해야합니다 : 절대 위치 또는 위치 : 같은 고정 된 속성은 ViewFactory의 확장, 무엇인가 : 당신은이에 (이미지를 배치하는 경우이 경우

public class ExtendedHTMLEditorKit extends HTMLEditorKit{ 
//.... other code here 
    public class MyHTMLFactory extends HTMLFactory{ 
     //other code here 
     @Override 
     public View create(Element elem) { 
      if (isLayered(elem)){ //it means, it has position attribute 
      return new PositionedView(elem); 
      } 
      else 
      return super.create(elem); 
     } 

     boolean isLayered(Element elem){ 
      SimpleAttributeSet sas = new SimpleAttributeSet(elem); 
      StyleSheet styles = (HTMLDocument elem.getDocument).getStyleSheet(); 
      Tag tag = element.getAttributes().getAttribute(AttributeSet.NameAttribute); 
      sas.addAttributes(styleSheet.getRule(tag, element)); 
      return sas.isDefined("position") 
      && !sas.getAttribute("position").toString().equalsIgnorecase("static"); 
     } 
    } 
} 

, 우리는 ... 나도 몰라 당신의 요소에 대한 정확한 뷰를 구축 한 후 필요 간단한 것일 수도 있습니다.) 많은 것들을 ... 코드에서 볼 수 있습니다. divs를 사용하고 있습니다 ...

내가하는 일을 더 많이 설명하겠습니다. 구성 요소를 만들었습니다. 새로운 JEditorPane을보고, 원래 요소의 innerCode를 배치 한 다음 부모 에디터의 올바른 위치로 옮긴다.

이것을 동기화하는 것은 정말 어렵다. ... 나는이 당신을 도움이되기를 바랍니다

public class PositionedView extends ComponentView{ 
    private JEditorPane view; 
    private JEditorPane parent; 

    @Override 
    public Component createComponent(){ 
     if (view == null){ 
     view = new JEditorPane(); 
     } 
     String innerText = dumpInnerText(getElement()); 
     view.setText(innerText); 
     view.setLocation(getAbsoluteX(), getAbsoluteY()); 
     parent.add(view); 
    } 

    @Override 
    public void setParent(View parent) { 
     if (parent != null) { 

     java.awt.Container host = parent.getContainer(); 
     if (host != null && host instanceof JEditorPane) { 
      parent = (JEditorPane) host; 
     } 
     } 

    super.setParent(parent); 
    } 

    protected int getAbsoluteX() { 
    //search for the attribute left or right and calculate the position over parent 
    } 

    protected int getAbsoluteY(){ 
    //search for the attribute top or bottom and calculate the position over parent 
    } 

    protected String dumpInnerText(Element element){ 
    //there are several ways to do it, I used my own reader/writer, 
    //because I've need add special tags support... 
    } 
} 

아 : 만 whant 표시를 사용하는 경우 편집하지만,이 같은

확인 .. 뷰가 있어야합니다 ... 더 간단합니다 ! 또 다른 일이 있습니다 : 이렇게하면 뷰가 불투명하지 않게 보호해야합니다. 즉, 모든 뷰 요소, 다른 경우에는 요소에 대해 빈 칸을 갖게됩니다.

또 다른 것은 뷰의 정확한 치수를 확인해야 할 수도 있습니다. width/height 속성을 얻기 위해 getAbsoluteX/getAbsoluteY를 수행해야 할 수도 있습니다.

+0

분명한 답변을 보내 주셔서 감사합니다. – krslynx

+1

p.s. 필자는 FlyingSaucer라는 타사 라이브러리를 사용하여이 문제를 해결했습니다. – krslynx

2

JEditorPane은 CSS 절대 위치 지정과 맞지 않습니다. 나는 JEditorPane이 제공 할 수있는 것보다 더 많은 것을 성취하려고 노력하고 있다고 생각합니다.

+0

그래, 나는 많이 생각했다. 건배! – krslynx

+0

* "JEditorPane은 CSS 절대 위치 지정에별로 좋지 않습니다."* 나는 당신이 그것을 못 박았다고 생각합니다. +1 –

관련 문제