Question

I have written a calculator in NetBeans and it functions perfectly. However, I have to actually click the buttons to insert numbers and am trying to remedy that with a KeyListener. I have all my numbers and function buttons set inside a JPanel named buttons. I have my display label in a JPanel named display.

I set my class to implement KeyListener and it inserted the KeyPressed, -Typed, and -Released methods; however I stuck from there. I'm not sure how to make my buttons actually listen for the KeyPressed event, and when it hears the event - activate the button. Also, my buttons are named by their number (e.g. the Zero Button is named zero, One button is one, etc.).

I've read that you actually have to implement a KeyListener somewhere by using: something.addKeyListener(something); but I cannot seem to figure this out.

Can I get some help here? I'm new to Java and this is my first solo project. And let me know if I didn't provide enough information.

EDIT: Most of my code is NetBeans Generated and I cannot edit the initialization of the components which seems to be my problem I think?

My class declaration:

public class Calculator extends javax.swing.JFrame implements KeyListener {
 //Creates new form Calculator
public Calculator() {
    initComponents();
}

One of my buttonPressed actions (all identical with changes for actual number):

private void zeroActionPerformed(java.awt.event.ActionEvent evt) {                                     
    if (display.getText().length() >= 16)
    {
        JOptionPane.showMessageDialog(null, "Cannot Handle > 16 digits");
        return;
    }
    else if (display.getText().equals("0"))
    {
        return;
    }
    display.setText(display.getText().concat("0"));

Main method supplied by NetBeans:

public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Calculator().setVisible(true);
        }
    });
}

The initComponents() netbeans generated is absolutely massive (about 500 lines of code) and I cannot edit any of it. Let me know if I can supply any more helpful information.

Was it helpful?

Solution 2

I'm not sure I completely understand your question, and some code would help, but I'll take a crack, since it sounds like a problem I used to have a lot.

It sounds like the reason that your key presses aren't being recognized is that the focus is on one of the buttons. If you add keylisteners to the buttons, then you shouldn't have any problem. In netbeans you can add keylisteners through the design screen really easily. Here's a picture showing you how to add a keyPressed listener to a button in a jPanel.

enter image description here

private void jButton1KeyPressed(java.awt.event.KeyEvent evt) {                                    
    //Check which key is pressed
    //do whatever you need to do with the keypressed information
}       

It is nice to be able to write out the listeners yourself, but if you are just learning, then it is also nice to get as much help as possible. This might not be the best solution, since you would have to add the listener for each of your buttons.

OTHER TIPS

Could there be an issue of Focus, and if so how can I resolve the issue?

Yes there is probably an issue with focus. That is why you should NOT be using a KeyListener.

Swing was designed to be used with Key Bindings. That is you create an Action that does what you want. Then this Action can be added to your JButton. It can also be bound to a KeyStroke. So you have nice reusable code.

Read the Swing tutorial on How to Use Key Bindings for more information. Key Bindings don't have the focus issue that you currently have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top