Link to home
Start Free TrialLog in
Avatar of tbboyett
tbboyettFlag for United States of America

asked on

Is it possible to add a web link to a JDialog

Hello, Is it possible to add a web link to a JDialog that will automatically open a web browser and take the user to that page?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you could use a JEditoPane, and add that to your dialog
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi cmaypop,

You can invent something like this:


import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;

public class LinkableDialog extends JDialog implements Runnable, ActionListener {
  JLabel jl = new JLabel("www.google.com");
  public void init() {
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        System.exit(0);
      }
    });
    jl.setForeground(Color.BLUE);
    add(jl);
    pack();
    center(this);
    jl.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        try {
          Runtime.getRuntime().exec("explorer http://"+jl.getText());
        } catch(IOException ioe) {
          ioe.printStackTrace();
        }
      }
    });
  }
  public void actionPerformed(ActionEvent ae) {
   
  }
  public void run() {
    init();
    setVisible(true);
  }
  public void center(Component c) {
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((dim.width - c.getWidth()) / 2, (dim.height - c.getHeight()) / 2);
  }
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new LinkableDialog());
  }
}


but this works only on windows with explorer installed