Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JFileChooser doesn't have focus first time it opens

Status
Not open for further replies.

brokenbone

Programmer
Oct 19, 2005
17
0
0
SI
Hi!

I'm using JFileChooser to export some data from my web application to a file. Everything is working fine, the only problem I have is when I run the application and try to export data the choose file dialog opens behind the browser window. This is quite unpleasant since the user thinks the dialog is not opened at all. This only happens the first time when the button to export the data is clicked after the application is run. Every next time the dialog gets the focus and is visible in front of the browser.

How can I set the focus to dialog the first time it opens?

My code looks like this:

Code:
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        loJFC = new JFileChooser();
 
        loJFC.setDialogTitle("Export data");
        
        loJFC.setFileFilter(new TextFilter()); // my class that accepts only txt files
        loJFC.setAcceptAllFileFilterUsed(false);
 
        loJFC.setSelectedFile(new File("MyFileName.txt")); // set the default name
        
        int liRetVal = loJFC.showSaveDialog(null);
 
        if (liRetVal == JFileChooser.APPROVE_OPTION)
        {
          loFile = loJFC.getSelectedFile();
        }
        else
        {
          return;
        }
 
// then I write to file etc...


Thanks in advance.
BB
 
I copy code from a web site:
Code:
    JFileChooser chooser = new JFileChooser() {  
      protected JDialog createDialog(Component parent) throws HeadlessException {  
        JDialog dialog = super.createDialog(parent);  
        // ...  
        Point p = calculateCenter(dialog);  
        dialog.setLocation(p.x, p.y);  
        return dialog;  
      }  
    };  
   int returnVal = chooser.showOpenDialog(this);
Try to pass your applet as parent.
 
I think your problem comes from this line :
Code:
int liRetVal = loJFC.showSaveDialog(null);
The Component typed parameter given to the showSaveDialog method is only given for that :
- determining above which GUI the save dialog should open,
- blocking parent thread to achieve modal behavior.

Just replace the null given by a reference to main JFrame of your GUI should solve that.

Water is not bad as long as it remains outside human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top