Как я могу добавить кликабельный URL в JTextArea? - PullRequest
2 голосов
/ 14 мая 2011

Я пишу приложение и использую JTextArea для отображения текста.Теперь я хочу показать какой-нибудь интерактивный URL-адрес в текстовой области вместе с обычным текстом, и если пользователь щелкает URL-адрес, веб-страница, на которую ссылается URL-адрес, должна открываться в новом окне веб-браузера.

Ответы [ 3 ]

3 голосов
/ 14 мая 2011

Используйте JEditorPane с HTMLEditorKit или JTextPane и установите тип контента "text / html"

2 голосов
/ 14 мая 2011

.. URL-адрес должен открыться в новом окне веб-браузера.

// 1.6+
Desktop.getDesktop().browse(URI);
0 голосов
/ 18 марта 2019

Вот пример открытия ссылок из JTextArea:

                JTextArea jtxa = new JTextArea(25,100);
                JScrollPane jsp = new JScrollPane(jtxa);
                JPanel jp = new JPanel();
                jp.add(jsp);
                jp.setSize(100,50);

                jtxa.addMouseListener(new MouseAdapter()
                {
                    public void mouseClicked(MouseEvent me)
                    {
                        if(me.getClickCount()==2) //making sure there was a double click
                        {
                            int x = me.getX();
                            int y = me.getY();

                            int startOffset = jtxa.viewToModel(new Point(x, y));//where on jtextarea click was made
                            String text = jtxa.getText();
                            int searchHttp = 0;
                            int wordEndIndex = 0;
                            String[] words = text.split("\\s");//spliting the text to words. link will be a single word

                            for(String word:words)
                            {
                                if(word.startsWith("https://") || word.startsWith("http://"))//looking for the word representing the link
                                {
                                    searchHttp = text.indexOf(word);
                                    wordEndIndex = searchHttp+word.length();
                                    if(startOffset>=searchHttp && startOffset<=wordEndIndex)//after the link word was found, making sure the double click was made on this link
                                    {
                                        try
                                        {
                                            jtxa.select(searchHttp, wordEndIndex);
                                            desk.browse(new URI(word)); //opening the link in browser. Desktop desk = Desktop.getDesktop();
                                        }
                                        catch(Exception e)
                                        {
                                            e.printStackTrace();
                                        }

                                    }
                                }
                            }                           
                        }

                    }
                });
...