troubleshooting Question

Swing - fileScrollPane/fileList not updating

Avatar of omom
omom asked on
JavaEditors IDEs
3 Comments1 Solution835 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros