Results 1 to 10 of 10
  1. #1
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default Automatically set the path

    I am creating a GUI using Netbeans 6.7.1 and this GUI contains a TextFields, JButtons.
    all i want to do is.. when i put a drive in TextField for example i put drive "c:\" in the TextField and when i click the JButton is automatically the path is in the drive "c:\" also.

    is it possible?

    Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    Oh..! sorry for not clearing my Explanation...
    my gui contains TextFields and JButtons right? and the plow of my program is to browse files
    and to browse this i use JFileChooser and i implement this with my JButtons... i want to know is? is it possible when i put "C:\" in my TextField and then, when i click my JButton the JFileChooser is set into Drive "C:\" also because i put in "C:\" in my TextField..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yes, it's possible. First of all, you are using JTextFields and not TextFields, right?

    Next of all, look up the Sun tutorial on JFileChooser and it will show you how to take use a directory to initialize the file chooser: How to use File Choosers

    Much luck!

  5. #5
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    Here's my code and i didn't got an error on this but the problem is i didn't make it my question on this forum about the JTexField... it still not working :-(
    although they can browse files but when i put the path in the JTextField the path is not set to specific path which i put into JTextField,

    Java Code:
    private void BrowseS1ActionPerformed(java.awt.event.ActionEvent evt) {
            JFileChooser JFileChooseS1 = new JFileChooser();
            JFileChooseS1.setMultiSelectionEnabled(true);
            JFileChooseS1.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = JFileChooseS1.showDialog(this,"Open/Save");
            if(result == JFileChooser.APPROVE_OPTION){
                File S1 = JFileChooseS1.getSelectedFile();
                TxtFldS1.setText(S1.toString());
            }
        }
    
        private void BrowseS2ActionPerformed(java.awt.event.ActionEvent evt) {
            JFileChooser JFileChooseS2 = new JFileChooser();
            JFileChooseS2.setMultiSelectionEnabled(true);
            JFileChooseS2.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = JFileChooseS2.showDialog(this,"Open/Save");
            if(result == JFileChooser.APPROVE_OPTION){
                File S2 = JFileChooseS2.getSelectedFile();
                TxtFldS2.setText(S2.toString());
            }
        }
    Anyone help!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Did you read the tutorial that I linked to above? As I mentioned in my post above, it tells you how to set the directory. To quote:
    By default, a file chooser that has not been shown before displays all files in the user's home directory. You can specify the file chooser's initial directory by using one of JFileChooser's other constructors, or you can set the directory with the setCurrentDirectory method.

  7. #7
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    yes i already read it. and i found another example
    here's the code:
    Java Code:
    private void BrowseS1ActionPerformed(java.awt.event.ActionEvent evt) {
            JFileChooser JFileChooseS1 = new JFileChooser(new File("C:\\"));
            JFileChooseS1.setMultiSelectionEnabled(true);
            JFileChooseS1.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = JFileChooseS1.showDialog(this,"Open/Save");
            if(result == JFileChooser.APPROVE_OPTION){
                File S1 = JFileChooseS1.getSelectedFile();
                TxtFldS1.setText(S1.toString());
            }
        }
    but the same problem is on the JTextField i didn't set the specific path.:-(

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mlibot View Post
    yes i already read it. and i found another example
    here's the code:
    Java Code:
    private void BrowseS1ActionPerformed(java.awt.event.ActionEvent evt) {
            JFileChooser JFileChooseS1 = new JFileChooser(new File("C:\\"));
            JFileChooseS1.setMultiSelectionEnabled(true);
            JFileChooseS1.setFileSelectionMode(JFileChooser.FILES_ONLY);
            int result = JFileChooseS1.showDialog(this,"Open/Save");
            if(result == JFileChooser.APPROVE_OPTION){
                File S1 = JFileChooseS1.getSelectedFile();
                TxtFldS1.setText(S1.toString());
            }
        }
    but the same problem is on the JTextField i didn't set the specific path.:-(
    Of course because I don't see you getting the text from the JTextField and using it in the JFileChooser constructor anywhere, but instead are using the String literal "C:\\". Have you tried to use the JTextField text in your code but not posted this attempt here yet? If so, let's see this attempt, otherwise we can't tell what you're doing wrong.

  9. #9
    mlibot is offline Member
    Join Date
    Sep 2009
    Posts
    44
    Rep Power
    0

    Default

    Thanks. Finally i got your point...
    Thanks Fubarable.

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Also, not to step on anyone's toes, but if you are using the netbeans gui builder, then default text for a field can be set by double clicking on the text field (slowly) in the gui builder view and simply typing the default text in. As far as setting text with a button click, I think fubarable has it covered in what he said -- you can always just use the setText() method for the textfield you are trying to set.

Similar Threads

  1. Automatically Generate an XML file
    By svpriyan in forum XML
    Replies: 2
    Last Post: 06-25-2009, 05:46 AM
  2. ActionListener run automatically problem
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:42 PM
  3. Application automatically terminates
    By PaulT in forum Eclipse
    Replies: 4
    Last Post: 03-18-2009, 05:02 PM
  4. Replies: 2
    Last Post: 07-04-2008, 12:33 PM
  5. Call a Method Automatically
    By rhm54 in forum New To Java
    Replies: 4
    Last Post: 02-07-2008, 08:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •