Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-06-2007, 09:44 PM
Member
 
Join Date: Jul 2007
Posts: 4
lost_in_java is on a distinguished road
keyListener isn't working for me
in an applet i made with a KeyListener and a MouseListener,
my keyListener will not respond to keys pressed.
the mouseListener works fine.
the keyTyped, keyPressed, and keyReleased methods just won't get called.
can someone please find my mistake?

Code:
public class Cursor extends JApplet implements MouseListener, KeyListener{ BooleanJLabel[][] grid = new BooleanJLabel[5][5]; Icon backgroundIcon = new ImageIcon(Cursor.class.getResource("images/background.gif")); Icon cursorIcon = new ImageIcon(Cursor.class.getResource("images/cursor.gif")); Icon exclamationIcon = new ImageIcon(Cursor.class.getResource("images/exclamation.gif")); Icon bothIcon = new ImageIcon(Cursor.class.getResource("images/both.gif")); int cursorX = 0; int cursorY = 0; public void init(){ setSize(250, 250); setLayout(new GridLayout(5,5)); for(int countY = 0; countY <5; countY++){ for(int countX = 0; countX <5; countX++){ BooleanJLabel background = new BooleanJLabel(backgroundIcon); background.addMouseListener(this); grid[countY][countX] = background; add(background); } } addKeyListener(this); } public void mouseClicked(MouseEvent m) { } public void mouseEntered(MouseEvent m) { int x = ((JLabel)m.getSource()).getX()/50; int y = ((JLabel)m.getSource()).getY()/50; cursorX = x; cursorY = y; if(grid[cursorY][cursorX].isActivated()){ grid[cursorY][cursorX].setIcon(bothIcon); }else{ grid[cursorY][cursorX].setIcon(cursorIcon); } } public void mouseExited(MouseEvent m) { int x = ((JLabel)m.getSource()).getX()/50; int y = ((JLabel)m.getSource()).getY()/50; if(grid[cursorY][cursorX].isActivated()){ grid[y][x].setIcon(exclamationIcon); }else{ grid[y][x].setIcon(backgroundIcon); } } public void mousePressed(MouseEvent m) { } public void mouseReleased(MouseEvent m) { if(m.getButton()==MouseEvent.BUTTON1){ grid[cursorY][cursorX].switchActivated(); if(grid[cursorY][cursorX].isActivated()){ grid[cursorY][cursorX].setIcon(bothIcon); }else{ grid[cursorY][cursorX].setIcon(cursorIcon); } } } public void keyPressed(KeyEvent k) { } public void keyReleased(KeyEvent k) { } public void keyTyped(KeyEvent k) { grid[4][4].setIcon(exclamationIcon); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-06-2007, 10:15 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
You are adding your mouse listener to BooleanJLabel. That means the mouse listener will only be enabled for the area of BooleanJLabel. Try clicking on BooleanJLabel, you should see that it is working. If it is not working, send us source code of BooleanJLabel class.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-06-2007, 10:20 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
It seems that i read the post too much quickly

Try putting System.out.println statements inside your keyPressed, keyReleased and keyTyped methods and open Java console to see the results. First we need to find if these methods are really called by the system or not since we cant be sure whether your setIcon(..) call changes something on the GUI.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-06-2007, 10:36 PM
Member
 
Join Date: Jul 2007
Posts: 4
lost_in_java is on a distinguished road
i know the statement
Code:
grid[4][4].setIcon(exclamationIcon);
works because i tested it in another area of the code.
i tried putting println but no response - the methods just arent getting called
i removed the BooleanJLabels and then i got a response after clicking the applet, so i think the problem is focus.
heres some more things i tried:
1. putting keyListeners on the labels themselves - didnt work - no idea why
2. calling requestFocus() in the applet - didnt work
3. making the applet bigger - worked after i clicked in the area not occupied by the labels - proving the problem is focus.
so why can't my labels take a keylistener? because i know they will have focus.
and why doesnt requestFocus() work?

oh heres the code to the BooleanJLabel class:
Code:
public class BooleanJLabel extends JLabel { private boolean activated; public BooleanJLabel(){ } public BooleanJLabel(String s){ super(s); } public BooleanJLabel(Icon i){ super(i); } public void activate(){ activated = true; } public void deactivate(){ activated = false; } public void switchActivated(){ activated = !activated; } public void setActivated(boolean b){ activated = b; } public boolean isActivated(){ return activated; } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-07-2007, 12:25 AM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
1. putting keyListeners on the labels themselves - didnt work - no idea why

I guess this is a focus issue.

2. calling requestFocus() in the applet - didnt work

If the applet does not have focus, it can't forward the focus to the jLabel. This might be what you saw.

From requestFocus method at JComponent (Java 2 Platform SE v1.4.2)

Quote:
Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window. This component must be displayable, visible, and focusable for the request to be granted. Every effort will be made to honor the request; however, in some cases it may be impossible to do so. Developers must never assume that this Component is the focus owner until this Component receives a FOCUS_GAINED event. If this request is denied because this Component's top-level Window cannot become the focused Window, the request will be remembered and will be granted when the Window is later focused by the use
So focus is not guaranteed.

Also check this thread. It has some useful information and can help you solve your problem:

Help with requestFocus
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-07-2007, 08:45 AM
Member
 
Join Date: Jul 2007
Posts: 4
lost_in_java is on a distinguished road
ok i figured out the problem.
by default, a JLabel is not focusable
i just did setFocusable(true) on all the JLabels and then put a keyListener on each one.
now it works fine.

thanks for the input.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 12-04-2008, 09:10 AM
Member
 
Join Date: Dec 2008
Posts: 1
Hitz_M is on a distinguished road
I am facing the same problem. I have set the setFocusable(true) but still the key listner doesnt work. The mouseListner works prefectly. Heres my code which creates and adds the labels to my dialog:

private JDialog checkoutDialog = null;
private JScrollPane scrollPane = null;
private JPanel centerPanel = null;
private JLabel thumbnailsLabel[] = null;
private JPanel thumbnailPanel = null;
private int thumbNailPosition = 0;


Vector thumbnailData = getThumbnailData();

thumbnailPanel = new JPanel(new GridBagLayout());
thumbnailPanel.setBackground(Color.WHITE);

thumbnailsLabel = new JLabel[thumbnailData.size()];
for(thumbNailPosition=0; thumbNailPosition<thumbnailData.size(); thumbNailPosition++)
{
layoutContraints.gridx = thumbNailPosition%(checkoutDialog.getSize().width / 200);
layoutContraints.gridy = GridBagConstraints.RELATIVE;
ImageIcon thumbnail = new ImageIcon ("../../Folder.jpg");
thumbnailsLabel[thumbNailPosition] = new JLabel(tempNode.getNodename(), thumbnail, JLabel.CENTER);
thumbnailsLabel[thumbNailPosition].setVerticalTextPosition(JLabel.BOTTOM);
thumbnailsLabel[thumbNailPosition].setHorizontalTextPosition(JLabel.CENTER);
thumbnailsLabel[thumbNailPosition].setPreferredSize(new Dimension(150,150));
thumbnailsLabel[thumbNailPosition].setText(thumbnailData.getNodename());
thumbnailsLabel[thumbNailPosition].setFocusable(true);
thumbnailsLabel[thumbNailPosition].addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt)
{

if(evt.getButton() == 1 && evt.getClickCount() == 2)
{
//Do something
}

}

});

thumbnailsLabel[thumbNailPosition].addKeyListener( new KeyAdapter(){
public void keyPressed (KeyEvent k)
{
//The execution never reaches here
System.out.println("in key listner");
if(k.getKeyCode() == KeyEvent.VK_ENTER)
{
//Do something
}
}

});

thumbnailPanel.add(thumbnailsLabel[thumbNailPosition], layoutContraints, thumbNailPosition);

}

scrollPane = new JScrollPane(thumbnailPanel,JScrollPane.VERTICAL_SC ROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR _AS_NEEDED);
scrollPane.setPreferredSize(new Dimension(450, 350));
centerPanel.add(scrollPane, BorderLayout.CENTER);
checkoutDialog.getContentPane().add(centerPanel, BorderLayout.CENTER);


Thanks,
Hitesh
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 12-05-2008, 06:24 AM
Fubarable's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 1,289
Fubarable is on a distinguished road
Don't use a keylistener here, but instead use key binding as these are higher level constructs and don't require direct focus. You can find an excellent tutorial on these over at the Sun Java tutorial site.

Good luck.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
calculator not working Renegade85 New To Java 5 03-10-2008 05:27 PM
sendredirect not working ranga Java Servlet 0 02-11-2008 12:48 PM
how to add a KeyListener leonard New To Java 1 08-06-2007 06:44 PM
Help with KeyListener in applet mathias Java Applets 1 08-06-2007 04:22 AM
Working With ANT JavaForums Eclipse 0 04-26-2007 10:16 PM


All times are GMT +3. The time now is 12:19 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org