Drag&Drop with swing - java

I need some help. Is it possible to simulate a drag & drop without registering a component?
E.g. I click the mousekey anywhere on the window and hold the mousekey down, at this moment, I want to create or simulate a DragSourceEvent programmatically with Java.
Is this possible?
Update:
Regarding Bob's reply, at least I got it, I can create a listener for the drag & drop:
DragSource dragSource = new DragSource();
DragGestureListener listener = new DragGestureListener() {
public void dragGestureRecognized(DragGestureEvent event) {
event.startDrag (null, strSel) ;
...
}
}
listener.dragGestureRecognized(new DragGestureEvent(
new DragGestureRecognizer(dragSource, component) {
}, DnDConstants.ACTION_COPY, new Point(0,0), events ));
but unfortunately i get this exception:
java.lang.IllegalArgumentException:
source actions at
java.awt.dnd.DragSourceContext.(DragSourceContext.java:169)
at
java.awt.dnd.DragSource.createDragSourceContext(DragSource.java:454)
at
java.awt.dnd.DragSource.startDrag(DragSource.java:293)
at
java.awt.dnd.DragSource.startDrag(DragSource.java:403)
at
java.awt.dnd.DragGestureEvent.startDrag(DragGestureEvent.java:203)
any suggestions?

The question you asked:
I haven't tried it, but in theory you should be able to create the Event object and get a handle on the Swing Event Queue from one of the system classes. However without having a valid component, there may be problems when methods try to work with the event.
What you probably meant:
Registering events for a standard window -- you should be able to set up drag and drop support for an empty JPanel or JFrame, but it'll take some hacking. Drag & Drop is a pain to work with at this level when not built in -- I suggest using something like an invisible component or something instead.

Related

Netbeans adding actions to output window

I have a netbeans platform application,
The Output window consists of two tabs in which am able to embed one of the tab in to a separate component with the following reference,
http://wiki.netbeans.org/DevFaqOWTabEmbedding
However, when i tried adding actions to it , the actions are not coming up in UI .
IOContainer ioc = IOContainer.create(new IOC());
InputOutput io = IOProvider.getDefault().getIO("test",
new Action[]{
new OneAction(),
new TwoAction(),
new ThreeAction()},ioc);
am expecting the three actions to be shown in the newly created window.
Something like this,
But am getting only the white window.
However, if i don't embed the tab in to another component, the actions appear.
Any help?
IOC has empty implementation of IOContainer.Provider method
setToolbarActions(JComponent comp, Action[] toolbarActions)
you should add JToolbar in IOC and implement that method.

Disable calendar event moving - Vaadin

I want to disable moving events from Vaadin Calendar
All of these handlers are automatically set when creating a new Calendar. If you wish to disable some of the default functionality, you can simply set the corresponding handler to null. This will prevent the functionality from ever appearing on the user interface. For example, if you set the EventMoveHandler to null, the user will be unable to move events in the browser. --> Book of Vaadin
I tried:
calendar.setHandler(null);
calendar.setHandler((EventMoveHandler) null);
calendar.setHandler((BaseEventMoveHandler) null);
EventMoveHandler handler = null;
calendar.setHandler(handler);
BaseEventMoveHandler baseHandler = null;
calendar.setHandler(baseHandler );
But nothing is working. Any suggestion....?
It really works for me with Vaadin 7.4.5:
calendar.setHandler((EventMoveHandler)null);
calendar.setHandler((EventResizeHandler)null);
If you want to disable all handlers and make the calendar completely read-only, this worked for me.
calendar.setReadOnly(true);
EDIT1:
This approach however doesn't disable resize pointers for the events. Thus, the best solution I see is to use it together with asmellado's answer :
calendar.setReadOnly(true);
calendar.setHandler((EventMoveHandler)null);
calendar.setHandler((EventResizeHandler)null);
Also, if I did not use the setReadOnly method, I was running in some strage client-side exceptions when I tried to move the event.

Validate CTabFolder before switching tabs

