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 196933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:48:35+00:00 2026-05-11T16:48:35+00:00

I have a JTextField in my Swing application that holds the file path of

  • 0

I have a JTextField in my Swing application that holds the file path of a file selected to be used. Currently I have a JFileChooser that is used to populate this value. However, I would like to add the ability for a user to drag-and-drop a file onto this JTextField and have it place the file path of that file into the JTextField instead of always having using the JFileChooser.

How can this be done?

  • 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-11T16:48:35+00:00Added an answer on May 11, 2026 at 4:48 pm

    First you should look into Swing DragDrop support. After that there are few little tricks for different operating systems. Once you’ve got things going you’ll be handling the drop() callback. In this callback you’ll want to check the DataFlavor of the Transferable.

    For Windows you can just check the DataFlavor.isFlavorJavaFileListType() and then get your data like this

    List<File> dropppedFiles = (List<File>)transferable.getTransferData(DataFlavor.javaFileListFlavor)
    

    For Linux (and probably Solaris) the DataFlavor is a little trickier. You’ll need to make your own DataFlavor and the Transferable type will be different

    nixFileDataFlavor = new DataFlavor("text/uri-list;class=java.lang.String");
    String data = (String)transferable.getTransferData(nixFileDataFlavor);
    for(StringTokenizer st = new StringTokenizer(data, "\r\n"); st.hasMoreTokens();)
    {
        String token = st.nextToken().trim();
        if(token.startsWith("#") || token.isEmpty())
        {
             // comment line, by RFC 2483
             continue;
        }
        try
        {
             File file = new File(new URI(token))
             // store this somewhere
        }
        catch(...)
        {
           // do something good
           ....
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Swing Application with a JTextField that the user is Supposed to
I have a JTextField that I'm not always passing a value to, but it
I've a text field which extends javax.swing.JTextField and also have document listener like this
I have a Java Swing application, that has many JTextFields and a datamodel. When
I have a swing application with multiple jtextfield on it. How do you replace
I have a swing gui application. I want to type into a image path
I'm programming a Swing Application and I have a friend that read much the
So I have a basic swing application, it contains a JTextfield , I add
I am learning Java with Swing and I have some problems with using JTextField
I have a program that uses three JTextField fields as the main data entry

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.