English 中文(简体)
Java部件的显示链接
原标题:Displaying links in Java components

I want to display a list of links in a java component, doesn t matter what component it is. By links I mean URLs of some sites. Those links can be clicked and doing so opens chosen URL in the default web browser for example google chrome / firefox ( I don t want to display web pages in java, only links ).

I already know how to display single link, but I am having problems with display a list of links. I tried to do it like this :

 public void appendTextToJEditorPane(String text) {

    try {
        Document doc = jEditorPane1.getDocument();
        String newLine = "
";

        String url = "<html><a href=" + text + ">" + text + "<//a><//html>.";

        doc.insertString(doc.getLength(), url, null);
        doc.insertString(doc.getLength(), newLine, null);

    } catch (BadLocationException exc) {
        exc.printStackTrace();
    }


}

    appendTextToJEditorPane("http://google.pl");
    appendTextToJEditorPane("http://wp.pl");
    appendTextToJEditorPane("http://onet.pl");

但是,由于在html关口存在“/”的问题,它没有工作。 我在JEditorPane获得通俗的文本。 如何适当调整联系?

最佳回答

你可以在JLabel部分使用html。 这意味着你也可以通过一名助手使用链接。

label.setText("<html><a href="your-link">link</a></html>.")
问题回答

If you can make an HTML of links and open this page in JEditorPane, will help you to code less and achieve your requirement.

我希望你能这样做。

In Swing, it is possible to put HTML formatted text on a component. So making your text <html><a href="...">...</a></html> should be enough.
Otherwise, you ll have to implement an ActionListener that opens the browser. You can do this with the Desktop.browse(URI) method.

If you make your component clickable you can open the browser from within your program with the URL. See java.lang.Desktop, the browse method specifically.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签