Hyperlink JTextField/JLabel To Open Up Default Mail Client?

how can i hyperlink a jtextfield or jlabel? i want to do this so that when the user clicks on the hyperlinked text it will open up their default mail clientthnx guys
[179 byte] By [Alex1989a] at [2007-11-27 2:35:40]
# 1

the first thing you have to to is to create a "style", that's to say you should first implement actions like changing cursor when mouse in on your component, urderlining the text, ...

the second thing is to create the hyperlink, and this problem is a little bit more difficult because you can do 2 things:

- maybe you just want to "contact" the server, typically you want to send data to a HTTP server via a PHP script so I need to execute script when clicking on the hyperlink

- maybe you want to open a browser and, or the path to browser is set somewhere and you can run a new process, or the path is not defined and I don't know how you can open the wanted URL.

There's a code used to join a server to execute a PHP script. This is nearly the same code if you want to show a html code in a HTML friendly component. To show HTML page, just use "retour" with by example jLabel1.setText(retour);

public boolean showHtmlPage()

{

boolean retour = true;

BufferedReader reader = null;

OutputStreamWriter writer = null;

String adresse = "http://my_site.com/my_page.php";

try

{

//encoding query parameters

String donnees = URLEncoder.encode("value1", "UTF-8")+"="+URLEncoder.encode(var1, "UTF-8");

donnees += "&"+URLEncoder.encode("value2", "UTF-8")+"=" + URLEncoder.encode(val2, "UTF-8");

//creating connection

URL url = new URL(adresse);

URLConnection conn = url.openConnection();

conn.setDoOutput(true);

//sending query

writer = new OutputStreamWriter(conn.getOutputStream());

writer.write(donnees);

writer.flush();

retour &= true;

//reading query

reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String ligne;

while ((ligne = reader.readLine()) != null) { /*System.out.println(ligne);*/ }

} catch (Exception e) { retour &= false; }

finally

{

try{ writer.close(); } catch(Exception e) { retour &= false; }

try{ reader.close(); } catch(Exception e) { retour &= false; }

}

return retour;

}

But I modified this piece of code from a source code I found on a Sun forum so I think you should find something like that if you continue searching on sun website and maybe it will suits you better.

Wahrani_jaleoa at 2007-7-12 2:54:13 > top of Java-index,Java Essentials,Java Programming...
# 2
no im not doing anything across the internet or a network. and im just using the default Java SE 6. all i want is a JTextField to have its text hyper linked so that when the user clicks on the text in the field, their default mail client (i.e. outlook express) is opened.
Alex1989a at 2007-7-12 2:54:13 > top of Java-index,Java Essentials,Java Programming...
# 3
So whats your problem?a) recognizing a "hyperlink"b) opening up the default mail client
camickra at 2007-7-12 2:54:14 > top of Java-index,Java Essentials,Java Programming...
# 4
i know how to open up the default mail client so all i need to know now is how to hyperlink text in a jtextfield. am i even using the right word? i want the text to be blue and underlined and when they click on that text it opens the default mail client. is that what a hyperlink
Alex1989a at 2007-7-12 2:54:14 > top of Java-index,Java Essentials,Java Programming...
# 5
bump
Alex1989a at 2007-7-12 2:54:14 > top of Java-index,Java Essentials,Java Programming...
# 6
> so all i need to know now is how to hyperlink text in a jtextfieldThen Swing related questions should be posted in the Swing forum. Did you search the Swing forum about using hyperlinks?
camickra at 2007-7-12 2:54:14 > top of Java-index,Java Essentials,Java Programming...
# 7

The JComponents support html, so:

Email:

setText("<A HREF=mailto:address@domain.com>test email</A>>");

Web Page:

setText("<A HREF=http://www.google.com>test link</A>");

If it does not appear to be opening stuff, just use the Desktop class:

Desktop.getDesktop().browse(someURL);

Desktop.getDesktop().mail(someURI);

FBLa at 2007-7-12 2:54:14 > top of Java-index,Java Essentials,Java Programming...