-1

I am currently working on Windows, and writing a simple Java application.

The application does a simple search for certain files, and I created a JPanel and a JTextArea and I add the text area to the panel, and I output the results, the file paths, on the text area using append.

So far so good.

Now, I wonder, if I wanted to be able to click on those paths (as if they were links) to open those files (they are either Word or PDF files), how do I do that?

I imagine this is probably going to require more than a couple of lines to code, but if anyone could point me out in the right direction I would appreciate it.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user
  • 2,015
  • 6
  • 22
  • 39

2 Answers2

3

I output the results, the file paths, on the JTextArea

See How to use lists. A JList would be a much more natural component for this. E.G. as seen in this answer.

See also FileBro which uses a JTree for the file listing and the Desktop class as mentioned by @MadProgrammer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Take a look at How to Integrate with the Desktop Class

Basically you want to use something like...

File file = new File(...);
Desktop desktop = Desktop.getDesktop();
desktop.edit(file);

or

desktop.open(file);

depending on if you want to edit or view the file (sometimes they are the same thing)

Take a look at the JavaDocs for java.awt.Desktop for more details

Updated with file opening example

Based on feedback, I would recommend using a JList of JTextArea to list the matching the Files, this gives you more control over determining what the user has actually selected and designed to, well, list stuff

This example requires the user to perform a double click to open the file...

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class FileListExample {

    public static void main(String[] args) {
        new FileListExample();
    }

    public FileListExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                File[] files = new File("...").listFiles();
                DefaultListModel<File> model = new DefaultListModel<>();
                for (File file : files) {
                    model.addElement(file);
                }
                JList<File> list = new JList<>(model);
                list.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) {
                            JList list = (JList) e.getComponent();
                            File file = (File) list.getSelectedValue();
                            try {
                                Desktop desktop = Desktop.getDesktop();
                                desktop.open(file);
                            } catch (IOException exp) {
                                exp.printStackTrace();
                            }
                        }
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(list));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

If you wanted to do something a little more fancy, you could even supply your own ListCellRenderer, for example...

FileList

public class FileListCellRenderer    extends DefaultListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

        Icon icon = null;
        if (value instanceof File) {
            File file = (File) value;
            value = file.getName();
            FileSystemView view = FileSystemView.getFileSystemView();
            icon = view.getSystemIcon(file);
        }

        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 

        setIcon(icon);

        return this;

    }

}

Which can be applied using...

list.setCellRenderer(new FileListCellRenderer());
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for taking the time to reply. I know how to open a file, my question is how do I make the text clickable? I want to somehow allow the user to click on a list of files and open that file. I can open the file, once I know which file the user has chosen, but I do not know how to create a jTextArea, or something else, where the user can actually choose a file. – user May 23 '14 at 00:49
  • 1
    @user79303 Really, you need to re-word you question. How is the file name displayed? Without context is very difficult to provide you with suggestions. Try providing an actual [runnable example that demonstrates your problem](https://stackoverflow.com/help/mcve). It will reduce the guess work and provide better responses – MadProgrammer May 23 '14 at 00:52
  • @MadProgrammer I suspected (but did not know for sure) that 'selecting the file' was part of the problem. This question could have been better specified. – Andrew Thompson May 23 '14 at 01:49
  • @AndrewThompson Given that people keep up-voting both answers would attest to that... – MadProgrammer May 23 '14 at 01:50
  • @MadProgrammer, I thought the question was clear, sorry if it was not. The problem is simple: I am doing a query on a word, and I get back a list of files, ranked by how many times the word appear. I get the list back, no problem. Now I want to allow the user to select a file in that list, and treat it as if it were a link on a web page, so that clicking on it would open it. – user May 23 '14 at 21:06
  • @AndrewThompson: I like your suggestion, and I will look into it. I know that JTextArea was not good, it just worked for the first part, where i was displaying the files. For that it worked. I can look into your suggestion as a better implementation. Will that allow me to select the file and open it? Thank you. – user May 23 '14 at 21:09
  • @user79303 I would suggest that you output the file list into a JList and allow the user to either double click or through some other interaction open the file. I'll see if I can do an example soon – MadProgrammer May 23 '14 at 21:19
  • @MadProgrammer: Thank you very much, I appreciate your help and quick reply, and sorry again for the confusion. – user May 23 '14 at 21:24
  • It worked liked a charm, and it was easier to implement than I thought it would be. Thank you again. – user May 25 '14 at 01:48
  • @user79303 Much easier then trying to make a `JTextArea` deal with it :P – MadProgrammer May 25 '14 at 03:31