Two Laptop Bag*
The moose likes Swing / AWT / SWT and the fly likes URL's within a container Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "URL Watch "URL New topic
Author

URL's within a container

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi, I hope someone can help me with this trivial question.
I'm just trying to put a URL into a container so it will be clickable for a user. I realize that a container's add methods does not take URL's, but how do I add a URL to a container?
At the moment, I have the URL as a string on a button and when the user clicks the button a new window pops up. I don't like that solution but I'm afraid I don't know how to fix it.
Sincerely
//Bjorn
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Moved to Swing/JFC/AWT


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
What are you trying to have the URL do? Do you want to load a page into a web browser?
Bjorn Lundstrom
Greenhorn

Joined: Oct 01, 2000
Posts: 6
Yes, I'm trying to load a browser with the URL. Although, to solve my problem, I put an actionListener on the label(where I just put a string 'www.blabla.com') and I also put a mouseMotionListener on the label so I could recognize when the mouse was moved over the label. Then I just had my actionPerformed do the rest(opening a new browser, with that string later creating a URL).
I thought there would be an easy way to just implement an URL(object) and just put it on a JLabel, but I couldn't succed doing that. At first I tried instantiating a JLabel with an Icon, which I instantiated with an ImageIcon, which I instantiated with an URL... That didn't work for me though..
I hope you understand what I tried to do here and if you could shed some lights on how to solve it 'properly', I would really appreciate it.
Sincerely
//Bjorn
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Try this. I have subclassed the JLabel class and have given it a constructor that takes a URL. The class simply changes the cursor and the foreground colour on the label when the mouse moves over it.
You'll have to put your own code into the actionPerformed method to actually open then URL in another window.
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class urlLabel extends JLabel
{
URL u;
public urlLabel(URL url)
{
super(url.getHost());
u = url;
this.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("Mouse clicked on label");
}
public void mouseEntered(MouseEvent e)
{
setForeground(Color.red);
setCursor(new Cursor(Cursor.HAND_CURSOR));
}
public void mouseExited(MouseEvent e)
{
setForeground(Color.black);
setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
});

}

public static void main(String[] args)
{
JFrame f = new JFrame("Test");
try
{
f.getContentPane().add(new JButton("Test Button"), BorderLayout.CENTER);
f.getContentPane().add(new urlLabel(new URL("http://www.microsoft.com")), BorderLayout.EAST);
}
catch(Exception e)
{
System.out.println("Exception: " + e);
}
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.pack();
f.setVisible(true);
}
}
Bjorn Lundstrom
Greenhorn

Joined: Oct 01, 2000
Posts: 6
pclarke, thank you for your suggestion on how to solve my problem. I really appreciate it. Again, thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URL's within a container