Name: pa48320 Date: 06/06/2003
Steps to reproduce:
1. Compile DialogTest.java
javac DialogTest.java
2. Run after setting the classpath
java DialogTest
3. Press "Open a File" button to bring up the JFileChooser. Choose a directory which has a lot of sub directories and files
4. Press a letter on the keyboard which is not visible on the JFileChooser to select a directory. For eg: there is a directory by name "pass" which is not visible initially on the JFileChooser. Now if you press P, this directory gets selected but the JFileChooser does not scroll to show us the selection but if you scroll manually (by pressing the scroll bar) you will notice that the directory is selected.
DialogTest.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.util.StringTokenizer;
public class DialogTest extends JFrame {
static private final String newline = "\n";
public DialogTest() {
super("FileChooserDemo");
//Create the log first, because the action listeners
//need to refer to it.
//Create a file chooser
final JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
TextFilter txt = new TextFilter();
fc.setFileFilter(txt);
fc.addChoosableFileFilter(txt);
//Create the open button
JButton openButton = new JButton("Open a File...");
openButton.setMnemonic(openButton.getText().charAt(0));
openButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(DialogTest.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//this is where a real application would open the file.
System.out.println("Opening: " + file.getName() + "." + newl
ine);
//StringTokenizer st = new StringTokenizer(null, " ");
StringBuffer buff = new StringBuffer("");
String ii = buff.toString();
}
else {
System.out.println("Open command cancelled by user." + newli
ne);
}
}
});
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(openButton);
//Add the buttons and the log to the frame
Container contentPane = getContentPane();
contentPane.add(buttonPanel);
}
public static void main(String[] args) {
StringBuffer tm=new StringBuffer("\"");
tm.append("THis is new");
tm.append("\"");
System.out.println(tm);
JFrame frame = new DialogTest();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
class TextFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept(File f)
{
if ( f.isDirectory() || f.getName().endsWith("txt") || f.getName
().endsWith("TXT") )
return true;
else
return false;
}
public String getDescription()
{
return "Text Files";
}
}
}
(Review ID: 187195)
======================================================================