1

I have a jlabel show content, then I add a hyperlink include on content. Using tag . I have a issue how to user can click this hyperlink, the mouseclick listener work. Here is my code:

String languages="<html> Deutsch, English (All), English (United Kingdom), Bahasa Indonesia, Italiano <a href=\"\">  edit</a> </html>";
            GUIConstants.ELM_ALIGN_RIGHT), 240, 0));*/
    JLabel lblLanguage= new JLabel();
    lblLanguage.setFont((new Font("Arial", Font.PLAIN, 12)));
    lblLanguage.setText(languages);

I wan if user click edit link, the popup will show. Otherwise, click the text on content, it's not working. Thanks

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
baopham
  • 17
  • 1
  • 5
  • @EricLeibenguth: The label includes text and hyperlink. I want to the action listening work for hyperlink, not the text. It's different your link. Thanks. – baopham Aug 04 '15 at 10:16
  • I don't think it can be done. Can you use 2 `JLabel` perhaps? Or a 3rd party library, like SwingX, with [`JXHyperlink`](http://javadoc.geotoolkit.org/external/swingx/org/jdesktop/swingx/JXHyperlink.html)? – Eric Leibenguth Aug 04 '15 at 10:21
  • @EricLeibenguth: I can use a JLabel and JLink button,but i have another issue: Position of hyperlink is not flexible.Because I want to the hyperlink always appear the end of character on text. The text can be change by user. I try to using by every way but not success. the layout I use JPane and GridBagConstraints. Could you give me advise. Tks – baopham Aug 04 '15 at 10:28

2 Answers2

2

I think this does what you are trying to achieve :

import java.awt.BorderLayout;   
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

public class HtmlMsg {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Message with HTML link");

        String languages="<html> Deutsch, English (All), English (United Kingdom), "
                + "Bahasa Indonesia, Italiano <a href=\"\">  edit</a> </html>";

        frame.getContentPane().add( panelWithHtmlListener(languages));
        frame.pack();
        frame.setVisible(true);
    }

    /**
     * 
     * @param msg
     *       to be displayed. Should contain HTML or html tag.
     *
     * @return
     *       Returns a panel with an HTML listener, displaying msg.
     */
    private static JPanel panelWithHtmlListener(String msg){

        JEditorPane jEditorPane = new JEditorPane("text/html", msg);
        jEditorPane.setEditable(false);
        jEditorPane.setOpaque(false);

        HyperlinkListener listener = new HyperlinkListener() {
            @Override
            public void hyperlinkUpdate(HyperlinkEvent hyperLink) {

                if (HyperlinkEvent.EventType.ACTIVATED.
                                equals(hyperLink.getEventType())) {
                    try {
                       //respond to html link clicked
                        JOptionPane.showMessageDialog(null, "HTML has been clicked !!" );
                    }
                    catch (Exception ex) { ex.printStackTrace();}
                }
            }

        };

        jEditorPane.addHyperlinkListener(listener);

        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout(5, 5));
        jPanel.add(jEditorPane,  BorderLayout.CENTER);

        return jPanel;
    }   
}
c0der
  • 18,467
  • 6
  • 33
  • 65
1

Given your requirements, I would recommend to use a JEditorPane with a Hyperlink listener, as described in this question: Is it possible to create programs in Java that create text to link in Chrome?

In your case you won't be following a URL, but rather opening a JDialog (so adapt the listener accordingly).

(Don't forget to make it uneditable!)

Community
  • 1
  • 1
Eric Leibenguth
  • 4,167
  • 3
  • 24
  • 51
  • 3
    @baopham, don't forget to "accept" the answer by clicking on the check mark so people know the problem has been solved. – camickr Aug 04 '15 at 14:38