2013-08-12 2 views
0

찾았습니다. here : 검색 대상은 있지만 여전히 문제가 있습니다.새로 고침 EditorPane

이 내 액션 코드 :

여기
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException { 

    jEditorPane1.setContentType("text/html"); 


    int returnVal = FileChooser1.showOpenDialog(this); 
    if (returnVal == FileChooser1.APPROVE_OPTION) { 


String image = String.format("<img src=\"%s\">", FileChooser1.getSelectedFile()); 
    jEditorPane1.setText(image); 

    } 
} 

하면 이미지가로드되지 볼 수 있듯이, 무슨의 스크린 샷이다. http://postimg.org/image/agc665ih1/

하지만 (저장 버튼 포함) 파일을 저장하고 (오픈 버튼) 같은 파일을 다시 열 경우, 이미지가 완벽하게로드됩니다.

이미 .repaint() 및 .revalidate() 메소드를 시도했지만 작동하지 않습니다. 아이디어가 있습니까?

답변

0

JEditorPane 페이지에서 경로를 설정하는 데 문제가있을 수 있습니다.

String image = String.format("<img src=\"%s\">", FileChooser1.getSelectedFile().getPath()); 

JEditorPane에 적절한 editorKit를 이미 선택했다고 가정합니다.

+0

내가 같은 결과 얻을 수 없음 :/ JEditorPane의 내용은 내가는 HTMLEditorKit(), 같은 코드보기를 선택을 : \t \t jEditorPane1.setEditorKit (새 HTMLEditorKit의()) ; – DJack

+0

이미지를 여는 방법, 그 코드를 붙여 넣을 수 있습니까? – Ashwani

+0

이게 문제가 아니지만 내 평판 때문에 몇 시간을 기다려야하기 때문에 나는 내 자신의 질문에 답할 수있는대로 코드를 붙여 넣을 것이다. :) – DJack

0

이제 코드로 대답 할 수 있습니다. 파일 선택기에이 클래스를 사용하고 있습니다.

import java.io.File; 

import javax.swing.filechooser.FileFilter;

클래스 jpgfilter는 FileFilter를 {

public boolean accept(File file) { 
     return file.isDirectory() || file.getAbsolutePath().endsWith(".jpg"); 
    } 

    public String getDescription() { 

     return "JPG image (*.jpg)"; 
    } 

}

을 확장 그리고 내 메인 클래스에서 나는이 있습니다

FileChooser1 = new javax.swing.JFileChooser(); 
    FileChooser1.setDialogTitle("Choose your image:"); 
    FileChooser1.setFileFilter(new jpgfilter()); 

을 그리고 그것 뿐이다.

0

실제로 해결책을 찾았지만 코드가 너무 많아서 쉽게 처리 할 수 ​​있다고 생각합니다. 실제로 이미지를 삽입하고 EditorPane의 내용을 .html 파일로 저장하고 엽니 다. .

코드 :

jEditorPane1.setContentType("text/html"); 

    int returnVal = FileChooser1.showOpenDialog(this); 
    if (returnVal == FileChooser1.APPROVE_OPTION) { 

     String image = String.format("<img src=\"%s\">", FileChooser1 
       .getSelectedFile().getPath()); 

     jEditorPane1.setText(image); 

     String type = jEditorPane1.getContentType(); 

     OutputStream os = new BufferedOutputStream(new FileOutputStream(
       "/Library/java_test/temp" + ".html")); 

     Document doc = jEditorPane1.getDocument(); 
     int length = doc.getLength(); 

     if (type.endsWith("/rtf")) { 
      // Saving RTF - use the OutputStream 
      try { 
       jEditorPane1.getEditorKit().write(os, doc, 0, length); 
       os.close(); 
      } catch (BadLocationException ex) { 
      } 
     } else { 
      // Not RTF - use a Writer. 
      Writer w = new OutputStreamWriter(os); 
      jEditorPane1.write(w); 
      w.close(); 
     } 

     String url = "file:///" + "/Library/java_test/temp" + ".html"; 

     jEditorPane1.setPage(url); 

    }