Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3954804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:08:48+00:00 2026-05-20T02:08:48+00:00

open.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ JFileChooser fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new java.io.File(.));

  • 0
    open.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent e){
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new java.io.File("."));
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

            if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                System.out.println(fileChooser.getSelectedFile());
                }
        }
    });

This might be a badly worded question but here we go:

I need this part of my code to produce ‘fileChooser.getSelectedFile());’ in a format that can be used elsewhere. I don’t mind if it is a variable (won’t really work because I need to call it in another actionListener) or (as I planned) output the selected folder as a string to an output file and then read that file in elsewhere in the program.

It’s important that the file path (e.g. C:/Users/Desktop/) be a String because that is what the class which will use the path takes in.

I’ve tried a couple of options but often get the “inconvertable types’ compile error etc, if anyone has any ideas they would share, that’d be great

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T02:08:48+00:00Added an answer on May 20, 2026 at 2:08 am
    open.addActionListener(new ActionListener(){
            public void actionPerformed (ActionEvent e){
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setCurrentDirectory(new java.io.File("."));
                fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    
                if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                    try{
                    DataOutputStream outputPath = new DataOutputStream(new FileOutputStream("C:/Users/Jonathan/Desktop/YouDetectJava/FolderPath.dat"));
                    String exportPath = fileChooser.getSelectedFile() + "\\";
                    outputPath.writeUTF(exportPath);
                    //System.out.println(exportPath);
                    }catch (IOException ioe){
                    }
                    }
            }
        });
    

    Seems to fix it. Sorry to post and answer my own question. Figured out how to do it whilst I was waiting for replies. Seems like to be a string ‘exportPath’ has to have a string in it. In this case “\” but it can also be “” too.

    No idea why but there you go 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

.addActionListener(new ActionListener(){ public void actionPerformed (ActionEvent e){ try{ ta.append(Searching Initiated at: +datetime()+\n); gui.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); task.execute();
public class MyWindow extends JFrame { JButton botonCreate = new JButton(Open son windows); public
I'm writing java applet. I want to to open a new Frame with content
open( my $handle, '<', 'file.dat' ) or die $!; my @data = map {
I create a jPanel and I open a new jDialog when I click on
I want to open a text file in a frame using swing components, preferably
I wrote this code: public class FileViewer extends JPanel implements ActionListener { /** *
I am trying to open a file in a JTextArea and then write and
Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
//open file if((fd = open(test.txt, O_RDWR | O_APPEND)) == -1) printf(open failed\n); //set offset

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.