In a CTabFolder, I'd like to check the content for unsaved data before the user can switch from one tab to another. SWT does not provide a PreSelection event, as stated here.
I found a workaround, suggesting to switch back to the old tab when a selection is triggered, validate the data and then perform the desired switch again, if data is valid.
I do understand the general idea of this workaround, however, it is not working for me. oldPageIndex and newPageIndex do always have the same value, though I did not click on the same tab.
this.tabContainer.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
int oldPageIndex = tabContainer.getSelectionIndex();
int newPageIndex = tabContainer.indexOf((CTabItem)event.item);
// Here: oldPageIndex == newPageIndex
...
}
});
Is this workaround still working or is there anything I could possibly be doing wrong? Or maybe, has there been any fix for a real PreSelection event in the meantime? I tried using event.doit, but the SelectionEvent is fired, when the tabs have been switched already.
You can use the selection listener but as you have found the getSelectionIndex() does not give you the old tab. So you will have to maintain the old tab index yourself.
This is the technique used by the Eclipse FormEditor.

Handle keypress in java (globally)

I tried the usual way to handle keypress: implements KeyListener and override keyPressed(). But there are many swing components, and if Im on a combobox, the combobox "thinks" the key is for him. I want to the main app capture the keypress, how to?
You are looking for this Key Bindings
You could use:
KeyEventPostProcessor kepp = new KeyEventPostProcessor() {
#Override
boolean postProcessKeyEvent(KeyEvent e) {
// handle key event globally
}
};
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventPostProcessor(kepp);
This is way harder than you'd think. For example, here's the effort you have to put in to get this working on a Panel with a JFileChooser in it. That's a similar question I asked in the past. I think that will help you solve your problem. You should read both answers given as they're both valid.
To summarize, you've gotta iterate all the InputMaps and the parents of the InputMaps and clear out the keys you want to globally use. Then apply your action to the KeyMap.
You'll need this:
private static void clearInputMap(InputMap inputMap) {
inputMap.clear();
while ((inputMap = inputMap.getParent()) != null) {
inputMap.clear();
}
}

Text Missing On Checkbox Component for Swing After Adding Action

I'm writing a simple Swing app. I tried adding a checkbox as listed below. Once I added the actionHandler loadPickers the name Foo disappeared from where it was sitting next to the right of chckbxNewCheckBox. I tried adding a call to setHideActionText(), but now nothing displays.
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
If I change it to this it works properly. I see the text "Foo".
JCheckBox chckbxNewCheckBox = new JCheckBox("Foo");
chckbxNewCheckBox.setToolTipText("");
chckbxNewCheckBox.setName("");
chckbxNewCheckBox.setHideActionText(true);
chckbxNewCheckBox.setAction(loadPickers);
chckbxNewCheckBox.setText("Foo"); //THIS DOES NOT WORK IF IT COMES BEFORE SET ACTION
mainPanel.add(chckbxNewCheckBox, "flowy,cell 0 1");
I've included the action here for completeness. Why does it work this way? Am I missing something here? Currently I'm using the WindowBuilder plugin for Eclipse with the Mig layout system (which I really like). Unfortunately I haven't figure out if there's a way to make WindowBuilder use the .setText() method instead of using the constructor. Any help on what I'm doing wrong, any insight on why this behavior exists like this, or a good workaround for WindowBuilder would be great.
private class LoadPickers extends AbstractAction {
public LoadPickers() {
//putValue(NAME, "SwingAction_2");
putValue(SHORT_DESCRIPTION, "Some short description");
}
public void actionPerformed(ActionEvent e) {
}
}
As explained in the JavaDoc of AbstractButton.setAction:
Setting the Action results in immediately changing all the properties described in Swing Components Supporting Action. Subsequently, the button's properties are automatically updated as the Action's properties change.
So all the following properties can be impacted by setting an action:
enabled
toolTipText
actionCommand
mnemonic
text
displayedMnemonicIndex
icon (NA for JCheckBox)
accelerator (NA for JCheckBox)
selected

Categories

Resources