0

Possible Duplicate:
How to add hyperlink in JLabel

In my program, I am searching through an index using Lucene and I am retrieving files. I have created XML files for the retrieved docs from the Lucene's search. Now, I want to make these XML files as hyperlinks and display to the user as the search results. That is I want the XML files to be open when the user clicks on this hyperlink. Any help appreciated!?

for(int i=0;i<file_count;i++)
 {
  file=str+index[i]+".xml";   

JLabel label = new JLabel(file,JLabel.CENTER );

label.setOpaque(true);
label.setBackground(Color.RED);
panel.add(label) ;

label.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
if(evt.getClickCount() > 0)
{

Runtime r= Runtime.getRuntime();
   try {
       System.out.println("testing : Inside mouseclicked");
       Process p = r.exec("cmd.exe /c start "+file);
       System.out.println("opened the file");
   } catch (IOException ex) {
System.out.println(ex.getMessage());
System.out.println();
}
}
}
});
 }

Here is the code that I have made. In this, I am suppose to get output on the screen "file_count" no of times. I am getting that but what is happening is all the links are showing the same file when clicked. Help?

Community
  • 1
  • 1
Aravind Chinta
  • 71
  • 1
  • 4
  • 9
  • Shorter question: How do I create a hyperlink to a file? Answer: results – Steve H. Apr 17 '12 at 19:32
  • @ChadNC: I have tried in JLabel, but there is problem with MouseClicked action! Suppose I have three files to be shown as the output and when I use JLabel's mouse clicked event, in the output panel it is showing the same file for all the three links. What to do? – Aravind Chinta Apr 17 '12 at 19:45
  • @SteveH.: Not in HTML, I want in Java. – Aravind Chinta Apr 17 '12 at 19:46
  • Like @ChadNC said, what have you tried? Please post some code and a specific question where/how it doesn't do what you want. Also, explain in more detail how you want to use JLabels in there -- everyone seems to be thinking you want a web app. – Philipp Reichart Apr 17 '12 at 19:58
  • The Web is full of answers. [Here](http://stackoverflow.com/questions/527719/how-to-add-hyperlink-in-jlabel)'s one from this site. – Marko Topolnik Apr 17 '12 at 20:01
  • i have put the code and I have told what I want. Helppppppp – Aravind Chinta Apr 17 '12 at 20:17
  • Please use a consistent and logical indent for code blocks. – Andrew Thompson Apr 18 '12 at 05:05
  • Not the immediate problem, but don't use `JLabel`/`MouseListener`, instead use something like `JButton`/`ActionListener` or `JList`/`ListSelectionListener`. The latter two combos. respond to both mouse **and keyboard**. – Andrew Thompson Apr 18 '12 at 05:07

1 Answers1

2

If I do understand your question correctly, you want to allow the user to open a file. The Desktop class (available as of JDK1.6) allows this

File fileToOpen = ...;
Desktop desktop = Desktop.getDesktop();
desktop.open( fileToOpen )

Depending on how you want to present this to the user, you can opt for your JLabel code with the listener but it is probably easier to use a JButton with an ActionListener. Both approaches are discussed in detail in the answer Marko Topolnik already suggested in his comment. The only difference is that they wanted to open an URL, while you want to open a file (so that answer uses the browse method instead of the open method of the Desktop class).

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
  • +1 A [`HyperlinkListener` in a `JEditorPane`](http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JEditorPane.html) is another approach. – trashgod Apr 17 '12 at 22:36