473,232 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

KeyListener is not working after the window has been moved

Basicaly, I have a frame with only a Canvas on it and a KeyListener. For some reason the keyListener stops registering my keys as soon as I have moved the Window from its starting position. I have tried several solution but none have worked. Heres the code
Expand|Select|Wrap|Line Numbers
  1. public class Test extends Canvas implements KeyListener
  2. {
  4.     static final int     WIDTH      =700;
  5.     static final int     HEIGHT     =700;
  6.     static final String  TITLE      ="HelloWorld";
  9.     public static void main(String[]args)
  10.     {
  11.         Test test = new Test();
  12.     }
  14.     public test()
  15.     {
  16.         JFrame frame = new JFrame(TITLE);
  17.         JPanel panel = (JPanel)frame.getContentPane();
  18.         setBounds(0,0,WIDTH,HEIGHT);
  19.         panel.setPreferredSize(new Dimension(WIDTH,HEIGHT));
  20.         panel.setLayout(null);
  21.         panel.add(this);
  22.         frame.setBounds(0,0,WIDTH,HEIGHT);
  23.         setIgnoreRepaint(true);
  24.         frame.setResizable(false);
  25.         frame.pack();
  26.         frame.addWindowListener(new WindowAdapter(){
  27.             public void windowClosing(WindowEvent e){
  28.                 System.exit(0);
  29.             }
  30.         });
  31.         frame.addKeyListener(this);
  32.         frame.setVisible(true);
  33.         requestFocus();
  34.     }
  36.     public void keyPressed(KeyEvent event){
  37.         System.out.println("KeyPressed");
  38.     }
  39.     public void keyTyped(KeyEvent event){
  40.         System.out.println("KeyTpyed");
  41.     }
  42.     public void keyReleased(KeyEvent event){
  43.         System.out.println("KeyReleased");
  45.     }
  46. }

I have used this code on both Linux(Ubuntu) and Vista with the same problem on both operating systems. Any help?
Mar 21 '08 #1
1 2725
11,448 Expert 8TB
A Canvas is a 'heavy weight' AWT component, i.e. it leaves it to the underlying
system to draw itself. Swing components (such as JFrames, JPanels, everything
starting with a capital J) don't like that and get confused by such an AWT component.
Make your class extend from a JPanel and all will be fine.

kind regards,

Mar 21 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Catherine Lynn Smith | last post by:
OK, I know this issue has been beat to death, but I have tested my script in Netscape 7.1 and it does seem to be getting the calculated values it needs to create placement coordinates, and every...
by: Marshall Dudley | last post by:
When I do the following line in Netscape, the popup loads as it should, but the parent window usually, but not always, reloads as well. <a href="#"...
by: Dan | last post by:
Hello, I am just now converting from the world of DOS. Making simple VB.NET apps was easier than I expected. Now I need some control over the window that the app is running in. Can a VB.NET...
by: Ashhad Syed | last post by:
I have turned on smartnavigation=True for some of the forms in my .NET application. It seemed to be working fine on Windows 2000 SP4, which also has VS 2003 installted, but when I moved the...
by: ThunderMusic | last post by:
Hi, I'm developping a form that renders differently when hovered and trigger some processing when not hovered (leaved). The thing is, when I leave my form and another form from my application is...
by: Sura | last post by:
Hi I have a flash interactive window which has html links and this appears on an html page. This window can be moved with the mouse on the html page. The html page has an iFrame too. When the...
by: pankajs | last post by:
Hello freinds! I am developing a game & a problem occur with pressing the keys the class name is game it extends JPanel, when I register keylistener by writing addKeyListener(this); in the...
by: Humakt | last post by:
I've done KeyListener before successfully, but for some reason I can't get it to work in my recent project. Here is the code: View class (setBattleController is called upon creating new Battle...
by: mrjohn | last post by:
Hey, I'm trying to make a program that will recognize when certain keys are released, so that I can encorperate it into a game I'm making. Unfortunately, it doesn't seem to be working. When I run the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.