473,320 Members | 1,883 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,320 software developers and data experts.

KeyListener is not working after the window has been moved

twitch3729
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. {
  3.  
  4.     static final int     WIDTH      =700;
  5.     static final int     HEIGHT     =700;
  6.     static final String  TITLE      ="HelloWorld";
  7.  
  8.  
  9.     public static void main(String[]args)
  10.     {
  11.         Test test = new Test();
  12.     }
  13.  
  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.     }
  35.  
  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");
  44.  
  45.     }
  46. }
  47.  

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 2751
JosAH
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,

Jos
Mar 21 '08 #2

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

Similar topics

1
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...
10
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="#"...
2
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...
3
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...
4
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...
1
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...
4
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...
3
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...
2
mrjohn
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
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...
1
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.