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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:32:50+00:00 2026-05-13T05:32:50+00:00

I am making a Java program, and one way to load files is to

  • 0

I am making a Java program, and one way to load files is to drag the file onto the application window (swing). I have code that works in Windows and Linux. It works in OS X, but the 1st file I drag throws an exception, and then the rest work fine.

Here is the code I use to enable DnD.

/*
 * Allow a file to be opened by dragging it onto the window
 */
public void drop(DropTargetDropEvent dtde){
    try {
        // Get the object to be transferred
        Transferable tr = dtde.getTransferable();
        DataFlavor[] flavors = tr.getTransferDataFlavors();

        // If flavors is empty get flavor list from DropTarget
        flavors = (flavors.length == 0) ? dtde.getCurrentDataFlavors() : flavors;

        // Select best data flavor
        DataFlavor flavor = DataFlavor.selectBestTextFlavor(flavors);

        // Flavor will be null on Windows
        // In which case use the 1st available flavor
        flavor = (flavor == null) ? flavors[0] : flavor;

        // Flavors to check
        DataFlavor Linux = new DataFlavor("text/uri-list;class=java.io.Reader");
        DataFlavor Windows = DataFlavor.javaFileListFlavor;

        // On Linux (and OS X) file DnD is a reader
        if(flavor.equals(Linux)) {
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

            BufferedReader read = new BufferedReader(flavor.getReaderForText(tr));
            // Remove 'file://' from file name
            String fileName = read.readLine().substring(7).replace("%20"," ");
            // Remove 'localhost' from OS X file names
            if(fileName.substring(0,9).equals("localhost")) {
                fileName = fileName.substring(9);
            }
            read.close();

            dtde.dropComplete(true);
            System.out.println("File Dragged:" + fileName);
            mainWindow.openFile(fileName);
        }
        // On Windows file DnD is a file list
        else if(flavor.equals(Windows)) {
            dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
            @SuppressWarnings("unchecked")
            List<File> list = (List<File>)tr.getTransferData(flavor);
            dtde.dropComplete(true);

            if(list.size() == 1) {
                System.out.println("File Dragged: " + list.get(0));
                mainWindow.openFile(list.get(0).toString());
            }
        } else {
            System.err.println("DnD Error");
            dtde.rejectDrop();
        }
    }
    //TODO: OS X Throws ArrayIndexOutOfBoundsException on first DnD
    catch(ArrayIndexOutOfBoundsException e){
        System.err.println("DnD not initalized properly, please try again.");
    } catch(IOException e){
        System.err.println(e.getMessage());
    } catch(UnsupportedFlavorException e){
        System.err.println(e.getMessage());
    } catch (ClassNotFoundException e){
        System.err.println(e.getMessage());
    }
}

For some reason, OS X throws an ArrayIndexOutOfBoundsException on this line:

flavor = (flavor == null) ? flavors[0] : flavor;

After that exception is thrown, if I drag another file onto the window, it works. Why does it throw an exception?

NOTE: mainWindow.openFile() is a function that opens a file. It takes a string parameter (the file name), and the program opens that file.

NOTE 2: This is on OS X 10.6.2 (Snow Leopard).

  • 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-13T05:32:51+00:00Added an answer on May 13, 2026 at 5:32 am

    I had this problem too but it appears to be fixed with the latest java version:

    ray@featuritis:~/projects>java -version
    java version "1.6.0_17"
    Java(TM) SE Runtime Environment (build 1.6.0_17-b04-248-10M3025)
    Java HotSpot(TM) 64-Bit Server VM (build 14.3-b01-101, mixed mode)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.