Don't use a KeyListener. Swing was designed to be used with Key Bindings.
Add a Key Binding to each button. Then when the letter is typed the button will be the source of the event so you can just remove the button from the panel.
For an example of how key bindings work see below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class CalculatorPanel extends JPanel
{
private JTextField display;
public CalculatorPanel()
{
Action numberAction = new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
// display.setCaretPosition( display.getDocument().getLength() );
display.replaceSelection(e.getActionCommand());
}
};
setLayout( new BorderLayout() );
display = new JTextField();
display.setEditable( false );
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout( new GridLayout(0, 5) );
add(buttonPanel, BorderLayout.CENTER);
for (int i = 0; i < 10; i++)
{
String text = String.valueOf(i);
JButton button = new JButton( text );
button.addActionListener( numberAction );
button.setBorder( new LineBorder(Color.BLACK) );
button.setPreferredSize( new Dimension(30, 30) );
buttonPanel.add( button );
InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke(text), text);
inputMap.put(KeyStroke.getKeyStroke("NUMPAD" + text), text);
button.getActionMap().put(text, numberAction);
}
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("Calculator Panel");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new CalculatorPanel() );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
So in your case the code in the actionPerformed() method of the Action would be something like:
JButton button = (JButton)e.getSource();
JPanel parent = (JPanel)button.getParent();
parent.remove(button);
parent.revalidate();
parent.repaint();
Edit:
When using a KeyListener you might use HashMap to bind the character to the related button:
HashMap<Character, JButton> buttons = new HashMap<Character, JButton>();
...
buttons.put('a', aButton);
buttons.put('b', bButton);
Then the keyTyped() code in the KeyListener code would be something like:
JButton button = buttons.get( e.getKeyChar() );
panel.remove( button );
panel.revalidate();
panel.repaint();