Results 1 to 8 of 8
  1. #1
    lost_in_java is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default 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?

    Java 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);
    	}
    }

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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.

  3. #3
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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.

  4. #4
    lost_in_java is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    i know the statement
    Java 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:
    Java 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;
    	}
    }

  5. #5
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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)

    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

  6. #6
    lost_in_java is offline Member
    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Default

    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.

  7. #7
    Hitz_M is offline Member
    Join Date
    Dec 2008
    Posts
    1
    Rep Power
    0

    Default

    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

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

Similar Threads

  1. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 03:27 PM
  2. sendredirect not working
    By ranga in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2008, 10:48 AM
  3. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 PM
  4. Help with KeyListener in applet
    By mathias in forum Java Applets
    Replies: 1
    Last Post: 08-06-2007, 02:22 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •