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);
            }
        }
    });
}

I am trying to re-use the above EditorPane across a number of different GUIs, as they have various hyperlinks which act the same way.

But I am not sure how to call the initEditorPane method in the GUIs calling it.

Here's What I mean:

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> ");
    }

emailTxtComp and pageTxtComp are both now type EditorPane instead of JEditorPane. so the method initEditorPane(JEditorPane editorPane) can't be used.

also the lines

initEditorPane(emailTxtComp);
            initEditorPane(pageTxtComp);

what do I change them to?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
MooHa
  • 819
  • 3
  • 11
  • 23

1 Answers1

0

Difficult question, because i don't know how your system is designed. Here are 2 proposals:

  1. Automatically call the method initEditorPane from the constructor of the object (if all these panes should have support)
  2. Make static initialization in EditorPane.

    public static void buildPane(EditorPane aPane) { initEditorPane(aPane.editorPane); }

And, as it was said in comments by trashgod, since Java 6 you can use class Desktop to call the browser.

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);
    }
  }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48