Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners adding mouse listeners to drop target

Have Programming Question? Ask it here!
 
 
 


cjay
adding mouse listeners to drop target
2 Answer(s)      a year and 9 months ago
Posted in : Java Beginners


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


Advertisement
View Answers

July 10, 2012 at 11:29 AM


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);
}


July 10, 2012 at 11:30 AM


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



Related Tutorials/Questions & Answers:
adding mouse listeners to drop target
with adding mouse listeners to "table" which is drop target, to accept drop...adding mouse listeners to drop target  import java.awt.*; import... in to a drop target it always goes to a default position. i cant customize
Mouse Listeners
Java NotesMouse Listeners There are several styles for using the mouse listeners.   They are usually added to a graphics panel... a panel and want the listeners outside it because it is more convenient
Advertisements
how to custom location of a object after dragging to the drop target
;how to change the position of a button when after it drags on the drop target... how to drag the object inside the drop target. (that means how to change the position of the object after it drops on to the drop target
Mouse Drag and Drop
Mouse Drag and Drop       This section illustrates you how to drag and move mouse to draw a figure. To draw a figure using drag and drop, we have used Rectangle2D class to draw
The Mouse
- Handles moves and drags. Mouse Listeners - How and where to write mouse listeners... Java NotesThe Mouse The mouse is handled automatically by most components... (and shouldn't care) whether this was from a mouse click on the button, or from
Action Listeners
Action Listeners  Please, could someone help me with how to use action listeners I am creating a gui with four buttons. I will like to know how to apply the action listener to these four buttons.   Hello Friend, Try
Mouse Listener
Mouse Listener  how to move any object with help of mouse but not drag it in an applet
Mouse Motion
Mouse Motion  Could you please send me the code of finding the (x,y) co-ordinates in a JFrame Compoenent..?? I need it urgently for my project
mouse events
mouse events  When i click a button i want to retrieve image and audio from database and play it. how can i do dis. I m using Netbeans IDE and mysql for database. pls do hel[p me
Event listeners in mxml
Event listeners in mxml  hi.. just tell me about How do you add event listeners in mxml components. Now AS3 components? Please give an example in both MXML and AS3 Thanks  Ans: There are some function
how to use mouse pressed
how to use mouse pressed   to action mouse clicked
How to set target to videos??
How to set target to videos??  Hi, i am developing project in HTML 5 with videos so, i am dispalaing videos in two table coloumn,if the video in one coloumn is clicked,it should be played in another coloumn. plzz suggest any idea
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very lengthy...so please give the code for multiple
Image swap on mouse over and mouse move
Image swap on mouse over and mouse move  i tried the code , sent form u...But if there are 50 images and..i want swap images on mousse over and mouse out, then the code becomes very lengthy...so please give the code for multiple
mouse over effects in css
mouse over effects in css  I have written a mouse over effect in CSS. But it is not working in HTML. Can any one suggest me? Thanks
Adding an employee
Adding an employee  coding for adding an employee
Swap images oon mouse over and mouse out
Swap images oon mouse over and mouse out  I have multiple images...i want to swap each image on mouse out and mouse over..   <html> <script language="javascript" type="text/javascript"> if(document.images
HTML5 target attribute, Definition and use of target attribute.
HTML5 target attribute, Definition and use of target attribute. In this tutorial we will inform about the target attribute of <area> tag in html5... in the document. The target attribute helps to open the document in the same window
Listeners
, and top-level listeners for mouse listeners for graphics panels. Other... listeners: Named Inner Class Listeners are one of the most common ways... with several components. Anonymous Inner Class Listeners are sometimes
mouse event - Java Beginners
mouse event  import javax.swing.*; import java.awr.*; import...=getContentPane(); setLayout(new FlowLayout()); setTitle("Mouse Event..."); public MouseEventt() { f.setLayout(new FlowLayout()); f.setTitle("Mouse
4D Mouse
4D Mouse       Here... mouse is an advanced species of traditional 2D mouse that can move only horizontal no vertical, while 4D mouse can move all the direction ups & down and left
Drop Box
Drop Box  program draw 2d shapes in java
Drop Box
Drop Box  program draw 2d shapes in java
adding a dialogue
adding a dialogue  Blockquote Hi can you help with the program below,the program is a loop that prints out a code and a quantity when prompt for the user input.what I need is to modify the code to incorporate a dialogue asking
adding loop
adding loop  Hi I have a program that is not compiling when I add a loop can you help me?The program below is compiling without the loop. > Blockquote mport java.util.*; import java.text.*; import java.util.Scanner
Mouse Buttons, Modifier Keys
Java NotesMouse Buttons, Modifier Keys Mouse Buttons. Java supports up to three mouse buttons. Even if your mouse doesn't have three separate... the mouse button. The MouseEvent object that is passed to the listener
Handling Mouse Clicks in Java
Handling Mouse Clicks in Java     ... the mouse click event in the awt application. This program simply implements the left click event of the mouse. When you click "Click Me" button
Drop Down
Drop Down  How to insert date into database using dropdown like facebook
drop down
drop down  how can i add data from choice/dropdown component of java awt to myaql table
Drag and drop file uploading - Ajax
Drag and drop file uploading  Hi all, This is NageswaraRao i want file uploading feature on my web development..using drag and drop mouse functionality. Problem:I have Created one Text area when i drop the file on text area
Reflection api :Invocation target exception - JDBC
Reflection api :Invocation target exception   Am using a function to insert a record into the database .Using reflection api am invoking... in Invocation target Exception .How to resolve
Target attribute in anchor tag, Use of target attribute of anchor tag in HTML5.
Target attribute in anchor tag, Use of target attribute of anchor tag in HTML5. In this tutorial, we will see the use of target attribute of anchor tag in HTML5. Target attribute tells the browser where to display linked document
How to design a mouse
How to design a mouse Follow this example to learn how to design a mouse. New File: First take a new file (Ctrl + N). Use Pen Tool (P... mouse pointer then select Direct selection tool (A key) and adjust the points
Wats Deploying war in Target Server - Development process
Wats Deploying war in Target Server   Hi Friends, I am new to web application, can u explain briefly wat is "Deploying a .war,.jar,.ear files in target server. Thank u in advance   Hi rajendra
Flex target property example
Flex target property example   ... object property  target . We also have another event object property... target and currentTarget differ in their targeting jobs.  target property may
Adding two numbers
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
drop down menu
drop down menu  drop down menu using html
javascript adding a value to an array
javascript adding a value to an array  How to add a value to a function argument or an array in JavaScript
adding view to viewcontroller
adding view to viewcontroller  How to add a frame window that opens on button click .. in iPhone application
adding groups in contacts
adding groups in contacts  how to add groups in contacts using servlet and jsp????pls help.thanku in advance
Auto select cell on mouse hover using 'hover' mouse event
Auto select cell on mouse hover using 'hover' mouse event In this tutorial, we will discuss about how to highlight Table's cell on mouse hover using jQuery 'hover' mouse event. In this example, a table is given
Drop Table
Drop Table       Drop Table in SQL get rid of an object or table from the database. Using a drop Query... from database. The Drop table cannot get it back, once you run the drop Query
adding buttons - Swing AWT
adding buttons  can u plzz explain how to add button to a dialog box and make them perform some event on the image loaded on the panel
adding buttons - Swing AWT
adding buttons  can u plzz explain how to add button to a dialog box and make them perform some event on the image loaded on the panel
Adding photo to iPhone simulator
Adding photo to iPhone simulator  Hi, there is no photo in my iPhone simulator.. how can i add one? Please suggest. Thanks
Drop Index
Drop Index       Drop.... Understand with Example The Tutorial illustrate an example from Drop Index..._table(stu_id); Drop Index THE drop index stu_index 1 is used
How do i create the node for target SMO in java..???
How do i create the node for target SMO in java..???  How do i create the node for target SMO in java..??? or else whats the method for accessing the target SMO
drop down list in Struts
drop down list in Struts  How to create a drop down list in Struts
DROP a table and TRUNCATE
DROP a table and TRUNCATE   What are the differences between DROP a table and TRUNCATE a table
testcase for adding - JUNIT
testcase for adding  hi deepak...I am new to junit.can u send me a JUnit test case for adding or multiplying of two numbers.please i need it urgently
Advertisements
 

 

 

DMCA.com