The Artima Developer Community
Sponsored Link

Java Answers Forum
a conceptual query about keyevent handling

8 replies on 1 page. Most recent reply: Oct 11, 2004 6:18 AM by gaurav

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
gaurav

Posts: 12
Nickname: goravtomar
Registered: Jul, 2004

a conceptual query about keyevent handling Posted: Oct 5, 2004 10:05 PM
Reply to this message Reply
Advertisement
iam not getting what is the mistake in the following code. i know i am missing a concept smewhere.
i am adding a panel in a frame and adding a keylistener to that panel, but whenever i presses the key the key events are not delievered to the panel, i dont know why?

public class Testing extends JPanel implements KeyListener{

public testing(){
setSize(800,600);
setLayout(null);
addKeyListener(this);
}

public static void main(String[] args){
JFrame frame=new JFrame();
frame.setSize(820,620);
testing test=new testing();
frame.getContentPane().add(test);
frame.validate();
frame.show();
}

public void keyPressed(KeyEvent e) {}

public void keyReleased(KeyEvent e) {
System.out.println("hi");
}

public void keyTyped(KeyEvent e) {}
}

the keyevents if generated are not handled by this listener.
--------------------------------------------------------------------- --------------------------------------------------------------------------
if i change the code slightly it works-----------
if instead of adding the keylistener to panel i add it to jframe it works.

public class testing extends JPanel implements KeyListener{

public testing(){
this.setSize(800,600);
this.setLayout(null);
}

public static void main(String[] args){
JFrame frame=new JFrame();
frame.setSize(820,620);
testing test=new testing();
frame.addKeyListener(test);
frame.getContentPane().add(test);
frame.validate();
frame.show();
}

public void keyPressed(KeyEvent e) {}

public void keyReleased(KeyEvent e) {
System.out.println("hi");
}

public void keyTyped(KeyEvent e) {}
}


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: a conceptual query about keyevent handling Posted: Oct 6, 2004 1:53 AM
Reply to this message Reply
This says you CAN add KeyListner to JPanel. Does this helps you?

http://leepoint.net/notes-java/30GUI/50keyboard/keyboard.html

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: a conceptual query about keyevent handling Posted: Oct 6, 2004 1:54 AM
Reply to this message Reply
Ok this should clear your doubt.

http://forum.java.sun.com/thread.jsp?forum=424&thread=548499&tstart=15&trange=15

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: a conceptual query about keyevent handling Posted: Oct 6, 2004 1:57 AM
Reply to this message Reply
import javax.swing.*;
import java.awt.event.*;
 
public class Testing
    extends JPanel
    implements KeyListener {
 
  public Testing() {
    setSize(800, 600);
    setFocusable(true);
    setLayout(null);
    addKeyListener(this);
  }
 
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(820, 620);
    Testing test = new Testing();
    frame.getContentPane().add(test);
    frame.validate();
    frame.show();
  }
 
  public void keyPressed(KeyEvent e) {}
 
  public void keyReleased(KeyEvent e) {
    System.out.println("hi");
  }
 
  public void keyTyped(KeyEvent e) {}
}
 
[java]

gaurav

Posts: 12
Nickname: goravtomar
Registered: Jul, 2004

Re: a conceptual query about keyevent handling Posted: Oct 6, 2004 9:52 PM
Reply to this message Reply
that was nice of u. it really helped, and solved my problem. i got confused bcoz in my version i was using the following code

package rough;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Testing extends JPanel implements KeyListener{


public Testing(){
setSize(800,600);
setLayout(null);
if(!this.isFocusable()){
setFocusable(true);
}
addKeyListener(this);
}

public static void main(String[] args){
JFrame frame=new JFrame();
frame.setSize(820,620);
Testing test=new Testing();
frame.getContentPane().add(test);
frame.validate();
test.requestFocus();
frame.show();
}

public void keyPressed(KeyEvent e) {}

public void keyReleased(KeyEvent e) {
System.out.println("hi");
}

public void keyTyped(KeyEvent e) {}
}
------------------------------------------------------
i was setting focus when it wasn't set and it was not wrking.
ur code really wrks, it may b a bug in java code.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: a conceptual query about keyevent handling Posted: Oct 7, 2004 1:00 AM
Reply to this message Reply
if(!this.isFocusable()){
 setFocusable(true);
}


I think you have misunderstood the isFocusable() method. The Java API documentation says

isFocusable() Returns whether this Component can be focused.

It doesn't says that this method returns whether the Component has the focus.

So instead you might want to use this code

    if (!this.isFocusOwner())
      setFocusable(true);

gaurav

Posts: 12
Nickname: goravtomar
Registered: Jul, 2004

Re: a conceptual query about keyevent handling Posted: Oct 8, 2004 2:37 AM
Reply to this message Reply
thanks 4 dat 2, i was actually in a hurry at that time and missed that. thanks once again.
can u plz tell me how do draw lines of variable widths as v can draw in vb and vc

by default the drawLine(x1,y1,x2,y2) method draws line of 1 pixel width, but i want to draw lines that r more thicker then 1 pixel.

Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: a conceptual query about keyevent handling Posted: Oct 10, 2004 7:22 PM
Reply to this message Reply
Sorry, I am not a Java GUI guy. I manage to help you with the previous question with few google searches and Java API. I hope someone else will answer that question.

gaurav

Posts: 12
Nickname: goravtomar
Registered: Jul, 2004

Re: a conceptual query about keyevent handling Posted: Oct 11, 2004 6:18 AM
Reply to this message Reply
thanks, the answer for that ques. is that u can do so by using BasicStroke class.

Flat View: This topic has 8 replies on 1 page
Topic: FAT filestore implementation in java Previous Topic   Next Topic Topic: Simple array equality question (hopefully!)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use