Results 1 to 3 of 3
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default keyListener how to modify object which called it?

    Hi im using keylistener for my buttons but i cant work out how to determin inside the keylistener, which button is being called.
    because once i know which button is being called i wish to change its background.


    PHP Code:
            
               for (int i=0; i < colorShow.length;i++){
                colorShow[i] = new JButton();
                colorShow[i].setPreferredSize(new Dimension(20,20));
                colorShow[i].setBorder(BorderFactory.createLineBorder(Color.black));
                colorShow[i].addKeyListener(this);
                dataPanel.add(colorShow[i]);
    
            }

    PHP Code:
        
    @Override
       public void keyPressed( KeyEvent e ) {}
       @Override
       public void keyReleased( KeyEvent e ) { }
       @Override
       public void keyTyped( KeyEvent e ) {
    
         //code here to change background when any key is pressed
          }
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    figured it out

    e.getComponent().setBackground(Color.red);
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    4

    Default

    Someone in this forum told me before that keylistners is not the correct approch to set shortcut
    keys since key lisnteners are use in AWT before Swing exist. And she advised to use KeyBinding instead.

Similar Threads

  1. How do I modify the values in an object array?
    By ConMan in forum New To Java
    Replies: 1
    Last Post: 06-02-2010, 11:26 PM
  2. Help Using KeyListener to Move an Object
    By jjonesmu in forum New To Java
    Replies: 3
    Last Post: 04-20-2010, 10:35 PM
  3. KeyListener - Is this what I need?
    By dbashby in forum New To Java
    Replies: 26
    Last Post: 04-18-2009, 04:14 PM
  4. Replies: 1
    Last Post: 07-26-2008, 05:26 PM
  5. how to add a KeyListener
    By leonard in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:44 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
  •