2013-02-09 6 views
2
public class EditorPane extends javax.swing.JPanel { 

/** 
* Creates new form EditorPane 
*/ 
public EditorPane() { 
    initComponents(); 
} 

private void launchHyperLink(HyperlinkEvent e) { 
    try { 
     if (System.getProperty("os.name").toLowerCase().contains("windows")) { 

      String cmdFileLocation = System.getenv("windir") + File.separator + "system32" + File.separator + "cmd.exe"; 
      Runtime.getRuntime().exec(new String[]{cmdFileLocation, "/c", "start", e.getDescription()}); 
     } 
    } catch (IOException ex) { 
     Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

private void initEditorPane(JEditorPane editorPane) { 
    editorPane.setBorder(null); 
    editorPane.setContentType("text/html"); 
    editorPane.setEditable(false); 
    editorPane.setOpaque(false); 
    editorPane.addHyperlinkListener(new HyperlinkListener() { 

     @Override 
     public void hyperlinkUpdate(HyperlinkEvent e) { 
      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 
       launchHyperLink(e); 
      } 
     } 
    }); 
} 

같은 방식으로 작동하는 다양한 하이퍼 링크가 있으므로 여러 GUI에서 위의 EditorPane을 다시 사용하려고합니다.응용 프로그램 전체에서 JEditorPane 재사용

그러나 GUI 호출시 initEditorPane 메서드를 호출하는 방법을 잘 모르겠습니다.

여기 내 말은 무엇입니까 :

public class MainMenu extends javax.swing.JFrame { 

    private AcademicDTO ac; 

    public MainMenu(AcademicDTO academicDTO) { 
     this.ac = academicDTO; 
     initComponents(); 
     searchTable.init(GUIStaticDataHelper.ACADEMIC_SUMMARY_COLUMNS); 
     myContactTable.init(GUIStaticDataHelper.CONTACT_SUMMARY_COLUMNS); 
     initEditorPane(emailTxtComp); 
     initEditorPane(pageTxtComp); 
     initComponentsWithData(); 
     initListeners(); 
    } 

    public void initComponentsWithData() { 
     nameLabel.setText("" + ac.getTitle() + " " + ac.getForename() + " " + ac.getSurname()); 
     roleLabel.setText(ac.getRole()); 
     roomLabel.setText("Room: " + ac.getRoom()); 
     pageLabel.setText("Page:"); 
     pageTxtComp.setText("<html>&nbsp;<a href='" + ac.getPage() + "'>" + ac.getPage() + "</a>&nbsp;</html>"); 
     hoursLabel.setText("Hours: " + ac.getHours()); 
     phoneLabel.setText("Phone: " + ac.getPhone()); 
     mobileLabel.setText("Mobile: " + ac.getMobile()); 
     emailLabel.setText("Email:"); 
     myContactTable.setData(ac.getContacts()); 
     if (ac.getImage() != null) { 
      imageLabel.setIcon(new ImageIcon(ac.getImage())); 
     } 
     emailTxtComp.setText("<html>&nbsp;<a href='mailto://" + ac.getEmail() + "'>" + ac.getEmail() + "</a>&nbsp;</html> "); 
    } 

emailTxtComppageTxtComp 모두 지금 EditorPane 대신 JEditorPane 유형입니다. 따라서 방법 initEditorPane(JEditorPane editorPane)은 사용할 수 없습니다. 또한

라인

initEditorPane(emailTxtComp); 
      initEditorPane(pageTxtComp); 

제가로 변경합니까?

+0

'Desktop # open()'을'Runtime # exec()'이상으로 고려하십시오. – trashgod

답변

0

어려운 질문입니다. 귀하의 시스템이 어떻게 설계되었는지 모르기 때문입니다. (모든 창 지원을해야하는 경우)

  1. 자동으로 개체의 생성자에서 방법 initEditorPane를 호출
  2. 이 EditorPane에서 정적 초기화를 확인 : 다음은이 개 제안합니다.

    public static void buildPane (EditorPane aPane) { initEditorPane (aPane.editorPane); 브라우저를 호출하는 클래스 데스크톱을 사용할 수있는 Java 6 이후 }

그리고,이 trashgod 별 댓글에 말한대로.

protected void launchHyperLink(HyperlinkEvent e) { 
    if (Desktop.isDesktopSupported()) { 
    try { 
     Desktop.getDesktop().browse(e.getURL().toURI()); 
    } catch (Exception ex) { 
     Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    } 
}