• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

URL's within a container

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to Swing/JFC/AWT
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you trying to have the URL do? Do you want to load a page into a web browser?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pclarke, thank you for your suggestion on how to solve my problem. I really appreciate it. Again, thank you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic