跨应用程序重用JEditorPane

时间:2013-02-09 13:13:55

标签: java swing code-reuse jeditorpane jtextcomponent

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,因为它们有各种超链接,它们的行为方式相同。

但我不知道如何调用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);

我将它们更改为什么?

1 个答案:

答案 0 :(得分:0)

难题,因为我不知道你的系统是如何设计的。以下是2个提案:

  1. 从对象的构造函数中自动调用方法initEditorPane(如果所有这些窗格都应该有支持)
  2. 在EditorPane中进行静态初始化。

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

  3. 而且,正如垃圾桶的评论中所述,自Java 6以来,您可以使用类桌面来调用浏览器。

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