Problem with Focus and KeyListener

Hi !!!

I have a problem. I created game Space Invaders, and it's working fine. But I created a new Form and put a JButton int it. I want when I press button to open/start the game. And that's the problem.... The game starts, but it won't take commands from keyboard, like it has lost KeyListener I think it's something with Focus, I tried setFocusable(), requestFocus() in game's constructor, but nothing. Any idea ?

The code is too big to post it here, so if anybody wants, I can send you code by email, just write... My email is divac12@gmail.com

[572 byte] By [Divaca] at [2007-10-3 9:38:28]
# 1

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.Ellipse2D;

import javax.swing.*;

public class FocusControl extends JPanel implements ActionListener {

Ellipse2D.Double ball;

double delta = 2;

FocusControl() {

ball = new Ellipse2D.Double(100, 100, 30, 30);

registerKeys();

setFocusable(true);

}

public void actionPerformed(ActionEvent e) {

System.out.println("button action event");

KeyboardFocusManager kfm =

KeyboardFocusManager.getCurrentKeyboardFocusManager();

kfm.focusNextComponent();

}

private void registerKeys() {

getInputMap().put(KeyStroke.getKeyStroke("UP"), "UP");

getActionMap().put("UP", up);

getInputMap().put(KeyStroke.getKeyStroke("LEFT"), "LEFT");

getActionMap().put("LEFT", left);

getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "DOWN");

getActionMap().put("DOWN", down);

getInputMap().put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");

getActionMap().put("RIGHT", right);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

g2.setPaint(Color.red);

g2.fill(ball);

}

private JPanel getSouth() {

JButton button = new JButton("Button");

button.addActionListener(this);

JPanel panel = new JPanel();

panel.add(button);

return panel;

}

public static void main(String[] args) {

FocusControl test = new FocusControl();

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.getContentPane().add(test.getSouth(), "Last");

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

private Action up = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

ball.y -= delta;

repaint();

}

};

private Action left = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

ball.x -= delta;

repaint();

}

};

private Action down = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

ball.y += delta;

repaint();

}

};

private Action right = new AbstractAction() {

public void actionPerformed(ActionEvent e) {

ball.x += delta;

repaint();

}

};

}

crwooda at 2007-7-15 4:54:16 > top of Java-index,Security,Cryptography...