How to receive key events during a drag&drop? - java

I'm currently trying to receive key events during a drag and drop, but it seems to me that the focus is taken away while dragging so that I can't listen to any key events.
I'm dragging a JComponent subclass that implements KeyListener and requests the focus in the DragSourceListener's dragEnter method, but my assumption is that the focus is taken away from it afterwards.
Now, who's got the focus and how can I take it away back to my JComponent. Or is there a different approach that is more suitable for dnd?
Thank you in advance.
UPDATE:
It's a lot of code necessary to make this work so I'm only going to post some snippets to show you what I'm trying to do:
public class Stone extends JComponent implements Serializable, KeyListener {
public Stone(...) {
//...
setFocusable(true);
addKeyListener(this);
this.dragSource = DragSource.getDefaultDragSource();
this.dgListener = new StoneDGListener();
this.dsListener = new StoneDSListener();
this.dragSource.createDefaultDragGestureRecognizer(
this,
DnDConstants.ACTION_MOVE,
this.dgListener
);
//...
}
//...
public void keyPressed(KeyEvent e) {
System.out.println("Stone: "+e.getKeyCode());
}
//...
public class StoneDSListener implements DragSourceListener, Serializable {
//...
#Override
public void dragEnter(DragSourceDragEvent dsde) {
//...
Stone.this.requestFocus();
addKeyListener(Stone.this);
}
//...
}
}
What happens is that before I'm dragging the Stone component my JPanel has the focus so it receives any keys I'm pressing.
During the drag I can't listen to any pressed keys(so I don't know who's got the focus) even though I'm requesting it when in dragEnter() and after I release the Stone any key events are send to the Stone.
It's probably not important for the question but to illustrate what I'm doing here's a screenshot:
image showing the "drag" http://img685.imageshack.us/img685/1884/pico.png
(Here I'm dragging the Stone from the collection below to the game field on the top). In this state I don't know how to find out what keys are pressed. I need to figure this out in order to be able to rotate the Stone.

Not sure who has focus during a drag and drop. But an alternative solution to your problem would be to add a KeyEventDispatcher for your Stone class to the KeyboardFocusManager. From the JavaDoc:
The KeyboardFocusManager is both a centralized location for client code to query for the focus owner and initiate focus changes, and an event dispatcher for all FocusEvents, WindowEvents related to focus, and KeyEvents+.
+ my emphasis.
Basically we use similar sort of code to intercept KeyEvents before they hit the Component that has focus.
Just gave it a quick test for your particular drag and drop context and it seems to work alright (as long as your application has focus within the operating system). Essentially something along the lines of:
Public Stone(...) {
// ...
KeyboardFocusManager fm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
fm.addKeyEventDispatcher(
new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e) {
System.out.println("Key Press: " + e.getKeyChar());
return false;
}
}
);
// ...
}
You will need to do a bit of leg-work on enabling and disabling when the user is no longer dragging and dropping as my test currently prints all the time.
I also wonder if it is possible to use the KeyboardFocusManager to determine who actually ends up with focus during a drag and drop?
Anyway, I hope this gives you a few new ideas to try.

Related

what is the correct handling of isPopupTrigger() in MouseAdapter

I need to write a Java Swing application that will run and act about the same on Linux, Windows and Mac. And for virtually all Widgets I create, I need to add some special handling, including, everything needs to be able to pop up a menu when asked. Even widgets like labels and buttons.
I'm able to catch menu requests by adding:
MouseListener m = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e)
{
if (e.isPopupTrigger())
{
onMenuSummons(); //do my thing
}
}
};
addMouseListener(m);
to the constructor on my extended widget.
But I'm concerned that this is a Linux-only solution. Do I only need to check isPopupEvent() in MousePressed? What about MouseClicked? I could hook both to be sure, but do I run a risk that some platform someday would have isPopupEvent() be true in both functions for the same mouse action?
Ideally MouseAdapter would spare me the question by offering an overridable popupRequest(MouseEvent e), but it doesn't. So what is the always right, works everywhere on all platforms and always will forever solution?
Also, some of the widgets I'm extending may have their own popup menus; I need to suppress those and implement my own. How do I make sure only my own menu is displayed? TIA.
Read the section from the Swing tutorial on Bringing up a Popup Menu for the basic of using menus and popup menus.
To write a MouseListener the basic code is:
class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
some of the widgets I'm extending may have their own popup menus; I need to suppress those and implement my own.
Also note the the example from the tutorial is older. The newer approach for adding a popup menu to a component is to use:
component.setComponentPopupMenu(....);
Not sure, but since this method only allows a single popup it may replace the default popup?

