John Smith
John Smith

Reputation: 719

How to display the text within files when I click on them in my JList?

I am writing a Java GUI program to fit the following spec: Write a Java GUI application which allows the user to select a file from the local file system. Display the contents of the selected file in a JTextArea with scrollbars. It is sufficient to support display of text files only.

I am now trying to implement the feature that will display the contents of the selected file in the JTextArea but I do not know how to do this. I assume that I will add some kind of event listener to the JList and then call setText on JTextArea but do not know how to go about this. Any other suggestions to improve my program to meet the spec would be great. Here is my GUI class:

import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileSystemView;

public class FileGUI {

    @SuppressWarnings("unchecked")
    public FileGUI() {

        JFrame window = new JFrame("Local File List");

        Container pane = window.getContentPane();
        pane.setLayout(new GridLayout(2,1));

        @SuppressWarnings("rawtypes")
        JList fileList = new JList(new File("C:\\").listFiles());
        fileList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        fileList.setCellRenderer(new MyCellRenderer());
        fileList.setLayoutOrientation(javax.swing.JList.HORIZONTAL_WRAP);
        fileList.setName("fileList");
        fileList.setVisibleRowCount(-1);
        pane.add(new JScrollPane(fileList));

        JTextArea jt = new JTextArea(20, 50);
        jt.setEditable(false);
        JScrollPane js = new JScrollPane(jt);
        pane.add(js);

        window.pack();
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                @SuppressWarnings("unused")
                FileGUI FG = new FileGUI();

            }

        });
    }

    private static class MyCellRenderer extends DefaultListCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getListCellRendererComponent(@SuppressWarnings("rawtypes") JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

            if (value instanceof File) {

                File file = (File) value;
                setText(file.getName());
                setIcon(FileSystemView.getFileSystemView().getSystemIcon(file));

                if (isSelected) {
                    setBackground(list.getSelectionBackground());
                    setForeground(list.getSelectionForeground());
                } else {
                    setBackground(list.getBackground());
                    setForeground(list.getForeground());
                }
                setEnabled(list.isEnabled());
                setFont(list.getFont());
                setOpaque(true);
            }       

            return this;
        }

    }

}

Thanks for your time.

Upvotes: 1

Views: 286

Answers (1)

Petro
Petro

Reputation: 3662

A simple way would be inside your onclick listener, read the file then update your view.

    list_item.addListSelectionListener(new ListSelectionListener() {

    @Override
    public void valueChanged(ListSelectionEvent arg0) {
        try{
                 BufferedReader br = new BufferedReader(new FileReader("C:\\myfile.txt"));
                 String line;  
                 while ((line = br.readLine())!= null) 
                  {  
                  // read file line by line here  
                  your_text_area.append(line+"\n");

                 } 
                 }catch(Exception e){
                     e.printStackTrace();
                 }
    }
});

Additional Source:

Upvotes: 2

Related Questions