0

I have Java Desktop application that displays some information in a JTable that contains URL + text in the each cell. As I am facing difficulties to show and open URL in cell so I am planning to show the content of cell in JEditorpane. User can open the content of cell in JEditorpane where user can easily see and edit the content.

Again how can I make only the URL click-able and allow the user to open URL in a default browser in JEditorpane.

Please note:

  1. JEditorpane pane will contain both text and URL so I need to make only URL click-able.
  2. The JEditorpane is editable. User can edit the content.
Community
  • 1
  • 1
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88
  • 1
    bor better help sooner please post an http://sscce.org/ demonstate your issue – mKorbel Jan 27 '12 at 09:43
  • possible duplicate of [How to show URL as a click-able URL in Table and allow them to open in default browser?](http://stackoverflow.com/questions/9029514/how-to-show-url-as-a-click-able-url-in-table-and-allow-them-to-open-in-default-b) – trashgod Jan 27 '12 at 12:20

4 Answers4

10
  1. Implement an HyperlinkListener. E.G. in JavaDocs for JEditorPane.
  2. Ensure the JEP is displaying HTML, has a content type of text/html, and is not editable.
  3. On event, Desktop.browse(URI) to the URL.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
8

Andrew's answer is correct. The links in editable JEditorPane tip is available at Processing hyperlinks in editable JEditorPane with HTMLEditorKit.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
StanislavL
  • 56,971
  • 9
  • 68
  • 98
5

You might consider to use a JXTable and JXHyperlink (both in the SwingX project) - they support hyperlinks in the renderer, complete with HyperlinkAction backed by Desktop (the class mentioned by @Andrew Thompson)

forgot to mention that the appropriate renderer is installed by default for class URI, a code snippet which triggers the appropriate DeskTop action out off the box by clicking into the cell containing a URI:

    // quick model which returns URI class
    DefaultTableModel model = new DefaultTableModel(0, 1) {

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return URI.class;
        }

    };
    // fill with supported uri types 
    model.addRow(new Object[]{new URI("mailto:contributor@java.net")});
    model.addRow(new Object[] {new URI("http://swingx.java.net")});
    model.addRow(new Object[] {new URI("http://stackoverflow.com/questions/9031371/how-to-show-url-as-a-click-able-url-in-jeditorpane-and-allow-them-to-open-in-def")});
    model.addRow(new Object[] {new URI("http://dummy.org")});
    // use in JXTable
    JXTable table = new JXTable(model);
    // that's it :-)

Addendum

overlooked your requirement of url + text - in swingx default support, the whole cell is clickable, not only the string representation of the url

kleopatra
  • 51,061
  • 28
  • 99
  • 211
1
    javax.swing.JEditorPane jep = new javax.swing.JEditorPane();
    jep.setEditable(false);
    jep.setContentType("text/html");
    jep.addHyperlinkListener(new BrowserOpener());

    class BrowserOpener implements HyperlinkListener {    
        @Override
        public void hyperlinkUpdate(HyperlinkEvent event) {
            if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                    Desktop.getDesktop().browse(event.getURL().toURI());
                } catch (IOException | URISyntaxException ex) {
                    // TODO 
                }
            }
        }
    }
Marcus
  • 1,857
  • 4
  • 22
  • 44