tags:

views:

36

answers:

2

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);
+1  A: 

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.

Michael Angstadt
+1  A: 

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

See exemple on javadoc JEditorPane.

Istao