Search code examples
javaswingfilepdfjtextarea

Clicking on java JTextArea or other methods


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.


Solution

  • 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());