É muito simples! Estou usando um JList habilitando o seu DRAG, e o JTable associado com DropTarget para receber a String arrastada da JList.
package com.blogspot.receitastecnologicas.dnd; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Point; import java.awt.dnd.DropTarget; import java.awt.dnd.DropTargetDropEvent; import java.util.ArrayList; import java.util.List; import javax.swing.AbstractListModel; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.DropMode; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.table.DefaultTableModel; public class DragAndDrop extends JFrame { /** * */ private static final long serialVersionUID = 1L; private AbsModellistModel = new AbsModel<>(); private JTable table; private DefaultTableModel tableModel; JList listDrag; public DragAndDrop() { super("DnD - Receitas Tecnologicas"); listDrag = new JList(listModel); listDrag.setFocusable(false); listDrag.setDragEnabled(true); // habilitando o Drag listDrag.setBorder(BorderFactory.createLoweredBevelBorder()); /*** * @TODO NÃO USE DefaultTableModel - USE AbstractTableModel */ tableModel = new DefaultTableModel(new Object[] { "TO DO", "DONE" }, 10); table = new JTable(tableModel); table.setRowHeight(48); // table.setEditingColumn(2); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setDropMode(DropMode.INSERT); //O javadoc já diz tudo: Associate a DropTarget with this component. The Component will receive drops only if it is enabled. table.setDropTarget(new DropTarget() { @Override public synchronized void drop(DropTargetDropEvent dtde) { try { Point point = dtde.getLocation(); int column = table.columnAtPoint(point); int row = table.rowAtPoint(point); table.addRowSelectionInterval(row, row); table.addColumnSelectionInterval(column, column); // inserindo na tabela String text = (String) listModel.getElementAt(listDrag .getSelectedIndex()); tableModel.setValueAt(text, table.getSelectedRow(), table.getSelectedColumn()); } catch (Exception e) { e.printStackTrace(); } super.drop(dtde); } }); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JPanel wrap = new JPanel(); wrap.add(new JLabel("Selecione e Arraste:")); wrap.add(listDrag); panel.add(Box.createHorizontalStrut(4)); panel.add(Box.createGlue()); panel.add(wrap); panel.add(Box.createGlue()); panel.add(Box.createHorizontalStrut(4)); getContentPane().add(panel, BorderLayout.NORTH); JScrollPane sp = new JScrollPane(table); getContentPane().add(sp, BorderLayout.CENTER); getContentPane().setPreferredSize(new Dimension(500, 500)); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { UIManager.put("swing.boldMetal", Boolean.FALSE); DragAndDrop dragAndDrop = new DragAndDrop(); dragAndDrop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); dragAndDrop.pack(); dragAndDrop.setVisible(true); } }); } } @SuppressWarnings("hiding") class AbsModel extends AbstractListModel { /** * */ private static final long serialVersionUID = 1L; List list = new ArrayList (); public AbsModel() { list.add((String) "Sarapatel"); list.add((String) "Buchada"); list.add((String) "Rabada"); list.add((String) "Carne de Sol com Macaxeira"); list.add((String) "Pirão"); } @Override public int getSize() { // TODO Auto-generated method stub return list.size(); } @Override public String getElementAt(int index) { // TODO Auto-generated method stub return list.get(index); } }
Nenhum comentário:
Postar um comentário