import java.awt.*; import java.awt.dnd.DnDConstants; import java.awt.dnd.DragGestureRecognizer; import java.awt.dnd.DragSource; import java.awt.dnd.DropTarget; import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.TitledBorder; import javax.swing.JComponent.*; import javax.swing.table.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem;
import java.awt.geom.*; import javax.swing.border.TitledBorder; //import java.awt.dnd.DropTargetEvent.*;
public class JPanels extends JFrame { private static ActionListener actionListener; public static void main(String[] args) {
new JPanels();
}
public JPanels() {
JFrame frame = new JFrame("DND INTERFACE"); frame.setDefaultCloseOperation(JFrame.EXITONCLOSE); JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu); JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); fileMenu.add(newMenuItem); frame.setJMenuBar(menuBar); frame.setSize(800, 500); frame.setVisible(true); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); content.setBackground(Color.lightGray); JPanel SixChoicePanel1 = new JPanel (new GridLayout(15,1)); setBackground(Color.lightGray);
SixChoicePanel1.setBorder(BorderFactory.createTitledBorder("Tools"));
JButton option = new JButton("Drag this button1"); JButton option1 = new JButton("Drag this button"); JButton option2 = new JButton("Drag this button"); JButton option3 = new JButton("Drag this button"); JButton option4 = new JButton("Drag this button");
SixChoicePanel1.add(option); SixChoicePanel1.add(option1); SixChoicePanel1.add(option2); SixChoicePanel1.add(option3); SixChoicePanel1.add(option4); JPanel panel = new JPanel(new GridLayout(2,1)); panel.setBackground(Color.white); JTable table; //=new JTable;
DefaultTableModel modeltable = new DefaultTableModel(8,8);
table = new JTable(modeltable); table.setBorder(BorderFactory.createLineBorder (Color.blue, 2));
int height = table.getRowHeight(); table.setRowHeight(height=50);
table.setColumnSelectionAllowed(true); //table.setTransferHandler(new MyTransferHandler()); table.setDragEnabled(true);
le1.setFillsViewportHeight(true);
panel.add(table); panel.setSize(400,400);
JPanel area = new JPanel(); JButton btnSave = new JButton("Save"); area.add(btnSave); panel.add(area); frame.add(SixChoicePanel1); frame.add(panel, BorderLayout.EAST); pack(); DnDListener dndListener = new DnDListener(); DragSource dragSource = new DragSource(); DropTarget dropTarget1 = new DropTarget(table, dndListener); DragGestureRecognizer dragRecognizer2 = dragSource. createDefaultDragGestureRecognizer(option1, DnDConstants.ACTION_COPY, dndListener); DragGestureRecognizer dragRecognizer3 = dragSource. createDefaultDragGestureRecognizer(option2, DnDConstants.ACTION_COPY, dndListener);
} }
i have a problem with adding mouse listeners to "table" which is drop target, to accept drop component wherever it drops from the mouse. in this code when component drops in to a drop target it always goes to a default position. i cant customize the position on drop target. please someone help me with this. thanks in advance
Here is a drag and drop application of Java Swing.
import java.awt.*; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.awt.dnd.peer.*; import java.awt.dnd.DropTarget; import javax.swing.*; public class DragDrop implements DragGestureListener, DragSourceListener, DropTargetListener, Transferable { static final DataFlavor[] supportedFlavors = {null}; static { try { supportedFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType); } catch (Exception ex) { ex.printStackTrace(); } } Object object; public Object getTransferData(DataFlavor flavor) { if (flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)) { return object; } else { return null; } } public DataFlavor[] getTransferDataFlavors() { return supportedFlavors; } public boolean isDataFlavorSupported(DataFlavor flavor) { return flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType); } public void dragGestureRecognized(DragGestureEvent ev) { ev.startDrag(null, this, this); } public void dragDropEnd(DragSourceDropEvent ev) { } public void dragEnter(DragSourceDragEvent ev) { } public void dragExit(DragSourceEvent ev) { } public void dragOver(DragSourceDragEvent ev) { object = ev.getSource(); } public void dropActionChanged(DragSourceDragEvent ev) { } public void dragEnter(DropTargetDragEvent ev) { } public void dragExit(DropTargetEvent ev) { } public void dragOver(DropTargetDragEvent ev) { dropTargetDrag(ev); } public void dropActionChanged(DropTargetDragEvent ev) { dropTargetDrag(ev); } void dropTargetDrag(DropTargetDragEvent ev) { ev.acceptDrag(ev.getDropAction()); } public void drop(DropTargetDropEvent ev) { ev.acceptDrop(ev.getDropAction()); try { Object target = ev.getSource(); Object source = ev.getTransferable().getTransferData(supportedFlavors[0]); Component component = ((DragSourceContext) source).getComponent(); Container oldContainer = component.getParent(); Container container = (Container) ((DropTarget) target).getComponent(); container.add(component); oldContainer.validate(); oldContainer.repaint(); container.validate(); container.repaint(); } catch (Exception ex) { ex.printStackTrace(); } ev.dropComplete(true); }
continue..
public static void main(String[] arg) { Button button = new Button("Drag this button"); Label label = new Label("Drag this label"); Checkbox checkbox = new Checkbox("Drag this check box"); CheckboxGroup radiobutton = new CheckboxGroup(); Checkbox checkbox1 = new Checkbox("Drag this check box", radiobutton, false); Choice country = new Choice(); country.add("India"); country.add("US"); country.add("Australia"); Frame source = new Frame("Source Frame"); source.setLayout(new FlowLayout()); source.add(button); source.add(label); source.add(checkbox); source.add(checkbox1); source.add(country); JFrame target = new JFrame("Target Frame"); target.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); target.setLayout(new FlowLayout()); DragDrop dndListener = new DragDrop(); DragSource dragSource = new DragSource(); DropTarget dropTarget1 = new DropTarget(source, DnDConstants.ACTION_COPY,dndListener); DropTarget dropTarget2 = new DropTarget(target, DnDConstants.ACTION_COPY,dndListener); DragGestureRecognizer dragRecognizer1 = dragSource.createDefaultDragGestureRecognizer(button, DnDConstants.ACTION_COPY, dndListener); DragGestureRecognizer dragRecognizer2 = dragSource.createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_COPY, dndListener); DragGestureRecognizer dragRecognizer3 = dragSource.createDefaultDragGestureRecognizer(checkbox, DnDConstants.ACTION_COPY, dndListener); DragGestureRecognizer dragRecognizer4 = dragSource.createDefaultDragGestureRecognizer(checkbox1, DnDConstants.ACTION_COPY, dndListener); DragGestureRecognizer dragRecognizer5 = dragSource.createDefaultDragGestureRecognizer(country, DnDConstants.ACTION_COPY, dndListener); source.setBounds(0, 200, 200, 200); target.setBounds(220, 200, 200, 200); target.setVisible(true); source.setVisible(true); } }
For more information, visit the following link:
http://www.roseindia.net/java/example/java/swing/SwingDragDrop.shtml
Ads