Hi Im trying to add a KeyListener to a JFrame. I've done this before and this worked perfectly. Now I am copying the code to let it listen to another JFrame, but now it stopped working for some reason. I don't know what i did wrong.
EDIT: It looks like everything works untill i push a button from one of the 2 JPanels inside the screen. It's like it lost focus after that. How can i fix this?
This is the constructor of the new JFrame where I add the KeyListener :
public class QuizSoftwareView extends JFrame implements View{
private Observable $model;
private Controller $controller;
public EditQuestionsView $editQuestionsView;
public EditTeamsView $editTeamsView;
public AdministratorMenu $adminMenu;
private boolean $isPressed; /* To check if we already listened to a key press event */
/**
* Constructor to make a new quiz
*/
public QuizSoftwareView(Observable model, Controller controller) {
this.setTitle(QuizSoftwareModel.$language.getMessages().getString("title"));
$model = model;
if (controller == null)
$controller = new QuizSoftwareController(model);
else
$controller = controller;
$isPressed = false;
initComponents();
setFocusable(true);
/* Add a keylistener for every team */
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("TEST");
int teamSize; /* team size*/
teamSize = ((QuizSoftwareModel) getModel()).getQuiz().getModel().getTeams().size();
System.out.println(teamSize);
/* F1 is keycode 112, so % 111 gives 1 -> team 1, team 1 gets button F1, team 2 gets button F2 and so on... */
if((e.getKeyCode() % 111) <= teamSize /*&& !alreadyPressed((e.getKeyCode() % 111))*/) { /* If you pressed a number under the teamsize we listen to it, and that team hasn't pressed their button before */
/* Give a pop up message to the admin that a team has pushed their button */
//buttonPressed((e.getKeyCode() % 111));
System.out.println("TESTT");
((QuizSoftwareController)getController()).showScoreView((e.getKeyCode() % 111));
}
}
});
$isPressed = false;
}
}
This is the code of where i did this before (works perfect) :
public class QuizView extends JFrame implements View {
private Observable $model;
private Controller $controller;
private QuestionView $questionView;
private MediaPlayer $mediaView;
private Question $question;
private boolean $isPressed; /* To check if we already listened to a key press event */
public QuizView(Observable model, Controller controller){
setFocusable(true);
$model = model;
if (controller == null)
$controller = new QuizController(model);
else
$controller = controller;
$question = null;
$isPressed = false;
$questionView = new QuestionView($model, null); /* null -> Give the default controller as parameter */
$mediaView = new MediaPlayer($model, null); /* null -> Use default controller */
$model.addObserver($questionView);
$model.addObserver($mediaView);
setTitle("Quiz"); /* Universal word so no messagebundle */
getContentPane().setLayout(new BorderLayout());
getContentPane().add($questionView, BorderLayout.CENTER);
getContentPane().add($mediaView, BorderLayout.EAST);
setExtendedState(this.MAXIMIZED_BOTH);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
((QuizController)getController()).stop();
dispose();
}
});
setFocusable(true);
/* Add a keylistener for every team */
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int teamSize; /* team size*/
teamSize = ((QuizModel) getModel()).getTeams().size();
/* F1 is keycode 112, so % 111 gives 1 -> team 1, team 1 gets button F1, team 2 gets button F2 and so on... */
if((e.getKeyCode() % 111) <= teamSize && !alreadyPressed((e.getKeyCode() % 111))) { /* If you pressed a number under the teamsize we listen to it, and that team hasn't pressed their button before */
/* Give a pop up message to the admin that a team has pushed their button */
buttonPressed((e.getKeyCode() % 111));
((QuizController)getController()).showScoreView((e.getKeyCode() % 111));
}
}
});
$isPressed = false;
pack();
setVisible(false);
}
}
Someone knows what's wrong? Help would be greatly appreciated since this is for a project for school.
Thanks!