大佬教程收集整理的这篇文章主要介绍了将文件从OS拖放到JTable Java中,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我个人会放弃滚动窗格上的放置目标,这将导致您遇到许多问题。
您的放置方法有点怪异…
这是一个坏主意....
List fileList = null;
try {
fileList = (List) t
.getTransferData(DataFlavor.javafileListFlavor);
} catch (UnsupportedFlavorException E) {
// Todo auto-generated catch block
e.printstacktrace();
} catch (IOException E) {
// Todo auto-generated catch block
e.printstacktrace();
}
file f = (filE) fileList.get(0);
table.SETVALueAt(f.getabsolutePath(), row, column);
table.SETVALueAt(f.length(), row, column + 1);
基本上,您尝试从可转移文件中提取文件列表,并且不管操作成功与否,您都尝试使用它!您根本不验证返回的值…
由于您已经有name和size列,因此您的放置代码通常实际上并不关心放置在哪个列上,因此我实际上将其完全忽略。
至于行,现在您有两个选择。当用户没有放在现有行上时添加新行,或者拒绝该尝试。
@H_450_18@拒绝表格的“外部”拖动(或拒绝不调用现有行的拖动)
要在用户拖动时拒绝该操作,您需要重写该dragOver
方法…
@OverrIDe
public synchronized voID dragOver(DropTargetDragEvent dtdE) {
Point point = dtde.getLOCATIOn();
int row = table.rowAtPoint(point);
if (row < 0) {
dtde.rejectDrag();
table.clearSELEction();
} else {
dtde.acceptDrag(DnDConstants.ACTION_copY_OR_MOVE);
table.setRowSELEctionInterval(row, row);
}
}
现在,我在这里有点聪明(不是很聪明)。基本上,如果用户拖动了一行,则将其突出显示。这使得下降的方向更加明显。
在您的放置方法中,我还将进行一些其他检查…
@OverrIDe
public synchronized voID drop(DropTargetDropEvent dtdE) {
Point point = dtde.getLOCATIOn();
int row = table.rowAtPoint(point);
if (row >= 0) {
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();
model.SETVALueAt(f.getabsolutePath(), row, 0);
model.SETVALueAt(f.length(), row, 2);
}
} catch (UnsupportedFlavorException E) {
e.printstacktrace();
} catch (IOException E) {
e.printstacktrace();
}
} else {
dtde.rejectDrop();
}
} else {
dtde.rejectDrop();
}
}
@H_450_18@接受表格的Drag的“外部”
这个过程相对来说是相同的,除了现在我们可以抛弃那些本来会导致我们拒绝拖放的条件(显然)
@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);
}
和drop
方法
@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();
}
}
注意。这将在放置点插入行,将所有现有行向下推,或者如果未拖放到现有行上,则将其添加到末尾…
这是我用来测试代码的完整示例。
public class Droptable {
public static voID main(String[] args) {
new Droptable();
}
public Droptable() {
EventQueue.invokelater(new Runnable() {
@OverrIDe
public voID run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassname());
} catch (ClassnotFoundException | InstantiationException | illegalaccessexception | UnsupportedLookAndFeelException eX) {
}
JFrame frame = new JFrame();
frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new borderLayout());
frame.add(new DropPane());
frame.pack();
frame.setLOCATIOnrelativeTo(null);
frame.setVisible(true);
}
});
}
public class DropPane extends JPanel {
private Jtable table;
private JscrollPane scroll;
private DefaulttableModel tm = new DefaulttableModel(new String[]{"file", "file Type", "Size", "Status"}, 0);
public DropPane() {
table = new Jtable();
table.setShowGrID(true);
table.setShowHorizontallines(true);
table.setShowVerticallines(true);
table.setGrIDcolor(color.GRAY);
table.setModel(tm);
table.setFillsVIEwportHeight(true);
table.setPreferredSize(new Dimension(500, 300));
scroll = new JscrollPane(tablE);
table.setDropTarget(new DropTarget() {
@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) {
model.addRow(new Object[]{f.getabsolutePath(), "", f.length(), "", ""});
} else {
model.insertRow(row, new Object[]{f.getabsolutePath(), "", f.length(), "", ""});
row++;
}
}
}
}
} catch (UnsupportedFlavorException E) {
e.printstacktrace();
} catch (IOException E) {
e.printstacktrace();
}
} else {
dtde.rejectDrop();
}
}
});
add(scroll, borderLayout.CENTER);
}
}
}
有人可以告诉我我在做什么错吗?我可以使用常规面板进行拖放操作,但现在尝试使用表格,无法对其进行整理。我对Points和DropTargets感到困惑。不要介意“添加”按钮。我觉得我需要首先处理DnD。
public class Table extends JFrame implements ActionListener {
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 ","Status" };
/*
* Object[][] data = { {"File1",".jpg","32 MB","Not Processed"},* {"File2",".txt"," 5 Kb",{"File3",".doc","3 Kb",* "Not Processed"},* };
*/
table = new JTable();
table.setModel(tm);
table.setFillsViewportHeight(true);
table.setPreferredSize(new Dimension(500,300));
scroll = new JScrollPane(tablE);
table.setDropTarget(new DropTarget() {
@Override
public synchronized void drop(DropTargetDropEvent dtdE) {
Point point = dtde.getLOCATIOn();
int column = table.columnAtPoint(point);
int row = table.rowAtPoint(point);
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
Transferable t = dtde.getTransferable();
List fileList = null;
try {
fileList = (List) t
.getTransferData(DataFlavor.javaFileListFlavor);
} catch (UnsupportedFlavorException E) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException E) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f = (FilE) fileList.get(0);
table.SETVALueAt(f.getAbsolutePath(),row,column);
table.SETVALueAt(f.length(),column + 1);
super.drop(dtdE);
}
});
scroll.setDropTarget(new DropTarget() {
@Override
public synchronized void drop(DropTargetDropEvent dtdE) {
Point point = dtde.getLOCATIOn();
int column = table.columnAtPoint(point);
int row = table.rowAtPoint(point);
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
Transferable t = dtde.getTransferable();
List fileList = null;
try {
fileList = (List) t
.getTransferData(DataFlavor.javaFileListFlavor);
} catch (UnsupportedFlavorException E) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException E) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File f = (FilE) fileList.get(0);
table.SETVALueAt(f.getAbsolutePath(),column + 1);
// handle drop outside current table (e.g. add row)
super.drop(dtdE);
}
});
add(scroll,BorderLayout.CENTER);
menubar = new JMenuBar();
menu = new JMenu("File");
file = new JMenuItem("file");
menu.add(filE);
// menubar.add(menu);
add(menu,BorderLayout.NORTH);
ImageIcon icon = new ImageIcon("lock_icon.png");
add = new JButton("Add",icon);
add.addActionListener(this);
JFileChooser choose = new JFileChooser();
choose.addActionListener(this);
}
@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);
}
});
}
}
以上是大佬教程为你收集整理的将文件从OS拖放到JTable Java中全部内容,希望文章能够帮你解决将文件从OS拖放到JTable Java中所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。