Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

how to open url from string on JOptionPane?

807600Jul 24 2007 — edited Jul 25 2007
hello there
i've made a JOptionPane that contains a string url
and i'm wondering if it is possible to add MousMotionListener
to the url and if it is possible to open that url in the default Browser?
JOptionPane.showMessageDialog(null, " Visit us at www.google.com" ,"Visit us",JOptionPane.ERROR_MESSAGE);

Comments

807600
is it not possible?
any ideas?
807600
I wanted to do the same thing a few weeks ago, but I gave up. It's not impossible, but a bit complicated for my needs. To open the URL in the browser you need to do a system call to open an external application (the web browser) from your Java app. Therefore you must know the correct path to the application and be aware that the syntax could change depending on the OS.
Another way could be to retrieve the remote web content and display it in a JEditorPane. In this manner you can show a remote HTML page, but you don't have the functionalities of a web browser (i.e. navigation through links). And it won't work with dynamically-generated web content.
HTH.
807600
I wanted to do the same thing a few weeks ago, but I
gave up. It's not impossible, but a bit complicated
for my needs. To open the URL in the browser you need
to do a system call to open an external application
(the web browser) from your Java app. Therefore you
must know the correct path to the application and be
aware that the syntax could change depending on the
OS.
Another way could be to retrieve the remote web
content and display it in a JEditorPane. In this
manner you can show a remote HTML page, but you don't
have the functionalities of a web browser (i.e.
navigation through links). And it won't work with
dynamically-generated web content.
HTH.
Feh. If you are using the current version of Java, there's no need to get your knickers in a twist:
Desktop.getDesktop().browse(uri);
807600
any other ideas
807600
any other ideas
????????
807600
i know that code bigdaddy posted but i think i can't use it in my case
is that possible?
807600
Possible that you can't use it? Have you fallen and can't up?
807600
Have you fallen and can't up?
X )

the sense of neophyte helplessness does seem to be
permeating these forums lately.
(2 words-of-the-day down, 354 to go...)
807600
so i have to invoke a mouse event to the url
meaning when the mouse passes on the url it is highlited and when he presses it the browser is opened
807600
why not just make your life easier and put a non editable
jeditorpane in the dialog and let it do all the work for you.
807600
i would be so grateful if you give me a little demo or link to study
because i'm so new
807600
so i have to invoke a mouse event to the url
meaning when the mouse passes on the url it is
highlited and when he presses it the browser is opened
Yes, there is no prepacked "hyperlink" widget in the J2SE, so you have to listen for
a mouse press. Unless you use JEditorPane, but that's overkill:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;

public class HyperExample extends MouseAdapter implements Runnable {
    @Override
    public void run() {
        JLabel label = new JLabel("<html><u>http://java.sun.com</u></html>");
        label.addMouseListener(this);
        JFrame f = new JFrame();
        f.getContentPane().add(label);
        f.pack();
        f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    @Override
    public void mousePressed(MouseEvent evt) {
        try {
            URI uri = new URI("http://java.sun.com");
            Desktop.getDesktop().browse(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new HyperExample());
    }
}
807600
Feh. If you are using the current version of Java,
there's no need to get your knickers in a twist:
Desktop.getDesktop().browse(uri);
I learn something every day. ;-) Thanks BigDaddy.
807600
thanks so much bigdaddy for the help
is there another example for the JOptionPane because it seems difficult to me
807600
i need the JOptionPane example also please
807600
Post what you have. It's not hard to display a given component on a JOptionPane.
807600
sir the following JOptionPane contains a url
i want to open the default browser when the user presses the link
by left mouse click and change the mouse cursor when it passes over the link and it is difficult to me as a newer could you help me?
807600
sir the following JOptionPane contains a url
i want to open the default browser when the user presses the link
by left mouse click and change the mouse cursor when it passes over the link and it is difficult to me as a newer could you help me?
thanks at all
JOptionPane.showMessageDialog(null, " Visit us at www.google.com" ,"Visit us",JOptionPane.ERROR_MESSAGE);
807600
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OptionPaneExample extends MouseAdapter{
    public static void main(String[] args) {
        String msg = "<html><u>http://java.sun.com</u></html>";
        JLabel url = new JLabel(msg);
        url.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        url.addMouseListener(new OptionPaneExample());

        JPanel message = new JPanel();
        message.add(new JLabel("Visit us at "));
        message.add(url);

        JOptionPane.showMessageDialog(null, message);
    }

    public void mousePressed(MouseEvent evt) {
        System.out.println("etc...");
    }

    public void mouseEntered(MouseEvent evt) {
        evt.getComponent().setForeground(new Color(0xC0, 0xC0, 0xF0));

    }

    public void mouseExited(MouseEvent evt) {
        evt.getComponent().setForeground(Color.BLACK);
    }
}
1 - 19
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 22 2007
Added on Jul 24 2007
19 comments
1,632 views