1

I have created a Java dialog which uses embedded HTML to display a message. The message should contain a link that, when clicked, it should fire an event (launch another dialog). I can't figure out how to do it. Is this really possible?

Here is the code:

message = new JLabel("<html>You have selected <i>"+registry_name+" "+ registry_version +"</i><BR> in the " +"<FONT COLOR=\"#0000FF\"><U><A href=\"javascript:popup();\" id=\"test-link\">container.</A></U></FONT>" +"<script type=\"text/javascript\">"+"function popup(){var generator=window.setVisible(true);}</script></html>");
   JOptionPane.showConfirmDialog(dialog.dialog, message , "Selection Window",JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);

2 Answers 2

2

You could create a JLabel with the appropriate formatting to make it look and act like a link (blue and underlined with the "hand" mouse cursor).

public class Link extends JFrame {
  public static void main(String[] args) {
    new Link();
  }

  public Link(){    
    JLabel link = new JLabel("<html><font color=\"#0000ff\"><u>The link</u></font></html>");
    link.setCursor(new Cursor(Cursor.HAND_CURSOR));
    link.addMouseListener(new MouseListener(){
      public void mouseClicked(MouseEvent e) {
        System.out.println("Link was clicked");
      }
      //other MouseListener methods 
    });

    add(link);
    pack();
    setVisible(true);       
  }
}

Not sure if you can put a link inside a JLabel that also contains normal text. In that situation, you might need to create multiple JLabels.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a read only JEditorPane and put a link and use a HyperlinkListener.

See exemple on javadoc JEditorPane.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.