How do you get the current keyboard state globally? (i.e. what keys are currently depressed regardless of if the inquiring application has focus)

I'm writing a screen capture utility, and I'd like to be able to store the current state of the keyboard and mouse whenever a screenshot is taken.
Doing this for the mouse is simple, since using the PointerInfo class in a manner described in this related question gives you the screen coordinates for the current mouse location and the click information if desired. However, I haven't been able to find an analog to this class for the keyboard; all the keyboard related classes appear to be focus specific.
So, is there a way to get the current keyboard state in Java?
P.S. Remember that I'm looking for a function to call to retrieve the information regarding what keys are depressed, not a listener for such depression events.
Something you can do is implement the KeyListener interface and give states to all the keys you're interested in.
If you're interested in checking if the arrow keys are depressed upon a screenshot, for instance, you can implement this KeyListener interface and override keyPressed() and keyReleased() methods and set the state for those keys you are interested in to keyPressed or keyReleased. Depending on the event. That way, when the screenshot occurs, you can just read the state of those keys
If you need this solution to be global, regardless of application focus, you could write a small hook in C that you can integrate with Java Native Interface to listen for key events. Java doesn't let you listen to key events without you attaching the listener to a component and that component having focus. Have a look at JNativeHook.
If you just need it when your application has focus but on every component you could inelegantly attach the listener to all your components or you could write your own custom KeyEventDispatcher and register it on the KeyBoardFocusManager. That way, as long as your application has focus, regardless of component that has specific focus, you could catch all keyboard events. See:
public class YourFrame extends JFrame {
public YourFrame() {
// Finish all your layout and add your components
//
// Get the KeyboardFocusManager and register your custom dispatcher
KeyboardFocusManager m = KeyboardFocusManager.getCurrentKeyboardFocusManager();
m.addKeyEventDispatcher(new YourDispatcher());
}
private class YourDispatcher implements KeyEventDispatcher {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_TYPED) {
// Do something to change the state of the key
} else if (e.getID() == KeyEvent.KEY_PRESSED) {
// Do something else
}
return false;
}
}
public static void main(String[] args) {
YourFrame yF = new YourFrame();
yF.pack();
yF.setVisible(true);
}
}

a key listener that listen only special keys?

