1 Replies - 2707 Views - Last Post: 30 November 2012 - 03:45 AM Rate Topic: -----

#1 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

unable to drag and drop files from OS into JTable

Posted 30 November 2012 - 12:49 AM

UPDATED CODE

Two questions. I can't figure out how to make a new row with the dragged file. Can you guys tell me why I'm not able to drag and drop and also when dropping how to have the file contents set to the correct column e.g. file name under "FIle" size under "SIze" and so on. Thanks in advance

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DropMode;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Table extends JFrame {
	private JPanel panel, toolPanel;
	private JTable table;
	private JScrollPane scroll;
	private JButton add;
	private JFileChooser choose;
	private JMenuBar menubar;
	private JMenu menu;
	private JMenuItem file;
	private DefaultTableModel tm = new DefaultTableModel(new String[] { "File",
			"File Type", "Size", "Status" }, 2);

	public Table() {

		// String column [] = {"Filename ","File Type", "Size", "Status" };
		/*
		 * Object[][] data = { {"File1", ".jpg","32 MB", "Not Processed"},
		 * {"File2", ".txt"," 5 Kb", "Not Processed"}, {"File3", ".doc","3 Kb",
		 * "Not Processed"},
		 * 
		 * };
		 */

		table = new JTable();
		table.setModel(tm);
		table.setFillsViewportHeight(true);
		table.setDropMode(DropMode.INSERT_ROWS);
		table.setPreferredSize(new Dimension(500, 50));

		panel = new JPanel();
		panel.setPreferredSize(new Dimension(505, 350));
		panel.setBackground(Color.LIGHT_GRAY);
		panel.setBorder(BorderFactory.createTitledBorder("Files"));

		toolPanel = new JPanel();
		toolPanel.setPreferredSize(new Dimension(505, 25));
		toolPanel.setBackground(Color.ORANGE);
		toolPanel.setVisible(true);
		add(toolPanel, BorderLayout.NORTH);

		scroll = new JScrollPane(table);

		panel.add(scroll, BorderLayout.CENTER);

		menubar = new JMenuBar();
		menubar.setBackground(Color.DARK_GRAY);
		menu = new JMenu("File");
		menu.setBackground(Color.LIGHT_GRAY);
		file = new JMenuItem("file");
		menu.add(file);
		// menubar.add(menu);
		add(menu, BorderLayout.SOUTH);
		add(panel);

		ImageIcon icon = new ImageIcon("lock_icon.png");

		/*
		 * add = new JButton("Add", icon); add.addActionListener(this);
		 * 
		 * JFileChooser choose = new JFileChooser();
		 * choose.addActionListener(this);
		 */
	}// end table constructor
		// @Override

	public synchronized void dragOver(DropTargetDragEvent dtde) {

		Point point = dtde.getLocation();
		int row = table.rowAtPoint(point);
		if (row < 0) {
			table.clearSelection();
		} else {
			table.setRowSelectionInterval(row, row);
		}
		dtde.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
	}

	// @Override
	public synchronized void drop(DropTargetDropEvent dtde) {
		if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
			dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
			Transferable t = dtde.getTransferable();
			List fileList = null;
			try {
				fileList = (List) t
						.getTransferData(DataFlavor.javaFileListFlavor);
				if (fileList.size() > 0) {
					table.clearSelection();
					Point point = dtde.getLocation();
					int row = table.rowAtPoint(point);
					DefaultTableModel model = (DefaultTableModel) table
							.getModel();
					for (Object value : fileList) {
						if (value instanceof File) {
							File f = (File) value;
							if (row < 0) {
								System.out.println("addRow");
								model.addRow(new Object[] {
										f.getAbsolutePath(), "", f.length(),
										"", "" });
							} else {
								System.out.println("insertRow " + row);
								model.insertRow(row,
										new Object[] { f.getAbsolutePath(), "",
												f.length(), "", "" });
								row++;
							}
						}
					}
				}
			} catch (UnsupportedFlavorException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			dtde.rejectDrop();
		}
	}

	/*
	 * @Override public void actionPerformed(ActionEvent e) { JButton clicked =
	 * (JButton) e.getSource();
	 * 
	 * int returnValue = 0;
	 * 
	 * if (clicked == add) { choose = new JFileChooser();
	 * choose.showOpenDialog(null);
	 * 
	 * if (returnValue == JFileChooser.APPROVE_OPTION) { File file =
	 * choose.getSelectedFile(); file.getAbsolutePath();
	 * 
	 * }
	 * 
	 * }
	 * 
	 * }
	 */

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {

				Table t = new Table();

				t.setDefaultCloseOperation(EXIT_ON_CLOSE);
				t.pack();
				// t.setSize(600, 200);
				t.setVisible(true);
				t.setTitle("ZipLock");
				t.setIconImage(null);

			}
		});

	}

}


This post has been edited by healix: 30 November 2012 - 08:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: unable to drag and drop files from OS into JTable

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: unable to drag and drop files from OS into JTable

Posted 30 November 2012 - 03:45 AM

Your code is way off. You need to read the following and base yours on its examples
http://docs.oracle.c.../dnd/intro.html
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1