Избегайте отступов / полей в<p>, используемых в JEditorPane

Я хочу иметь кликабельную ссылку во всплывающей подсказке Jlabel. Поскольку это не поддерживается "из коробки", я нашел это решение, которое я адаптировал под свои нужды (см. Код ниже). С обычными всплывающими подсказками я привык использовать парапрафы для автоматического переноса длинных всплывающих подсказок на заданную ширину (например, <html><p width="300">Setting 'File' will show a single video which will be played with the first subtitle found for the specified languages.<br><br>Using 'Folder' will show a folder containing a list of videos with different subtitles.</p></html>).

Теперь, когда я использую абзац с HyperLinkToolTip, сверху есть поле, которое я не могу понять, как удалить. Я пытался играть с полем и отступом, но это не сработало.

package test;

import java.awt.Desktop;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.ToolTipUI;

public class HyperLinkToolTip extends JToolTip {
    private static final long serialVersionUID = -8107203112982951774L;

    private JEditorPane theEditorPane;

    public HyperLinkToolTip() {
        setLayout(new GridLayout());

        theEditorPane = new JEditorPane();
        theEditorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
        theEditorPane.setContentType("text/html");
        theEditorPane.setEditable(false);
        theEditorPane.setForeground(new ColorUIResource(255, 255, 255));
        theEditorPane.setBackground(new ColorUIResource(125, 184, 47));

        theEditorPane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    if(Desktop.isDesktopSupported())
                    {
                      try {
                        Desktop.getDesktop().browse(new URI(e.getDescription()));
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } catch (URISyntaxException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    }
                }
            }
        });
        add(theEditorPane);
    }

    public void setTipText(String tipText) {
        theEditorPane.setText(tipText);
    }

    public void updateUI() {
        setUI(new ToolTipUI() { });
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame(HyperLinkToolTip.class.getName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();

        JButton btn = new JButton() {
            private static final long serialVersionUID = -2927951764552780686L;

            public JToolTip createToolTip() {
                JToolTip tip = new HyperLinkToolTip();
                tip.setComponent(this);
                return tip;
            }

            // Set tooltip location
            public Point getToolTipLocation(MouseEvent event) {
                return new Point(getWidth() / 2, getHeight() / 2);
            }
        };

        btn.setText("Tooltip Test");
        btn.setToolTipText("<html><p width=\"300\">Specifies the languages to search for subtitles. E.g. 'eng', 'eng,fra,esp'.<br><br>See <a href=\"http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes\">this article</a> for a full list of languages.</p></html>");

        panel.add(btn);
        frame.setContentPane(panel);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                frame.setSize(400, 400);
                frame.setVisible(true);
            }
        });
    }
}

Результат выглядит так: Пример HyperLinkToolTip

Вопрос: Может кто-нибудь сказать мне, как избежать разрыва в верхней части всплывающей подсказки?

1 ответ

Решение

Похоже, вы можете использовать <div>...</div> теги вместо <p>...</p> tags,

        btn.setToolTipText("<html><div width=\"300\">Specifies the languages to search for subtitles. E.g. 'eng', 'eng,fra,esp'.<br><br>See <a href=\"http://en.wikipedia.org/wiki/List_of_ISO_639-2_codes\">this article</a> for a full list of languages.</div></html>");
Другие вопросы по тегам