KeyListener won't do anything - java

I'm trying to add a keylistener to JFrame but whenever I click a button, nothing happens. I am trying to get the keylistener to just print something basic but whenever i try to use an if statement nothing happens in the console.
package gui;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.JButton;
public class KeyListener implements java.awt.event.KeyListener {
ArrayList <JButton> _buttons;
JFrame _frame1;
public KeyListener(ArrayList <JButton> buttons, JFrame frame1){
_buttons = buttons;
_frame1 = frame1;
_frame1.addKeyListener(this);
_frame1.requestFocusInWindow();
_frame1.setFocusable(true);
this.keyTyped();
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_LEFT){
System.out.println("yes");
}
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
That is my keylistener class
This is my game class
package model;
import java.awt.event.KeyListener;
import javax.swing.*;
public class Game implements Runnable{
KeyListener _keylistener;
#Override
public void run() {
JFrame frame1 = new JFrame("KeyBricks");
frame1.setVisible(true);
JPanel panel1 = new JPanel();
frame1.addKeyListener(_keylistener);
model.Board board = new model.Board(panel1);
frame1.add(panel1);
frame1.pack();
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}

You could register a KeyEventDispatcher:
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
#Override
public boolean dispatchKeyEvent(KeyEvent e) {
System.out.println("dispatchKeyEvent: "+e);
return false;
}
});
Note that the registered KeyEventDispatchers will receive KeyEvents before they are dispatched to their targets, allowing to retarget the event, consume it, dispatch the event itself, or make other changes.

Related

Java why is the key listener not working?

I do not know why this doesn't work. I have already read many posts, and added setFocusable but it just does not work.
public class Spiel {
public static void main(String[] args) {
Playground pg = new Playground();
pg.setLocation(0,0);
pg.setSize(1000,1000);
pg.setVisible(true);
pg.setFocusable(true);
}
}
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class Playground extends JFrame implements KeyListener {
Playground(){
}
#Override
public void keyTyped(KeyEvent e) {
System.exit(0);
}
#Override
public void keyPressed(KeyEvent e) {
System.exit(0);
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
You only implemented the KeyListener but if you want it to actually work you still need to register it to your frame.
Playground(){
addKeyListener(this); // should do the trick
}
Otherwise your frame wouldn't know that it actually has to listen and call the methods when a key is pressed.

Adding a keylistener to a JMenuBar

I have the following code for a JMenuBar. How would I add my key listener CustomKeyListener to the entire JMenuBar?
package UI;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.*;
import java.awt.Point;
import java.awt.font.*;
import UI.*;
public class GuiDMenuBar extends JMenuBar
{
JMenu m_file,m_code;
JMenuItem mi_f_new,mi_f_open;
public GuiDMenuBar()
{
setBorderPainted(true);
makeFileMenu();
makeCodeButton();
}
void makeFileMenu()
{
m_file = new JMenu("File");
m_file.setMnemonic('F');
mi_f_new = new JMenuItem("New");
mi_f_new.setMnemonic('N');
mi_f_open = new JMenuItem("Open");
mi_f_open.setMnemonic('O');
mi_f_new.setAccelerator(KeyStroke.getKeyStroke("control N"));
mi_f_open.setAccelerator(KeyStroke.getKeyStroke("control O"));
m_file.add(mi_f_new);
m_file.add(mi_f_open);
add(m_file);
}
void makeCodeButton()
{
m_code = new JMenu("Code");
m_code.setMnemonic('C');
add(m_code);
}
public void addListeners(ActionListener al)
{
mi_f_new.addActionListener(al);
mi_f_open.addActionListener(al);
}
class CustomKeyListener implements KeyListener
{
#Override
public void keyTyped(KeyEvent e)
{
System.out.println("Type");
}
#Override
public void keyPressed(KeyEvent e)
{
System.out.println("Press");
}
#Override
public void keyReleased(KeyEvent e)
{
System.out.println("Release");
}
}
}
I have tried adding
m_code.setFocusable(true);
m_code.addKeyListener(new CustomKeyListener());
to the void makeCodeButton however that did not pick up when anything was typed for that one JMenu. This is why I want it added to the whole JMenuBar instead.
Edit to show full CustomKeyListener
class CustomKeyListener implements KeyListener
{
Robot Roby = null;
#Override
public void keyTyped(KeyEvent e)
{
char c = e.getKeyChar();
if(c==KeyEvent.VK_ENTER)
{
if(m_code.isSelected())
{
try
{
Roby = new Robot();
}
catch(AWTException awte)
{
awte.printStackTrace();
}
clcikComponent(m_file);
}
}
System.out.println("Type");
}
#Override
public void keyPressed(KeyEvent e)
{
System.out.println("Press");
}
#Override
public void keyReleased(KeyEvent e)
{
System.out.println("Release");
}
public void clcikComponent(Component comp)
{
Point p = comp.getLocationOnScreen();
System.out.println(p);
Roby.mouseMove(p.x,p.y);
Roby.mousePress(MouseEvent.BUTTON1_MASK);
Roby.mouseRelease(MouseEvent.BUTTON1_MASK);
}
}
I still think this is a bad idea. As a user I would find it very confusing to find out that using the Enter key on one menu invokes an Action, while it doesn't do anything on other menus. The suggestion I gave in my comment is a far better solution.
However, I was curious why it didn't work and I found out that focus is actually on the JRootPane when the menu is displayed (and events are only dispatched to the component with focus). So you could add the KeyListener to the root pane.
However, there is an easier approach. You can use the JMenu.addMenuKeyListener(...) method. The interface is the same as a KeyListener except all methods include "menu" in the name.

No response from KeyListener

I'm trying to make an easy Java program, but I cannot get any inpute from it. Can anyone suggest a solution?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
class KeyIns extends JFrame implements KeyListener {
public void KeyIns(){
addKeyListener(this); //==> this is why ....
}
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("1");
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("2");
}
#Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("2");
}
}
public class Hello {
public static void main(String[] args){
KeyIns inkey = new KeyIns();
inkey.setSize(368, 300);
inkey.setLocation(250, 250);
inkey.setVisible(true);
}
}
KeyListener will only respond if the component it is registered to is both focusable and has focus.
The other problem is JFrame contains a bunch of other components on top of it, including the root pane and content pane. Registering a KeyListener to the frame is probably never going to achieve anything
A better solution would be to use the Key bindings API
A lot will matter based on what it is you are trying to achieve
The problem is you are never invoking the method
public void KeyIns(){
addKeyListener(this); //==> this is why ....
}
Either invoke the method KeyIns() or remove the word void (so that it becomes constructor) like this
public KeyIns(){
addKeyListener(this); //==> this is why ....
}

