Moe
Moe

Reputation: 57

Unable to declare a class field

We are trying to pass the variable file to another class. We are having a problem at these lines:

file = (File) item;

        listModel.addElement(file);

at the start of the class we declare the field file which we thought could be used throughout the whole class.. yet, when we get to the lines mentioned we get the error file cannot be resolved to a variable If we make file a local variable the class works, however we cannot pass on the variable file onto another class.

Here is the code:

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

@SuppressWarnings("serial")
public class FileDragDemo extends JPanel {
    public static File file;
    public static List data;
    public JList list = new JList();


    public FileDragDemo() {
        list.setDragEnabled(true);
        list.setTransferHandler(new FileListTransferHandler(list));

        add(new JScrollPane(list));

    }

    public static void createAndShowGui() {
        FileDragDemo mainPanel = new FileDragDemo();

        JFrame frame = new JFrame("FileDragDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) throws Exception {

        //String filestring ="";
        //data = new List;

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
                PdfEasyManager PdfEasyManagerObject = new PdfEasyManager();
                try {
                    PdfEasyManagerObject.main(args);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

@SuppressWarnings("serial")
class FileListTransferHandler extends TransferHandler {
    public JList list;

    public FileListTransferHandler(JList list) {
        this.list = list;
    }

    public int getSourceActions(JComponent c) {
        return COPY_OR_MOVE;
    }

    public boolean canImport(TransferSupport ts) {
        return ts.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
    }

    public boolean importData(TransferSupport ts) {
        try {
            @SuppressWarnings("rawtypes")

            List data = (List) ts.getTransferable().getTransferData(
                    DataFlavor.javaFileListFlavor);
            if (data.size() < 1) {
                return false;
            }// close if

            DefaultListModel listModel = new DefaultListModel();

            for (Object item : data) {

                file = (File) item;

                listModel.addElement(file);

            } // close for
            //String filestring = file.toString();

            list.setModel(listModel);
            return true;

        }// close try  
        catch (UnsupportedFlavorException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
    }
}

Upvotes: 1

Views: 98

Answers (2)

Stefan Dollase
Stefan Dollase

Reputation: 4620

You can only use the field file from the class where it is declared. You have two classes: FileDragDemo declares the field file, but the class FileListTransferHandler tries to use it. This is indeed supposed to give you the described error.

To solve this problem, you can declare the class FileListTransferHandler in the class FileDragDemo. To do this, simply close the } of the outer class declaration after the inner class declaration, like so:

public class FileDragDemo {
    // ...

    class FileListTransferHandler {
        // ...
    }
}

Upvotes: 1

Pachelbel
Pachelbel

Reputation: 533

The static field file is declared in class FileDragDemo. However, the lines in which you are using it are in another class named FileListTransferHandler. If you want to access it within methods of this class, you have to qualify the field access with the declaring class: FileDragDemo.file.

However, in the code you posted I think using this static variable seems not too elegant, I would use a local variable here. Often static variables can and should be avoided.

Upvotes: 4

Related Questions