JDK-4390885 : Ability to set the location of JFileChooser
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2000-11-20
  • Updated: 2001-03-14
  • Resolved: 2001-03-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
1.4.0 betaFixed

Name: krC82822			Date: 11/20/2000

java version 1.2.x and 1.3.x

Please add the ability to set the location of a JFileChooser. This might be a
bug, but JFileChooser.setLocation() has no effect when
JFileChooser.showXXXXDialog() is called. This is becoming very important as the
popularity of multiple monitors is increasing. These dialogs show up on the
wrong monitor which is really anoying.   Also due to bug #4189244 I am unable to
avoid painting problems by positioning the dialog away from the menu item that
initiated it.

A JFileChooser.showXXXXDialog(Component parent, Dimension location) would be
adiquate if setLocation() is not appropriate.
(Review ID: 112175) 

CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: merlin-beta FIXED IN: merlin-beta INTEGRATED IN: merlin-beta

WORK AROUND Subclass JFileChooser and override the method showDialog(), adding a call to setLocation(): ------------------- import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SpecialFileChooser extends JFileChooser { private JDialog dialog; private int returnValue = ERROR_OPTION; public int showDialog(Component parent, String approveButtonText) { if (approveButtonText != null) { setApproveButtonText(approveButtonText); setDialogType(CUSTOM_DIALOG); } Frame frame = parent instanceof Frame ? (Frame) parent : (Frame)SwingUtilities.getAncestorOfClass(Frame.class, parent); String title = getUI().getDialogTitle(this); dialog = new JDialog(frame, title, true); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { returnValue = CANCEL_OPTION; } }); dialog.getContentPane().add(this, BorderLayout.CENTER); dialog.pack(); dialog.setLocationRelativeTo(parent); rescanCurrentDirectory(); returnValue = ERROR_OPTION; dialog.show(); return returnValue; } public void approveSelection() { returnValue = APPROVE_OPTION; if (dialog != null) { dialog.setVisible(false); } super.approveSelection(); } public void cancelSelection() { returnValue = CANCEL_OPTION; if (dialog != null) { dialog.setVisible(false); } super.cancelSelection(); } public static void main(String[] s) throws Exception { JFrame frame = new JFrame("FileChooser test"); frame.setVisible(true); JFileChooser chooser = new SpecialFileChooser(); int returnVal = chooser.showOpenDialog(frame); switch (returnVal) { case APPROVE_OPTION: System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); break; case CANCEL_OPTION: System.out.println("Cancelled"); break; case ERROR_OPTION: System.out.println("An error occurred"); break; } System.exit(0); } }

EVALUATION Need to add a protected method in JFileChooser called createDialog(). Overriding this method will allow setting location, etc. Example: class MyFileChooser extends JFileChooser { protected JDialog createDialog(Component parent) throws HeadlessException { JDialog dialog = super.createDialog(parent); dialog.setLocation(300, 200); return dialog; } } leif.samuelsson@Eng 2001-03-05