Swing - fileScrollPane/fileList not updating

In the code below, the fileScrollPane /fileList is not being updated when the JFileChooser is done.
fileListPanel.setBorder is being updated, but not the list.
Can someone tell me why?

import javax.swing.*;

import java.awt.*;
import java.io.*;

/*
 * Class for Input Panel
 *
 */

public class InputPanel extends JPanel implements constants.Constants
{
    private ButtonEventHandler buttonEventHandler;

    private JPanel fileListPanel; // panel for file list

    //  fileList will be updated with JFileChooser (from browse button)
    private JList fileList;
    private JScrollPane fileScrollPane;
   
    /*
     * Constructor
     */
    public InputPanel()
    {
        buttonEventHandler = new ButtonEventHandler( this );

        /*
         * An easy way to put space between a top-level container and its
         * contents is to put the contents in a JPanel that has an "empty"
         * border.
         */
        setBorder( BorderFactory.createEmptyBorder( 10, // top
                                                    10, // left
                                                    10, // bottom
                                                    10 ) ); // right

        fileListPanel = new JPanel();

        // layout for x panel
        GridLayout gridLayout = new GridLayout( 2, // rows
                                                1, // columns
                                                0, // horizontal gap
                                                0 ); // vertical gap
        fileListPanel.setLayout( gridLayout );
        String currentDirectory = new String( System.getProperty( "user.dir" ) );
        fileListPanel.setBorder( BorderFactory.createTitledBorder( currentDirectory ) );

        File dir = new File( currentDirectory );
        // filter that returns files (not directories)
        FileFilter fileFilter = new FileFilter()
        {
            public boolean accept( File file )
            {
                return file.isFile();
            }
        };
        File[] files = dir.listFiles( fileFilter );

        fileList = new JList( files );

        fileScrollPane = new JScrollPane(fileList);
       
        fileListPanel.add( fileScrollPane );
        add( fileListPanel );

        JButton buttonBrowse = new JButton( "Browse" );
        // buttonBrowse.setFont(new Font("Sanserif", Font.BOLD, 12));
        buttonBrowse.setActionCommand( EVENT_BROWSE );
        buttonBrowse.addActionListener( buttonEventHandler );
        add( buttonBrowse );

    } // end ()

   
    /*
     * called when browse button it clicked
     *
     * get new file list
     */
    public void getNewFilelist()
    {
        //System.out.println( "InputPanel.getNewFileList" );
        JFileChooser fc = new JFileChooser();
        fc.setMultiSelectionEnabled( true );

        // fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog( this );

        if ( returnVal == JFileChooser.APPROVE_OPTION ) {
            File[] files = fc.getSelectedFiles();

            // update title of panel (current directory)
            String currentDirectory = fc.getCurrentDirectory().toString();
            fileListPanel.setBorder( BorderFactory.createTitledBorder( currentDirectory ) );

            // update fileList
            fileList = new JList( files );
            fileScrollPane = new JScrollPane(fileList);
            fileListPanel.repaint(); //?
            fileScrollPane.repaint(); //?
            this.repaint();
        }
        else {
            // Open command cancelled by user
        }

    }

} // end class
omomAsked:
Who is Participating?
 
CEHJCommented:
You don't need to tear down parts of the gui to fill your JList. Just set its model

Not

>>
fileList = new JList( files );
etc.
>>

but

addFiles(files, filesList);

private void addFiles(File[] files, JList fileList) {
      DefaultListModel dlm = new DefaultListModel();
      for (File f : files) {
            dlm.addElement(f);
      }
      fileList.setModel(dlm);
}
0
 
asood314Commented:
instead of this

fileScrollPane = new JScrollPane(fileList);

do this

fileScrollPane.getViewport().setView(fileList);

that should fix the problem.
0
 
CEHJCommented:
:-)

You can optimize that further by starting with a DefaultListModel then do


private void addFiles(File[] files, JList fileList) {
      DefaultListModel dlm = (DefaultListModel)fileList.getModel();
      dlm.clear();
      for (File f : files) {
            dlm.addElement(f);
      }
      fileList.setModel(dlm);
}

    * Add to Knowledgebase
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.