 |
|
 |
Hi Friends,
I am facing one typical problem with listboxes in asp.net 2.0.
That is i want to drag items from one listbox to another listbox and viceversa. That is strictly on asp.net 2.0 with c# . Anybody can help me please.
Thankyou to one and all.
Regds,
Hari.
|
|
|
|
 |
|
 |
Hi Srinu
Very Nice Work
With Regards
Kumar
|
|
|
|
 |
|
 |
i am getting java.awt.dnd.InvalidDnDOperationException,when i drag objects very fast.Can this be resolved.
|
|
|
|
 |
|
 |
Notice the problem below. Btw, this was run on WinXP using Cygwin. ================================================================== $ which javac /cygdrive/c/j2sdk1.4.2_08/bin/javac
$ javac ListDemo.java Note: ListDemo.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details.
$ javac -deprecation ListDemo.java ListDemo.java:204: warning: plainTextFlavor in java.awt.datatransfer.DataFlavor has been deprecated DataFlavor.plainTextFlavor}; ^ 1 warning
$ java ListDemo Exception in thread "main" java.lang.NullPointerException at ListDemo.valueChanged(ListDemo.java:64) at javax.swing.JList.fireSelectionValueChanged(Unknown Source) at javax.swing.JList$ListSelectionHandler.valueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source) at javax.swing.DefaultListSelectionModel.insertIndexInterval(Unknown Source) at javax.swing.plaf.basic.BasicListUI$ListDataHandler.intervalAdded(Unknown Source) at javax.swing.AbstractListModel.fireIntervalAdded(Unknown Source) at javax.swing.DefaultListModel.addElement(Unknown Source) at ListDemo.<init>(ListDemo.java:39) at ListDemo.main(ListDemo.java:241)
|
|
|
|
 |
|
 |
I just found that another thread post solves the problem. Look here:
http://www.codeproject.com/java/dnd.asp?msg=943020#xx943020xx
|
|
|
|
 |
|
 |
:-DThis helped me to understand a lot of things about drag and drop that I could not find anywhere else. It will allow me to effectively drag references to file objects out of a jtable, Something I was looking for help on for quite a while.. kudos to you!!
|
|
|
|
 |
|
 |
I have iPod uploader program in java, so I can just add files to ipod in the way as the article says. However, sometime i need copy files back from iPod to Explorer. How can I do that?
|
|
|
|
 |
|
 |
* Create a temporary file containing the data to be transerfered to the OS
* use DataFlavor.javaFileListFlavor as Flavour.
* return a List containing the file(s) to DnD
-- Dirk
|
|
|
|
 |
|
 |
I am getting the following exception while running the program:
Exception in thread "main" java.lang.NullPointerException
at ListDemo.valueChanged(ListDemo.java:64)
Demo.java:241) ----- snipped ---
I am using JDK 1.4.2
|
|
|
|
 |
|
 |
