United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4390885 : Ability to set the location of JFileChooser

Details
Type:
Bug
Submit Date:
2000-11-20
Status:
Resolved
Updated Date:
2001-03-14
Project Name:
JDK
Resolved Date:
2001-03-14
Component:
client-libs
OS:
windows_nt
Sub-Component:
javax.swing
CPU:
x86
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.3.0
Fixed Versions:
1.4.0 (beta)

Related Reports

Sub Tasks

Description

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) 
======================================================================

                                    

Comments
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
merlin-beta

FIXED IN:
merlin-beta

INTEGRATED IN:
merlin-beta


                                     
2004-06-14
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);
    }
}

                                     
2004-06-11
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
                                     
2001-03-05



Hardware and Software, Engineered to Work Together