Simple KeyListener not working

I'm working on a really simple project in Java to try to understand how to use KeyListener.
I've created a Main class and a KeyListener, MouseListener class. I want to get something to happen when I press a keyboard key. So far the only thing that is working is "Hello" when I click.
Here is my code:
import javax.swing.JFrame;
import javax.swing.JPanel;
public class KeyPractice{
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.addKeyListener(new KeyEar());
panel.addMouseListener(new KeyEar());
frame.add(panel);
frame.setVisible(true);
frame.setSize(400, 400);
}
}
And the Keylistener class....
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class KeyEar implements KeyListener, MouseListener {
public KeyEar(){
}
#Override
public void mouseClicked(MouseEvent arg0) {
System.out.println("Hello");
}
#Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void keyPressed(KeyEvent arg0) {
System.out.println("Hello");
}
#Override
public void keyReleased(KeyEvent arg0) {
System.out.println("Hello");
}
#Override
public void keyTyped(KeyEvent arg0) {
System.out.println("Hello");
}
}
JPanel isn't focusable JComponent, have to add Object/JComponents that is focusable or interact with KeyEvents
KeyListener isn't proper listener for Swing JComponents, for Swing is replaced with KeyBindings
A JPanel cannot gain focus for KeyListener to work.
The preferred approach is to use Key Bindings for Swing. You can map an Action to a KeyStroke even when a component doesn't have focus.
Key Binding Example
I had similar problem but its so simple to solve but you have to found how to solve it witch is not so easy task :D
so how to solve this ? I just set all my buttons as focusable false.
instanceOfYourButton.setFocusable(false);
thats it

Prevent jLabel from losing focus when clicking on a nested jcombobox

I am making an app that will have some 'rectangles' (myLabel) with a text (rectangleName). Whenever I click on that rectangle, a combobox (nameComboBox) is shown to change the name of it.
So this is the declaration of that class.
public class myLabel extends JLabel implements MouseListener,FocusListener{
//this.panel;
JComboBox nameComboBox;
String rectangleName;
I added some focus stuff to that class as you can see it implements FocusListener.
I want to show the combobox only when the recangle is focused and hide it otherwise. The problem that I have is that when I select a rectangle and it shows the combobox because it's focused, when I click on the combobox, the rectangle loses its focus so it hides the combobox. Any way to prevent this?
Edit:
Replying to #mKorbel, This is my SSCCE
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class MyJLabel extends JLabel implements MouseListener,FocusListener{
JComboBox nameComboBox;
JPanel mainPanel;
String name;
public MyJLabel() {
this.setLocation(10,10);
this.setBounds(20, 20,200,200);
this.setBackground(Color.LIGHT_GRAY);
this.setFocusable(true);
setOpaque(true);
setHorizontalAlignment(SwingConstants.CENTER);
setFont(getFont());
setText(this.name);
this.nameComboBox= new JComboBox(new String[] { "option1","option2","option3" });
this.nameComboBox.setBounds(40,40,100,50);
this.nameComboBox.setVisible(false);
this.addMouseListener(this);
this.addFocusListener(this);
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("focus gained");
this.nameComboBox.setVisible(true);
}
#Override
public void focusLost(FocusEvent e) {
System.out.println("focus lost");
this.nameComboBox.setVisible(false);
}
#Override
public void mouseClicked(MouseEvent e) {
this.requestFocus();
}
#Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
MyJLabel myjl=new MyJLabel();
JFrame fr = new JFrame();
fr.setLayout(null);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox otherCombo = new JComboBox(new String[] { "otherOption1","otherOption2","otherOption3" });
otherCombo.setBounds(40,400,100,50);
fr.add(myjl.nameComboBox);
fr.add(myjl);
fr.add(otherCombo);
fr.setSize(300,500);
fr.setVisible(true);
}
}
You must start clicking on the combo at the bottom and you'll see in the console that the focus is lost. click on the gray rectangle and the focus will be gained. Click on the combo inside the rectangle and the rectangle will lose and gain the focus instantly, so it's impossible to choose an item. I would like to not change the focus of the rectangle while I am interacting with its combobox.

Categories