I'm getting the same problem.
Actually it seems to happen with the 1.4.2 JDK;
Does anyone know how to modify this code to make it work properly (I tryed the tips given in the "null pointer exception" comment topic, but it didn't worked!
|
|
|
|
 |
|
 |
i had the same problem. but if have sloved it:
here is the going version with jdk 1.4.2:
import java.awt.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class ListDemo extends JFrame
implements ListSelectionListener
{
private DroppableList list;
private JTextField fileName;
public ListDemo()
{
super("ListDemo");
fileName = new JTextField(50);
//Create the list and put it in a scroll pane
list = new DroppableList();
DefaultListModel listModel = (DefaultListModel)list.getModel();
list.setCellRenderer(new CustomCellRenderer());
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
list.addListSelectionListener(this);
JScrollPane listScrollPane = new JScrollPane(list);
String dirName = new String("\\");
String filelist[] = new File(dirName).list();
for (int i=0; i < filelist.length ; i++ )
{
String thisFileSt = dirName+filelist[i];
File thisFile = new File(thisFileSt);
if (thisFile.isDirectory())
continue;
try {
listModel.addElement(makeNode(thisFile.getName(),
thisFile.toURL().toString(),
thisFile.getAbsolutePath()));
} catch (java.net.MalformedURLException e){
}
}
String name = listModel.getElementAt(
list.getSelectedIndex()-1).toString();
fileName.setText(name);
//Create a panel that uses FlowLayout (the default).
JPanel buttonPane = new JPanel();
buttonPane.add(fileName);
Container contentPane = getContentPane();
contentPane.add(listScrollPane, BorderLayout.CENTER);
contentPane.add(buttonPane, BorderLayout.NORTH);
}
public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting() == false)
{
fileName.setText("");
if (list.getSelectedIndex() != -1)
{
//String name = list.getSelectedValue().toString();
String name = ((DefaultListModel)list.getModel()).getElementAt(list.getSelectedIndex()-1).toString();
fileName.setText(name);
}
}
}
private static Hashtable makeNode(String name,
String url, String strPath)
{
Hashtable hashtable = new Hashtable();
hashtable.put("name", name);
hashtable.put("url", url);
hashtable.put("path", strPath);
return hashtable;
}
public class DroppableList extends JList
implements DropTargetListener, DragSourceListener, DragGestureListener
{
DropTarget dropTarget = new DropTarget (this, this);
DragSource dragSource = DragSource.getDefaultDragSource();
public DroppableList()
{
dragSource.createDefaultDragGestureRecognizer(
this, DnDConstants.ACTION_COPY_OR_MOVE, this);
setModel(new DefaultListModel());
}
public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent){}
public void dragEnter(DragSourceDragEvent DragSourceDragEvent){}
public void dragExit(DragSourceEvent DragSourceEvent){}
public void dragOver(DragSourceDragEvent DragSourceDragEvent){}
public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent){}
public void dragEnter (DropTargetDragEvent dropTargetDragEvent)
{
dropTargetDragEvent.acceptDrag (DnDConstants.ACTION_COPY_OR_MOVE);
}
public void dragExit (DropTargetEvent dropTargetEvent) {}
public void dragOver (DropTargetDragEvent dropTargetDragEvent) {}
public void dropActionChanged (DropTargetDragEvent dropTargetDragEvent){}
public synchronized void drop (DropTargetDropEvent dropTargetDropEvent)
{
try
{
Transferable tr = dropTargetDropEvent.getTransferable();
if (tr.isDataFlavorSupported (DataFlavor.javaFileListFlavor))
{
dropTargetDropEvent.acceptDrop (
DnDConstants.ACTION_COPY_OR_MOVE);
java.util.List fileList = (java.util.List)
tr.getTransferData(DataFlavor.javaFileListFlavor);
Iterator iterator = fileList.iterator();
while (iterator.hasNext())
{
File file = (File)iterator.next();
Hashtable hashtable = new Hashtable();
hashtable.put("name",file.getName());
hashtable.put("url",file.toURL().toString());
hashtable.put("path",file.getAbsolutePath());
((DefaultListModel)getModel()).addElement(hashtable);
}
dropTargetDropEvent.getDropTargetContext().dropComplete(true);
} else {
System.err.println ("Rejected");
dropTargetDropEvent.rejectDrop();
}
} catch (IOException io) {
io.printStackTrace();
dropTargetDropEvent.rejectDrop();
} catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
dropTargetDropEvent.rejectDrop();
}
}
public void dragGestureRecognized(DragGestureEvent dragGestureEvent)
{
if (getSelectedIndex() == -1)
return;
Object obj = getSelectedValue();
if (obj == null) {
// Nothing selected, nothing to drag
System.out.println ("Nothing selected - beep");
getToolkit().beep();
} else {
Hashtable table = (Hashtable)obj;
FileSelection transferable =
new FileSelection(new File((String)table.get("path")));
dragGestureEvent.startDrag(
DragSource.DefaultCopyDrop,
transferable,
this);
}
}
}
public class CustomCellRenderer implements ListCellRenderer
{
DefaultListCellRenderer listCellRenderer =
new DefaultListCellRenderer();
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean selected, boolean hasFocus)
{
listCellRenderer.getListCellRendererComponent(
list, value, index, selected, hasFocus);
listCellRenderer.setText(getValueString(value));
return listCellRenderer;
}
private String getValueString(Object value)
{
String returnString = "null";
if (value != null) {
if (value instanceof Hashtable) {
Hashtable h = (Hashtable)value;
String name = (String)h.get("name");
String url = (String)h.get("url");
returnString = name + " ==> " + url;
} else {
returnString = "X: " + value.toString();
}
}
return returnString;
}
}
public class FileSelection extends Vector implements Transferable
{
final static int FILE = 0;
final static int STRING = 1;
final static int PLAIN = 2;
DataFlavor flavors[] = {DataFlavor.javaFileListFlavor,
DataFlavor.stringFlavor,
DataFlavor.plainTextFlavor};
public FileSelection(File file)
{
addElement(file);
}
/* Returns the array of flavors in which it can provide the data. */
public synchronized DataFlavor[] getTransferDataFlavors() {
return flavors;
}
/* Returns whether the requested flavor is supported by this object. */
public boolean isDataFlavorSupported(DataFlavor flavor) {
boolean b = false;
b |=flavor.equals(flavors[FILE]);
b |= flavor.equals(flavors[STRING]);
b |= flavor.equals(flavors[PLAIN]);
return (b);
}
/**
* If the data was requested in the "java.lang.String" flavor,
* return the String representing the selection.
*/
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if (flavor.equals(flavors[FILE])) {
return this;
} else if (flavor.equals(flavors[PLAIN])) {
return new StringReader(((File)elementAt(0)).getAbsolutePath());
} else if (flavor.equals(flavors[STRING])) {
return((File)elementAt(0)).getAbsolutePath();
} else {
throw new UnsupportedFlavorException(flavor);
}
}
}
public static void main(String s[])
{
JFrame frame = new ListDemo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
|
|
|
|
 |
|
 |
I checked your code and I have found an error with the selection of items in the list.
So I made these changes:
public void valueChanged(ListSelectionEvent e)
{
if (e.getValueIsAdjusting() == false)
{
fileName.setText("");
if (list.getSelectedIndex() != -1 &&
list.getSelectedIndex()
|
|
|
|
 |
|
 |
Hi I am getting an array index out of bounds from the getElementAt
I tried playing with this code but I haven't been able to make it work. was there supposed to be somthing after (list.getSelectedIndex() there is ther ";)" which I assume was ";" ")" but having that there that doesn't make any sense to put that in. I am missing somthing or is there somthing different in 1.5?
so this is what I have now...
String name = ((DefaultListModel)list.getModel()).getElementAt(list.getSelectedIndex()).toString();
java ListDemo
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.DefaultListModel.getElementAt(Unknown Source)
at ListDemo.(ListDemo.java:51)
at ListDemo.main(ListDemo.java:248)
|
|
|
|
 |
|
 |
I got the same problem with 1.5 until I took the line from the original file that says "list.setSelectedIndex(0);" and moved it to just after the line that says "fileName = new JTextField(50);"
I guess the order of events has changed a little bit with the new version of Swing.
|
|
|
|
 |
|
 |
Oh I changed somthign else to make it work. But I ended up re writing most of it anyways for the project I was working on. Next up I am writing a droppable JTree. yay!
|
|
|
|
 |
|
 |
please give java source code for projects
|
|
|
|
 |
|
 |
Its a really good article... thanks dude.
|
|
|
|
 |
|
 |
Just what the doctor ordered.
Barry
|
|
|
|
 |
|
 |
I needed it for JTree and my own classes. It helped me understand the DataFlavors better also.
|
|
|
|
 |
|
 |
a very needed one
Thak ya
starlight_beta
|
|
|
|
 |
|
 |
Exception in thread "main" java.lang.NullPointerException
at ListDemo.(ListDemo.java:32)
at ListDemo.main(ListDemo.java:241)
This is what happens when trying to run...
|
|
|
|
 |
|
 |
To avoid null pointer exception I had to do the following:
String dirName = new String("C:\\Access\\hold\\"); Where you put your local directory name.
and
NOTICE HOW I COMMENTED OUT THE BELOW CODE.
public void valueChanged(ListSelectionEvent e)
{
/*
if (e.getValueIsAdjusting() == false)
{
fileName.setText("");
if (list.getSelectedIndex() != -1)
{
String name = listModel.getElementAt((
list.getSelectedIndex()) -1).toString();
fileName.setText(name);
}
}
*/
}
After doing the above it ran fine, without null pointers.
|
|
|
|
 |
|
 |
actually it needs to be list.getSelectedIndex() -1 instead of list.getSelectedIndex()
|
|
|
|
 |
|
 |
I got the same problem as you did... when using JDK1.4. This program runs fine with JDK1.3.1.
Just recompile and run the code with JDK1.3.1 (no code changes would be required for the given code to run )
|
|
|
|
 |
|
 |
Very good. Thanks for your work. Actually I need to Drag and Drop between JTree and Windows Explorer. Your java
code made the things so easy.
Thanks
|
|
|
|
 |