my big problem is that i am codding a game.i have 2 player in a jframe that they can play cuncurrent. first player play with arrow keys and second with w/a/s/d keys..my instrutor said me that to achieve cuncurrent play i should instantiate two thread that every thread manage its own special player..noe i am confuse that how i can have two thread for tow players that only diffrence between them is the key that they listen...
another question that maybe can help me is can i have a listener that listen only some special keys??for exampe i new a listener that only listen w/s/a/d buttuns??(i am familiar with keyevent.getkeycode but it is not my mean because i want a listener that never listen another keys this is listen all keys and in decision choose codder favorite key)
i will be infinitively greatfull if you help me.
You can define your own keys processors like this (mainFrame is JFrame instance)
ActionMap actions = ((JComponent)mainFrame.getContentPane()).getActionMap();
InputMap inputs = ((JComponent)mainFrame.getContentPane()).getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
Action a=new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//call your action code here
}
};
actions.put("myAction", a);
inputs.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK), "myAction");
before that don't forget to set Focus to the JPanel before listening KeyListener check KeyBindings for extended funcionalities, here is very usefull infos about Listeners in Swing
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
myPanel.grabFocus();
myPanel.requestFocus();//or requestFocusinWindow()
}
});
A key listener will listen to all keys. You just have to react to the ones you need. Since you can have any number of key listeners, there is no issue in having a key listener per player.
The keyListener listens to all keys pressed.
But inside the code you can check which key is pressed, if ignore it if it is not one of the keys that you want to react to. In fact, you could use the same class for both listeners and just pass the key that each of them has to react to as parameters.
Having said that, you should not need two threads for it. I understand two threads to avoid the GUI freezing (a thread for managing the GUI, another for doing the calculations of positions). Of course, being it homework, it can be asked this way just so get to learn the basic concepts of threading.
Ok so heres how i would do it, i would have 2 classes, one called game and the other player, game extends your JFrame and player implements your keyListener, In game you could have 2 instances of Player, one for the instance that uses the D-keys and the other would use asdw or whatever, heres an example:
public void keyPressed(KeyEvent e)
{
switch(playerNumber)
{
case PLAYER_1:
if(e.getKeyCode==VK_UP)
{
//do some code for an "up event"
}
if(e.getKeyCode==VK_DOWN)
{
//do some code for a "down event"
}
if(e.getKeyCode==VK_LEFT)
{
//do some code for a "left event"
}
if(e.getKeyCode==VK_RIGHT)
{
//do some code for a "right event"
}
break;
case PLAYER_2:
if(e.getKeyCode==VK_W)
{
//do some code for an "up event"
}
if(e.getKeyCode==VK_S)
{
//do some code for a "down event"
}
if(e.getKeyCode==VK_A)
{
//do some code for a "left event"
}
if(e.getKeyCode==VK_D)
{
//do some code for a "right event"
}
break;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public static final int PLAYER_1 = 0;
public static final int PLAYER_2 = 1;
all you would have to do is tell the player instance if its player1 or player2, which would be easy, if you need any more examples or anything let me know, ive made plenty of hotseat games (games where more than 1 person are playing on 1 computer).

Attach to Window events from JPanel

I'm trying to listen for a window close event on the parent JFrame of a JPanel. In the WindowClosing event I'd like to de-register a listener to a different component.
Unfortunately the only code I can gaurantee to have run is the constructor for the panel. What this means is that the panel itself doesn't have an ancestor window yet, so simply calling SwingUtilities.getWindowAncestor doesn't work. So what I do is register a hierarchy listener, and in the hierarchyChanged event look for SHOWING_CHANGED event. When that even fires, now I can look for the window ancestor of the panel.
So basically I have the following:
class ShapeControlPanel extends JPanel{
public ShapeControlPanel(){
final ShapeControlPanel me = this;
me.addHierarchyListener(new HierarchyListener() {
#Override
public void hierarchyChanged(HierarchyEvent e) {
if((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED){
SwingUtilities.getWindowAncestor(me).addWindowListener(new WindowListener() {
/* Snipped some empty handlers */
#Override
public void windowClosing(WindowEvent e) {
/* Finally get to remove the handler. */
me.getApparent().removeLocationSelectionListener(me.GUID(), me);
}
});
}
}
});
}
}
Is this sane? Is there a more reasonable way of getting a handle on the frame closing event?
It's not the ugliest thing I've seen (I wouldn't even say it's all that bad), but you have to ask yourself: why does your panel really need to know when the window is closed? It seems to be an odd coupling that would best be removed.
I don't know enough about your context and what you are truly trying to accomplish to suggest an alternative right now. But if a panel needs to know about the container in which it resides, there is probably some bad design with harmful coupling.

MouseListener fired without checking JCheckBox

This one is pretty crazy:
I've got an AppSight recording (for those not familiar, it's a recording of what they did including keyboard/mouse input + network traffic, etc) of a customer reproducing a bug. Basically, we've got a series of items listed on the screen with JCheckBox-es down the left side. We've got a MouseListener set for the JPanel that looks something like this:
private MouseAdapter createMouseListener() {
return new MouseAdapter(){
public void mousePressed( MouseEvent e ) {
if( e.getComponent() instanceof JCheckBox ) {
// Do stuff
}
}
};
}
Based on the recording, it appears very strongly that they click just above one of the checkboxes. After that, it's my belief that this listener fired and the "Do stuff" block happened. However, it did NOT check the box. The user then saw that the box was unchecked, so they clicked on it. This caused the "Do stuff" block to fire again, thus undoing what it had done the first time. This time, the box was checked. Therefore, the user thinks that the box is checked, and it looks like it is, but our client thinks that the box is unchecked as it was clicked twice.
Is this possible at all? For the life of me, I can't reproduce it or see how it could be possible, but based on the recording and the data the client sent to the server, I can't see any other logical explanation.
Any help, thoughts, and or ideas would be much appreciated.
Maybe the user clicked on the checkbox, but before release the mouse button, he moved the mouse away from the component. I'm pretty sure the chechbox won't get checked in this case, although not so sure about the Event Thread behaviour under this scenario.
I don't think you can assume that because the mouse was pressed on the checkbox that it will be also released on the checkbox. Maybe just do something like this:
private MouseAdapter createMouseListener() {
return new MouseAdapter(){
public void mouseReleased( MouseEvent e ) {
if( e.getComponent() instanceof JCheckBox ) {
// And just to be sure....
JCheckBox jcb = (JCheckBox) e.getComponent();
if(jcb.isSelected())
{
// Do stuff
}
}
}
};
}

Categories