如何解决这个运行时异常?

时间:2012-11-16 20:17:51

标签: java exception user-interface jlist jfilechooser

如何解决此代码的运行时异常?
当点击按钮文件选择器并从中添加文件时,面板颜色消失(面板中发生错误的事情)

这是我的代码:

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileSystemView;
import javax.swing.border.BevelBorder;
import javax.swing.JFileChooser;
import javax.swing.plaf.FileChooserUI;

public class pan extends JPanel implements DropTargetListener {

private DefaultListModel listModel = new DefaultListModel();
private DropTarget dropTarget;
private JScrollPane droparea;
private JList list;
private JButton addbutton;


// Create the panel.  
//and add to it component

public pan() {
    setLayout(null);
    addbutton = new JButton("New button");
    addbutton.setBounds(10, 10, 90, 100);
    addbutton.addActionListener(new Action());
    add(addbutton);

    list = new JList();
    dropTarget = new DropTarget(list, this);
    list.setModel(listModel);
    list.setDragEnabled(true);
    FileListCellRenderer renderer = new FileListCellRenderer();
    list.setCellRenderer(renderer);
    list.addMouseListener(new mouselistner());
    list.clearSelection();
    list.setFixedCellHeight(40);
    droparea = new JScrollPane();
    droparea.setViewportView(list);
    droparea.setBounds(10, 150, 635, 330);
    add(droparea);

}// </editor-fold>
    // Variables declaration - do not modify

// End of variables declaration

public void dragEnter(DropTargetDragEvent arg0) {
    // nothing
}

public void dragOver(DropTargetDragEvent arg0) {
    // nothing
}

public void dropActionChanged(DropTargetDragEvent arg0) {
    // nothing
}

public void dragExit(DropTargetEvent arg0) {
    // nothing
}

public void drop(DropTargetDropEvent evt) {
    int action = evt.getDropAction();
    evt.acceptDrop(action);
    try {
        Transferable data = evt.getTransferable();
        if (data.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
        List<File> files = (List<File>) data
                .getTransferData(DataFlavor.javaFileListFlavor);
            for (File file : files) {
                listModel.addElement(file);
            }
        }
    } catch (UnsupportedFlavorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        evt.dropComplete(true);
    }
}

/** A FileListCellRenderer for a File. */
class FileListCellRenderer extends DefaultListCellRenderer {

    private static final long serialVersionUID = -7799441088157759804L;
    private FileSystemView fileSystemView;
    private JLabel label;
    private Color textSelectionColor = Color.BLACK;
    private Color backgroundSelectionColor = Color.CYAN;
    private Color textNonSelectionColor = Color.BLACK;
    private Color backgroundNonSelectionColor = Color.WHITE;

    FileListCellRenderer() {
        label = new JLabel();
        label.setOpaque(true);
        fileSystemView = FileSystemView.getFileSystemView();
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean selected, boolean expanded) {
        label.setBorder(new BevelBorder(BevelBorder.RAISED,
                Color.LIGHT_GRAY, Color.GRAY, null, null));
        File file = (File) value;
        label.setIcon(fileSystemView.getSystemIcon(file));
        label.setText(fileSystemView.getSystemDisplayName(file));
        label.setToolTipText(file.getPath());
        if (selected) {
            label.setBackground(Color.blue);
            label.setForeground(textSelectionColor);
        } else {
            label.setBackground(backgroundNonSelectionColor);
            label.setForeground(textNonSelectionColor);
        }

        return label;
    }
}

class Action implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==addbutton){
        //  FileSystemView fsv=FileSystemView.getFileSystemView();

            JFileChooser filechooser=new JFileChooser();
            filechooser.setMultiSelectionEnabled(true);
filechooser.setFileSelectionMode   (JFileChooser.FILES_AND_DIRECTORIES);
            File files=filechooser.getSelectedFile();
        filechooser.showDialog(null, "add");  //here panel problem i think
        listModel.addElement(files);  //and here run time error genrate 

        }

    }
}
 }

最后一行的两个问题

1 个答案:

答案 0 :(得分:1)

问题是,在显示JFileChooser对话框之前,您将获得所选文件,因此所选文件将为null。交换这两行得到:

int returnValue = filechooser.showDialog(null, "add");
if (returnValue == JFileChooser.APPROVE_OPTION) {
   File files = filechooser.getSelectedFile();
   listModel.addElement("fooooo"); 
}

如果点击取消按钮,最好使用JFileChooser的返回值。