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

  • Home
  • SEARCH
  • 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 9194887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:31:05+00:00 2026-06-17T21:31:05+00:00

I wrote a java code having an awt text field and a button, where

  • 0

I wrote a java code having an awt text field and a button, where if I click on the button, I can browse a file using JFileChooser. It needs to check whether the file has “.txt” extension or not.I wrote the code below, but it is not getting verified.

Where am I going wrong? Please help to determine where I am wrong.

         try{
            final JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            chooser.addChoosableFileFilter(new FileFilter() {
            public String getDescription() {
                return "*.txt";
            }
            public boolean accept(File filename)
            {

                if(filename.getName().endsWith(".txt")){
                    return true;
                }
                else{
                System.out.println("Browsed dest file extension must be .txt");
                return false;
                }}
            });
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(f,"Exception occurred");
        }
  • 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-06-17T21:31:06+00:00Added an answer on June 17, 2026 at 9:31 pm

    Your problem is that:

    chooser.showOpenDialog(null);
    

    stop execution of code until user select a file. Add this line after adding FileFilter and eveyrthing should work ok.

    Little explanation:

    Method showOpenDialog(Component c) blockexecution of current thread until user action and next line of your code will be executed after the user choose a file. If you invoke after adding a FileFilter once again showOpenDialog it will work as you expect.

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

Sidebar

Related Questions

I wrote some code to read a file in my Java Servlet class. (I'm
I wrote a piece of java code using threads, JDBC, Java Mail API etc.
I usually write java code using a simple text editor and .bat files to
I wrote some code (Java and LDAP) to create a user in the Active
I wrote the following code: import java.lang.*; import DB.*; private Boolean validateInvoice(String i) {
Is there any maximum size for code in Java? I wrote a function with
I wrote a same(..similar?) piece of code for Java 7 and C#(.net 3.5) and
I have a short question i have wrote this in java. Old code: class
I have wrote the code below which was taken from Java How to program
i am a newbie for java server pages and wrote a code for a

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.