Note that there are some explanatory texts on larger screens.

plurals
  1. POClass extending KeyListener not responding to key presses
    primarykey
    data
    text
    <p>Sorry about its being a duplicate - I was in a hurry when I wrote this, and didn't have time to check. Though I suppose it would have been faster, now that I think about it...</p> <hr> <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/286727/java-keylistener-for-jframe-is-being-unresponsive">Java KeyListener for JFrame is being unresponsive?</a> </p> </blockquote> <p>I have been writing an app and am trying to make hotkeys. I decided to use <code>KeyListener</code>, as it is all I know as of yet. However, the class is not responding to key presses. How would I fix this bug? If there is an alternative to <code>KeyListener</code> that will do the same thing, please let me know and preferably provide an example of how it would work.</p> <h3>Main class</h3> <pre><code>import java.awt.BorderLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; import panels.TabBar; public class __mn implements KeyListener { static JFrame disp = new JFrame("dat app"); static TabBar tabs = new TabBar(); public static void main(String[] args) { disp.setLayout(new BorderLayout()); disp.add(tabs, BorderLayout.PAGE_START); disp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); disp.setSize(TabBar.PREF_WIDTH, 500); disp.setResizable(false); disp.setLocationRelativeTo(null); disp.addKeyListener(new __mn()); disp.setVisible(true); } @Override public void keyPressed(KeyEvent e) { System.out.println(e.paramString()); } //Unused @Override public void keyReleased(KeyEvent e) { System.out.println(e.paramString()); } @Override public void keyTyped(KeyEvent e) { System.out.println(e.paramString()); } } </code></pre> <h3>TabBar class</h3> <pre><code>package panels; import java.awt.Color; import java.awt.FlowLayout; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; //http://www.dreamincode.net/forums/topic/245148-java-key-binding-tutorial-and-demo-program/ public class TabBar extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; public static final int NONE = -1; public static final int INBOX = 0; public static final int SEND_MSG = 1; public static final int PRIVATE_CHAT = 2; public static final int FEEDBACK = 3; public static final int PREF_WIDTH = 425; private static final String[] tabNames = {"Inbox", "Send a message", "Private chat", "Feedback"}; private static final JButton btnInbox = new JButton(tabNames[INBOX]); private static final JButton btnSendMSG = new JButton(tabNames[SEND_MSG]); private static final JButton btnPrivChat = new JButton(tabNames[PRIVATE_CHAT]); private static final JButton btnFeedback = new JButton(tabNames[FEEDBACK]); public int currentTab = -1; public TabBar() { this(new FlowLayout()); } public TabBar(LayoutManager layout) { super(layout); add(btnInbox); add(btnSendMSG); add(btnPrivChat); add(btnFeedback); btnInbox.addActionListener(this); btnSendMSG.addActionListener(this); btnPrivChat.addActionListener(this); btnFeedback.addActionListener(this); setBackground(Color.BLACK); } @Override public void actionPerformed(ActionEvent e) { System.out.println(e.paramString()); if (e.getSource() == btnInbox) currentTab = INBOX; else if (e.getSource() == btnSendMSG) currentTab = SEND_MSG; else if (e.getSource() == btnPrivChat) currentTab = PRIVATE_CHAT; else if (e.getSource() == btnFeedback) currentTab = FEEDBACK; else currentTab = NONE; } public void hotkeyPressed(char pressed) { pressed = Character.toLowerCase(pressed); System.out.println("Hotkey pressed: " + pressed); switch (pressed) { case 'i': setTab(INBOX); break; case 's': setTab(SEND_MSG); break; case 'p': setTab(PRIVATE_CHAT); break; case 'f': setTab(FEEDBACK); break; default: break; } } private void setTab(int tab) { System.out.println("Somthing pressed! tab=" + tab); currentTab = tab; switch (tab) { case INBOX: btnInbox.requestFocusInWindow(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload