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.createEmptyB
order( 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.createTitled
Border( 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.setActionComm
and( EVENT_BROWSE );
buttonBrowse.addActionList
ener( 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.setMultiSelectionEnable
d( true );
// fc.setFileSelectionMode(JF
ileChooser
.DIRECTORI
ES_ONLY);
int returnVal = fc.showOpenDialog( this );
if ( returnVal == JFileChooser.APPROVE_OPTIO
N ) {
File[] files = fc.getSelectedFiles();
// update title of panel (current directory)
String currentDirectory = fc.getCurrentDirectory().t
oString();
fileListPanel.setBorder( BorderFactory.createTitled
Border( 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