JFileChooser set focus to specific directory

Is it possible to set the JFileChooser to grab focus on the previous selected directory in directory only mode from the textfield containing the selected file name?
The behavior I am seeking is to show the chooser with a directory already selected and or highlighted. I have tried the set selected file method and that does not highlight the file as if the user selected the file. I keep getting various behaviors where I set the selected folder. In the file name text component it shows upper level directories at times instead of the directory I specified. The focus is typically located on the file name text component and I would rather have the focus on the directory entry specified in the file name text. Is that possible with JFileChooser?

Hello,
I've tried to work something out. I hope it doesn't seem overkill :import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowEvent;
import java.awt.*;
import java.io.File;
* Date: 9 mai 2008
* @author: Pierre Le Lannic
public class FileChooserTest {
     private File _dir;
     private JLabel _fileNameLabel;
     private JPanel _mainPanel;
     private JList _filePane;
     private JFileChooser _fileChooser;
     public FileChooserTest() {
          _mainPanel = new JPanel(new BorderLayout());
          _fileNameLabel = new JLabel();
          _fileNameLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
          _mainPanel.add(_fileNameLabel, BorderLayout.CENTER);
          _mainPanel.add(new JButton(new AbstractAction() {
               public void actionPerformed(ActionEvent e) {
                    prepareFileChooser();
                    _fileChooser.setSelectedFile(_dir);
                    int returnOption = _fileChooser.showSaveDialog(_mainPanel);
                    if (returnOption == JFileChooser.APPROVE_OPTION) {
                         setDir(_fileChooser.getSelectedFile());
          }), BorderLayout.EAST);
          setDir(new File(System.getProperty("user.home")));
     private void setDir(File aDirectory) {
          _dir = aDirectory;
          _fileNameLabel.setText(_dir == null ? " " : _dir.getAbsolutePath());
     private void prepareFileChooser() {
          if (_fileChooser == null) {
               _fileChooser = new JFileChooser() {
                    protected JDialog createDialog(Component parent) throws HeadlessException {
                         JDialog dialog = super.createDialog(parent);
                         dialog.addWindowFocusListener(new WindowFocusListener() {
                              public void windowGainedFocus(WindowEvent e) {
                                   requestFocusForList();
                              public void windowLostFocus(WindowEvent e) {}
                         return dialog;
               _fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
               _filePane = recursiveFindList(_fileChooser);
     private void requestFocusForList() {
          SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                    _filePane.requestFocus();
     private JList recursiveFindList(Container parent) {
          if (parent == null) return null;
          int n = parent.getComponentCount();
          for (int i = 0; i < n; i++) {
               Component child = parent.getComponent(i);
               if (child instanceof JList) return (JList)child;
               if (child instanceof Container) {
                    JList test = recursiveFindList((Container)child);
                    if (test != null) return test;
          return null;
     public void start() {
          SwingUtilities.invokeLater(new Runnable() {
               public void run() {
                    JFrame f = new JFrame();
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.setContentPane(_mainPanel);
                    f.setSize(400, 50);
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
     public static void main(String[] args) {
          new FileChooserTest().start();
}

Similar Messages

  • How to set focus on specific search field in FPM's search guibb?

    Is there any way to set the input focus to a specifc search field in an FPM's search guibb? I found that there is a godd way for forms by using the IO_EXTENDED_CTRL object in the GET_DATA function. But it seems that there is no corresponding way to do for search forms. Any other idea?
    Best regards,
    Thomas

    Hello Venkatesh. Yes that code does work. First I tried it on a table cell that was already rendered and it did work. The next time I tried it on a table row that was being added and it did not work there. So I added an on after rendering function for the table and added that code there. That did not work until I added a delay (timeout) to do a context switch before calling the focus and that worked.
    Once last thing though sometimes when I call focus on an input field (actually in a table row cell) if the field has text in it already the flashing cursor is at the beginning of the text and other times it is at the end of the text (which is the desired way). It depends on where I click in the row. Is there anyway to make sure the flashing cursor is at the end of the text when the focus is applied to a field that contains text?

  • Set focus to specific field when page is loaded

    Hi
    simple login page (jsp) with 2 fields. when the page is loaded, I want the focus directly in the first field, so that the user can immediately start typing his userid. How do I do this with Creator 2?
    thanks, Peter

    thanks, I was missing the proper search term ... :-)
    here I found the answer: http://swforum.sun.com/jive/thread.jspa?forumID=123&threadID=63057
    Peter

  • Save file without JFileChooser in specific directory

    hi!
    can you save a file without opening a JFileChooser dialogue to a specific directory??
    the problem I have got is not how to address to the directory but how to save the file....
    could someone help me please?
    thx
    charly

    If u only want to save the file instead of selecting from the filechooser then you should use
    JFileChooser fc = new JFileChooser();
    int retVal = fc.showSaveDialog(frame);
    if(retVal == JFileChooser.APPROVE_OPTION)
    String filename = fc.getSelectedFile().getName();
    getName() will give file name and getAbsolutePath() will give the full path.
    filename -> is the file name that u entered for saving in the field. I am not sure if this what u wanted. Hope this helps otherwise pls be more specific in ur question.
    Selvi.
    I was pondering the same question. if I just open the
    dialog, instead of choosing a file from a directory, I
    want to type in a new file name, and save it.
    help is appreciated.

  • Open files from a specific directory

    Dear all,
    I have the following source code that simply selects files from a directory.
    private void openFile()
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileSelectionMode(
    JFileChooser.FILES_ONLY );
    int result = fileChooser.showOpenDialog( this );
    // user clicked Cancel button on dialog
    if (result == JFileChooser.CANCEL_OPTION )
    return;
    File filename = fileChooser.getSelectedFile();
    if (filename == null || filename.getName().equals( "" ))
    JOptionPane.showMessageDialog( this,
    "Invalid File Name",
    "Invalid File Name", JOptionPane.ERROR_MESSAGE );
    else
    // open the file
    try
         // Open an input stream
         FileInputStream fin1 = new FileInputStream(filename);
    // Read and print a line of text
    BufferedReader d1 = new BufferedReader(new InputStreamReader(fin1));
    abs = d1.readLine();
    outputArea1.setText("");
    outputArea1.append(abs + "\n\n");
    outputArea1.setCaretPosition(0);
    // Close our input and output stream
    fin1.close();
    catch (IOException e5) {
    JOptionPane.showMessageDialog(this, "Error Opening File", "Error", JOptionPane.ERROR_MESSAGE);
    } // end catch
    } // end else
    } // end private
    My question is: Can I force it to open files from a specific directory, and NOT from MyDocuments directory????
    thanks,
    vxc

    \r = return
    \n = linefeed
    \j = illegal escape character
    JFileChooser fileChooser = new JFileChooser("C:\j2sdk1.4.2\bin");
    You could/should use:
    (new File).pathSeparator
    or
    (new File).pathSeparatorChar
    and
    (new File).separator
    or
    (new File).separatorChar
    http://java.sun.com/j2se/1.4.1/docs/api/java/io/File.html

  • Setting focus on a component from backing bean

    Is there an easy way to set focus in a specific component on the jspx from a method in the backing bean? (other than javaScript coding...)

    Hi,
    did you take a look at
    About control the cursor position in a JSF web page
    It contains some links to other weblogs. The links are helpfull. Maybe you'll be able to find your solution there......
    Good luck
    Luc Bors

  • How to set permissions IN Open Directory USING Open Directory groups?

    Hi all,
    Apologies if I've missed this but have been searching for two days trying to figure out how to delegate permissions within the OD to a number of different OD groups and i can't seem to find any way to do this either at the command line or with WGM.
    Examples: an OD group containing those who will manage the full directory need to have permissions on all containers, child objects, and their attributes in the directory. For this one in particular I seem to be able to nest a group in the default Admin group, but this isn't really what i'm after. I need to create OD groups with the ability only to manipulate objects of class apple-computer and similarly, apple-user (really all inetOrgPerson objects). In a nutshell: how do i set permissions on specific attributes or object classes using OD groups?
    thanks for any pointers...
    -andrew

    I think i just answered my own question: Open Directory is OpenLDAP. slapd is all i need.

  • How to set focus on a custom PO Item screen field when in error?

    Hi All,
    I have an interesting situation that i'm wondering if others have solved.  We have extended the PO item table (EKPO) by adding two new fields.  We then have implemented two BAdI's:  ME_GUI_PO_CUST and ME_PROCESS_PO_CUST to add them to the ME21N/ME22N/ME23N screens and logic to do some validation, via these respective BAdI's mentioned.  Everything works perfectly - with one small issue.  When we are doing some validation via a method on the ME_PROCESS_PO_CUST - and "invalidate" the field (and throw an error) when it is in error - I also want to be able to "set focus" on the field in question (basically: go to the particular tab on the ME* screen and highlight the field).  I have tried using SET CURSOR FIELD *****  within this BAdI (ME_PROCESS_PO_CUST) - but doesn't seem to work.  Has anyone tried to do this and have come up with a solution?  Would be much appreciative if you shared it!!!  Thanks much.
    Cheers,
    Matt
    ERP version that we have is:  ECC 6.0

    Just have a look at oss note 310154 - ME21N/ME51N: Customer-specific check, generating error log.
    In short:
    Add your error messages in EXIT_SAPMM06E_012 (using specific macros).
    Sample code (provided in Oss note) :
      loop at tekpo where knttp eq 'X'.
        loop at tekkn where ebeln eq tekpo-ebeln and
                            ebelp eq tekpo-ebelp and
                            kostl eq space.
          if not tekkn-id is initial.
            mmpur_business_obj_id tekkn-id.
            mmpur_metafield MMMFD_ACCOUNTINGS.
          endif.
          mmpur_message_forced 'E' 'ZE' '777' '' '' '' ''.
        endloop.
      endloop.

  • Set Focus target when Add New Instance

    [LC Designer ES2 9.0] Using Action Builder to add a new instance of subform SITE, where first field is called SiteName.
    If I set action to Set Focus to SiteName, the focus goes to the *original* SiteName field instead of to the new instance.
    Users will typically add a number of additional sites and, each time, I want the focus to be set on the first field of the newest instance, not the original instance. How do I accomplish that? Thanks!           ~Carol

    I understand the concept (I think...), but am not clear on where to place the code.
    I used Action Builder to create an AddSite action: when check box chkAddSite is checked | Add a new instance of subformSiteInfo | and set visibility of subformSiteInfo to visible. Does your code go in that same action sequence? If so, what is the next Action Builder result selection? If not, where do I insert it?
    I never know whether the new instance of the subform will begin on the following page, so the user might keep clicking the chkAddSite box, thinking it didn't work. Although, thinking this through, maybe I could force the new subform instance to always appear at the top of the following page, and somehow direct the focus there, rather than to the specific first field???
    Thanks!         ~Carol

  • How do I set up a specific homepage for Safari on my iPhone?

    How do I set up a specific page to be the page that opens every time I open Safari on my iPhone?  I saw that you go to Preferences, but I can't find where that is?  What are the steps from there?  Thanks.

    It actually is the answer to the question. Safari on the iPhone is not designed to allow for the setting of a home page. The only method to do it is what the last posted told you. That is how iOS handles the setting of a page, other than a bookmark in Safari. If you have a particular page you want to always open to in Safari, use the method described above which sets a shortcut on the home screen that you use instead of the regular Safari icon to start Safari.
    It could be considered a workaround, depending on how you view it.

  • How do I set up a specific playlist for songs so only those sync on A-TV??

    I have a huge number of songs on my Itunes library and I don't want the Apple TV (A-TV) to use so much space for all those songs - I prefer to save it for my video and movies. How can I set up a specific play list on Itunes for A-TV and then when I sync my A-TV - only that playlist goes over.
    Hopefully someone has an idea for me. Hope everyone had a great holiday.
    Skip

    Dear Winston,
    Thanks so much. It seems much easier after reading it. I did not have those tabs originally which was confusing... Once I clicked the correct preferences, the tabs came up. Now I am resyncing everything which will take time but I see it working.
    Thanks for your help Mr. Churchill....
    Skip

  • Error in setting permissions on file/directory $ORACLE_BASE/jre/1.1.8/LICEN

    Hi,
    On installing Oracle 9.2 on RedHat Linux release 4, I encounterd the
    following error:
    Error in setting permissions on file/directory
    $ORACLE_BASE/jre/1.1.8/LICENSE
    This error is encountered at the inital stage of installation after inputing all the
    required settings in the OUI. and at the Installing Java Runtinme Environment 1.1.8.1.0
    Could someone tell me why it happened and how to fix it? Your help is
    much appreciated.
    There was a post on this topic only in here and said to change the permissions of the
    ORACLE_BASE .......... I have tried out all these and all the permissions and the environment settings are correct but i'm not able to proceed with the installation even after a lot of trying.
    All you guys help is verry verry appreciated..........
    Thanks,

    Do the following and post the results:
    cd $ORACLE_BASE
    pwd
    ls -al
    cd jre
    pwd
    ls -al
    cd 1.18
    pwd
    ls -alI can't imagine anyone making recommendations without seeing what you've done.

  • Error in setting permissions of file/directory

    This is the error I am getting
    Error in setting permissions of file/directory /u01/app/oracle/jre/1.1.8/bin/i686/native_threads/.extract_args
    I am running RedHat 9 and obviously installing 9.2.0.
    Any ideas on why I might be getting this. I have set permissions for the oracle user for this directory.
    Thanks
    Dave

    from root try this command
    umask
    umask must set at 0022 ... if your server not set at this number use this command
    umask 0022

  • Error in setting permissions of file/directory /home/oracle/jre/1.1.8/LICENSE !

    Error in setting permissions of file/directory /home/oracle/jre/1.1.8/LICENSE ! Installation problem of 9i databse on redhat 7.3!!I installed sun jdk 1.3 or jdk 1.1.8_v3 and created a link for /usr/local/java !

    from root try this command
    umask
    umask must set at 0022 ... if your server not set at this number use this command
    umask 0022

  • I re-set password using specific user ID but when I put new password in it does not say incorrect password  it just spins and spins never accepting it.  What is the problem?

    I re-set password using specific user ID but when I put new password in it does not say incorrect password  it just spins and spins never accepting it.  What is the problem?

    Update on my problem connecting in Itunes with my Airport Express. Not sure if anything I tried made a difference
    but I finally got the Pop up window saying this device requires a Password. I entered the password and I can once again use the Airport Express as a remote speaker.

Maybe you are looking for