Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38017 > unrolled thread
| Started by | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| First post | 2018-02-09 11:51 -0800 |
| Last post | 2018-02-12 08:56 -0800 |
| Articles | 3 — 1 participant |
Back to article view | Back to comp.lang.java.programmer
How to listen for drag/drop events? Eric Douglas <e.d.programmer@gmail.com> - 2018-02-09 11:51 -0800
Re: How to listen for drag/drop events? Eric Douglas <e.d.programmer@gmail.com> - 2018-02-09 13:44 -0800
Re: How to listen for drag/drop events? Eric Douglas <e.d.programmer@gmail.com> - 2018-02-12 08:56 -0800
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-02-09 11:51 -0800 |
| Subject | How to listen for drag/drop events? |
| Message-ID | <9b73b344-ccfd-4046-90ba-b0980bb1a8c6@googlegroups.com> |
I created a custom object which extends JTextField.
By default this does not allow dragging text from the control.
I add setDragEnabled(true); in my constructor and it gets default drag and drop operations.
After that, I call getTransferHandler() and it shows a default TransferHandler has been created, of class javax.swing.plaf.basic.BasicTextUI$TextTransferHandler
So, I would rather not have to reinvent the wheel by setting a custom TransferHandler. This basic TransferHandler has some automatic processing already coded in. If I start a drag operation and hover over the control, it shows a cursor in the control with the drop location. If I complete a drag move event from the control it automatically removes the text. I don't want to copy/paste or write from scratch to reproduce the logic that works. I just want to fire a listener event telling me when something has been added to or removed from my control by drag/drop.
I don't want to have to use DocumentListener to listen for drag/drop as this picks up everything and I'm already listening for the focus gain/lost and keypress events. I want to identify drag/drop as unique events.
I tried adding a DropTargetListener as follows but this removes that default logic, when I hover the control with the drag event it doesn't show the cursor in the drop field, and doesn't remove text from the drag field on drag move.. This does get the dropped text but not the dragged text..
new DropTarget(this, new DropTargetListener() {
@Override
public void dragEnter(DropTargetDragEvent dtde) {
}
@Override
public void dragOver(DropTargetDragEvent dtde) {
}
@Override
public void dropActionChanged(DropTargetDragEvent dtde) {
}
@Override
public void dragExit(DropTargetEvent dte) {
}
@Override
public void drop(DropTargetDropEvent dtde) {
dtde.acceptDrop(dtde.getDropAction());
try {
System.out.println((String) dtde.getTransferable().getTransferData(DataFlavor.stringFlavor));
focus();
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
}
});
I can't find anything on this short of creating a new custom TransferHandler, when all I want is notify events with that default behavior.
[toc] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-02-09 13:44 -0800 |
| Message-ID | <65e4e9fb-4b25-4c58-bc6f-ebf6b7ca9e3b@googlegroups.com> |
| In reply to | #38017 |
On Friday, February 9, 2018 at 2:51:20 PM UTC-5, Eric Douglas wrote: > > I can't find anything on this short of creating a new custom TransferHandler, when all I want is notify events with that default behavior. I do need my JTextField to sometimes notify me only when my value changes between focusGain and focusLost, and sometimes notify me of all changes. I did set up the all changes to track on keyTyped events, though I notice that also picks up Enter and Tab which don't change the value. I could set up the key events and the drag/drop events using only the DocumentListener, but the stupid drop event is only sending it one character at a time, so if I drop in 4 characters I get 4 events when I only need one. The only workaround I can think of is to delay all document change events with some kind of Timer, to collect them and notify the change after not receiving any events for a few milliseconds. It sounds dumb to notify on every character when I'm dropping in multiple characters, when I'm using the change notification to send text out to filter another control.
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2018-02-12 08:56 -0800 |
| Message-ID | <c71ae87e-39e3-4474-9cd2-2559fda1765f@googlegroups.com> |
| In reply to | #38018 |
I believe I've found a suitable workaround. I have a custom DocumentFilter on my control. I override insertString which is accepting all text from a drag/drop at once, but I process each character in a loop to determine if, where, and how it should be allowed in the control using custom input masks. This method passes in a DocumentFilter.FilterBypass which I use to update what is currently displayed in the control, which is reporting each update to the DocumentListeners one character at a time. At the end of the insertString and remove methods for each of my filters, I call a setText() method which puts the full updated display String in a custom field in one abstract filter class, the super class for all 3 filters. I can report the "key change events" here and it gives me one event for every keypress or drag/drop which changes the control's value.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web