-
Bug
-
Resolution: Fixed
-
P4
-
1.2.1, 1.3.0
-
beta
-
x86, sparc
-
solaris_2.6, solaris_7, windows_nt
-
Verified
1. Instantiate a JFileChooser pointing to a directory with a large number of files
2. setSelectedFile() to a file that is alphabetically towards the end of the list of files in this directory
3. call showOpenDialog(). Although the file is selected, the JList of files within the file chooser does not scroll down to the selected file.
Does not happen with the save dialog.
raj.premkumar@eng 1999-03-31
Name: ks88420 Date: 09/01/2000
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Under JDK 1.3.0, the showOpenDialog() and showSaveDialog() Pop-ups (called in
my sample program by the 'File' menu items "Load Text File..." and "Save
as...") insert selected directories as 'File name' when the user traverses
directories.
When you run the program under JDK 1.2.2-06, both dialogs leave the 'File name'
unchanged when a directory is chosen (they only populating the field when a
file is chosen). I think that this is the correct behavior (Swing should not
reset the file name upon a change of the directory, and should not use a
directory name as a file name!)
Application to demonstrate the problem:
// Developed from 'Formatter.java' which has a Copyright Notice:
// Copyright MageLang Institute; Version $Id: //depot/main....
// I REMOVED ALL MENUS EXCEPT FOR 'FILE' MENU
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.util.*;
public class SaveAs extends JFrame {
final static int WIDTH = 400;
final static int HEIGHT = 300;
StyledDocument doc;
JTextPane pane;
JLabel statusInfo;
public SaveAs(String lab) {
super (lab);
// Get ContentPane
Container c = getContentPane();
// Setup Status Message Area
statusInfo = new JLabel();
c.add (statusInfo, BorderLayout.SOUTH);
// Setup Text Pane
doc = new DefaultStyledDocument();
pane = new JTextPane (doc);
// Place in JScrollPane
JScrollPane sp = new JScrollPane (pane);
c.add(sp, BorderLayout.CENTER);
// Setup Menus
JMenuBar menuBar = new JMenuBar();
setJMenuBar (menuBar);
// Setup File Menu
JMenu file = new JMenu ("File");
JMenuItem item;
file.add (item = new JMenuItem ("New"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doNewCommand();
}
});
file.add (item = new JMenuItem ("Open"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doOpenCommand();
}
});
file.add (item = new JMenuItem ("Load Text File..."));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doLoadCommand();
}
});
file.add (item = new JMenuItem ("Save"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doSaveCommand();
}
});
file.add (item = new JMenuItem ("Save as..."));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doSaveAsCommand();
}
});
file.addSeparator();
file.add (item = new JMenuItem ("Close"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doCloseCommand();
}
});
menuBar.add (file);
// REMOVED ALL OTHER MENUS AND (Color, Font, and Insert)
}
public static void main (String args[]) {
SaveAs frame = new SaveAs("Mini Text Editor, with Save As...");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public void doSaveAsCommand() {
String msg;
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setDialogTitle("Save as...");
int status = chooser.showSaveDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
char data[];
final Runnable doWaitCursor = new Runnable() {
public void run() {
setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doWaitCursor);
} catch (Exception e) {
e.printStackTrace();
}
}
};
appThread.start();
try {
String AbsolutePathName = chooser.getSelectedFile().getAbsolutePath();
FileOutputStream fos = new FileOutputStream (AbsolutePathName);
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (doc);
oos.flush();
oos.close();
statusInfo.setText ("Saved to disk");
} catch (IOException e) {
statusInfo.setText ("Unable to save");
e.printStackTrace();
}
}
}
public void doNewCommand() {
pane.setStyledDocument (doc = new DefaultStyledDocument());
}
public void doCloseCommand() {
System.exit (0);
}
public void doOpenCommand() {
try {
FileInputStream fis = new FileInputStream ("doc.ser");
ObjectInputStream ois = new ObjectInputStream (fis);
doc = (StyledDocument)ois.readObject();
ois.close();
pane.setStyledDocument (doc);
validate();
statusInfo.setText ("Reloaded from disk");
} catch (Exception e) {
statusInfo.setText ("Unable to reload");
e.printStackTrace();
}
}
public void doSaveCommand() {
try {
FileOutputStream fos = new FileOutputStream ("doc.ser");
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (doc);
oos.flush();
oos.close();
statusInfo.setText ("Saved to disk");
} catch (IOException e) {
statusInfo.setText ("Unable to save");
e.printStackTrace();
}
}
public void doLoadCommand() {
String msg;
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
char data[];
final Runnable doWaitCursor = new Runnable() {
public void run() {
setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doWaitCursor);
} catch (Exception e) {
e.printStackTrace();
}
}
};
appThread.start();
File f = chooser.getSelectedFile();
try {
// Clear out current document
pane.setStyledDocument (doc = new DefaultStyledDocument());
// Read in text file
FileReader fin = new FileReader (f);
BufferedReader br = new BufferedReader (fin);
char buffer[] = new char[4096];
int len;
while ((len = br.read (buffer, 0, buffer.length)) != -1) {
// Insert into pane
doc.insertString(doc.getLength(),
new String (buffer, 0, len), null);
}
statusInfo.setText ("Loaded: " + f.getName());
} catch (BadLocationException exc) {
statusInfo.setText ("Error loading: " + f.getName());
} catch (FileNotFoundException exc) {
statusInfo.setText ("File Not Found: " + f.getName());
} catch (IOException exc) {
statusInfo.setText ("IOException: " + f.getName());
}
final Runnable undoWaitCursor = new Runnable() {
public void run() {
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
};
appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(undoWaitCursor);
} catch (Exception e) {
e.printStackTrace();
}
}
};
appThread.start();
}
}
}
(Review ID: 108279)
======================================================================
2. setSelectedFile() to a file that is alphabetically towards the end of the list of files in this directory
3. call showOpenDialog(). Although the file is selected, the JList of files within the file chooser does not scroll down to the selected file.
Does not happen with the save dialog.
raj.premkumar@eng 1999-03-31
Name: ks88420 Date: 09/01/2000
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
Under JDK 1.3.0, the showOpenDialog() and showSaveDialog() Pop-ups (called in
my sample program by the 'File' menu items "Load Text File..." and "Save
as...") insert selected directories as 'File name' when the user traverses
directories.
When you run the program under JDK 1.2.2-06, both dialogs leave the 'File name'
unchanged when a directory is chosen (they only populating the field when a
file is chosen). I think that this is the correct behavior (Swing should not
reset the file name upon a change of the directory, and should not use a
directory name as a file name!)
Application to demonstrate the problem:
// Developed from 'Formatter.java' which has a Copyright Notice:
// Copyright MageLang Institute; Version $Id: //depot/main....
// I REMOVED ALL MENUS EXCEPT FOR 'FILE' MENU
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.util.*;
public class SaveAs extends JFrame {
final static int WIDTH = 400;
final static int HEIGHT = 300;
StyledDocument doc;
JTextPane pane;
JLabel statusInfo;
public SaveAs(String lab) {
super (lab);
// Get ContentPane
Container c = getContentPane();
// Setup Status Message Area
statusInfo = new JLabel();
c.add (statusInfo, BorderLayout.SOUTH);
// Setup Text Pane
doc = new DefaultStyledDocument();
pane = new JTextPane (doc);
// Place in JScrollPane
JScrollPane sp = new JScrollPane (pane);
c.add(sp, BorderLayout.CENTER);
// Setup Menus
JMenuBar menuBar = new JMenuBar();
setJMenuBar (menuBar);
// Setup File Menu
JMenu file = new JMenu ("File");
JMenuItem item;
file.add (item = new JMenuItem ("New"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doNewCommand();
}
});
file.add (item = new JMenuItem ("Open"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doOpenCommand();
}
});
file.add (item = new JMenuItem ("Load Text File..."));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doLoadCommand();
}
});
file.add (item = new JMenuItem ("Save"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doSaveCommand();
}
});
file.add (item = new JMenuItem ("Save as..."));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doSaveAsCommand();
}
});
file.addSeparator();
file.add (item = new JMenuItem ("Close"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doCloseCommand();
}
});
menuBar.add (file);
// REMOVED ALL OTHER MENUS AND (Color, Font, and Insert)
}
public static void main (String args[]) {
SaveAs frame = new SaveAs("Mini Text Editor, with Save As...");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public void doSaveAsCommand() {
String msg;
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setDialogTitle("Save as...");
int status = chooser.showSaveDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
char data[];
final Runnable doWaitCursor = new Runnable() {
public void run() {
setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doWaitCursor);
} catch (Exception e) {
e.printStackTrace();
}
}
};
appThread.start();
try {
String AbsolutePathName = chooser.getSelectedFile().getAbsolutePath();
FileOutputStream fos = new FileOutputStream (AbsolutePathName);
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (doc);
oos.flush();
oos.close();
statusInfo.setText ("Saved to disk");
} catch (IOException e) {
statusInfo.setText ("Unable to save");
e.printStackTrace();
}
}
}
public void doNewCommand() {
pane.setStyledDocument (doc = new DefaultStyledDocument());
}
public void doCloseCommand() {
System.exit (0);
}
public void doOpenCommand() {
try {
FileInputStream fis = new FileInputStream ("doc.ser");
ObjectInputStream ois = new ObjectInputStream (fis);
doc = (StyledDocument)ois.readObject();
ois.close();
pane.setStyledDocument (doc);
validate();
statusInfo.setText ("Reloaded from disk");
} catch (Exception e) {
statusInfo.setText ("Unable to reload");
e.printStackTrace();
}
}
public void doSaveCommand() {
try {
FileOutputStream fos = new FileOutputStream ("doc.ser");
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (doc);
oos.flush();
oos.close();
statusInfo.setText ("Saved to disk");
} catch (IOException e) {
statusInfo.setText ("Unable to save");
e.printStackTrace();
}
}
public void doLoadCommand() {
String msg;
JFileChooser chooser = new JFileChooser();
int status = chooser.showOpenDialog(this);
if (status == JFileChooser.APPROVE_OPTION) {
char data[];
final Runnable doWaitCursor = new Runnable() {
public void run() {
setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
}
};
Thread appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(doWaitCursor);
} catch (Exception e) {
e.printStackTrace();
}
}
};
appThread.start();
File f = chooser.getSelectedFile();
try {
// Clear out current document
pane.setStyledDocument (doc = new DefaultStyledDocument());
// Read in text file
FileReader fin = new FileReader (f);
BufferedReader br = new BufferedReader (fin);
char buffer[] = new char[4096];
int len;
while ((len = br.read (buffer, 0, buffer.length)) != -1) {
// Insert into pane
doc.insertString(doc.getLength(),
new String (buffer, 0, len), null);
}
statusInfo.setText ("Loaded: " + f.getName());
} catch (BadLocationException exc) {
statusInfo.setText ("Error loading: " + f.getName());
} catch (FileNotFoundException exc) {
statusInfo.setText ("File Not Found: " + f.getName());
} catch (IOException exc) {
statusInfo.setText ("IOException: " + f.getName());
}
final Runnable undoWaitCursor = new Runnable() {
public void run() {
setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
};
appThread = new Thread() {
public void run() {
try {
SwingUtilities.invokeAndWait(undoWaitCursor);
} catch (Exception e) {
e.printStackTrace();
}
}
};
appThread.start();
}
}
}
(Review ID: 108279)
======================================================================