Add Keyboard listener to a java code

Status
Not open for further replies.

abhinav_bipnesh

Journeyman
Hi,

I am trying to add key board listener to my Swing code but enable to do so as i am able to use mouse click event for button press. Now I want to add a key listener to my code so that even when a numeric key is pressed then the text field has the input.
The code is given below.
import java.awt.Image;
import java.awt.Toolkit;

/*
* Calculator.java
*
* Created on June 29, 2008, 12:12 PM
*/
import java.util.List;



/**
*
* @author Abhinav
*/
public class Calculator extends javax.swing.JFrame {

/** Creates new form Calculator */
public Calculator() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {

jPanel1 = new javax.swing.JPanel();
two = new javax.swing.JButton();
zero = new javax.swing.JButton();
sqrt = new javax.swing.JButton();
seven = new javax.swing.JButton();
plus = new javax.swing.JButton();
multi = new javax.swing.JButton();
textField = new javax.swing.JTextField();
three = new javax.swing.JButton();
nine = new javax.swing.JButton();
five = new javax.swing.JButton();
sub = new javax.swing.JButton();
dot = new javax.swing.JButton();
eight = new javax.swing.JButton();
equals = new javax.swing.JButton();
sign = new javax.swing.JButton();
reci = new javax.swing.JButton();
four = new javax.swing.JButton();
one = new javax.swing.JButton();
div = new javax.swing.JButton();
percent = new javax.swing.JButton();
six = new javax.swing.JButton();
bkSpace = new javax.swing.JButton();
clearE = new javax.swing.JButton();
clear = new javax.swing.JButton();
memState = new javax.swing.JToggleButton();
memClear = new javax.swing.JButton();
memRead = new javax.swing.JButton();
memStore = new javax.swing.JButton();
memCalc = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Calculator");
setIconImage(Toolkit.getDefaultToolkit().getImage("pic.gif"));
setLocationByPlatform(true);
setResizable(false);

two.setText("2");
two.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
twoActionPerformed(evt);
}
});

zero.setText("0");
zero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
zeroActionPerformed(evt);
}
});

sqrt.setText("sqrt");

seven.setText("7");
seven.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sevenActionPerformed(evt);
}
});

plus.setText("+");

multi.setText("*");

textField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
textField.setText("0");
textField.setFocusable(false);

three.setText("3");
three.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
threeActionPerformed(evt);
}
});

nine.setText("9");
nine.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nineActionPerformed(evt);
}
});

five.setText("5");
five.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fiveActionPerformed(evt);
}
});

sub.setText("-");

dot.setText(".");
dot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dotActionPerformed(evt);
}
});

eight.setText("8");
eight.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
eightActionPerformed(evt);
}
});

equals.setText("=");

sign.setText("+/-");
sign.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
signActionPerformed(evt);
}
});

reci.setText("1/x");

four.setText("4");
four.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
fourActionPerformed(evt);
}
});

one.setText("1");
one.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
oneActionPerformed(evt);
}
});

div.setText("/");

percent.setText("%");

six.setText("6");
six.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sixActionPerformed(evt);
}
});

bkSpace.setForeground(new java.awt.Color(255, 0, 0));
bkSpace.setText("Backspace");
bkSpace.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bkSpaceActionPerformed(evt);
}
});

clearE.setForeground(new java.awt.Color(255, 0, 0));
clearE.setText("CE");
clearE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearEActionPerformed(evt);
}
});

clear.setForeground(new java.awt.Color(255, 0, 0));
clear.setText("C");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});

memState.setEnabled(false);

memClear.setText("MC");
memClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
memClearActionPerformed(evt);
}
});

memRead.setText("MR");
memRead.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
memReadActionPerformed(evt);
}
});

memStore.setText("MS");
memStore.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
memStoreActionPerformed(evt);
}
});

memCalc.setText("M+");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(textField, javax.swing.GroupLayout.DEFAULT_SIZE, 336, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(memClear)
.addComponent(memRead)
.addComponent(memStore)
.addComponent(memCalc)
.addComponent(memState))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel1Layout.createSequentialGroup()
.addComponent(sign)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(zero)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(dot)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(plus)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(equals))
.addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel1Layout.createSequentialGroup()
.addComponent(four)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(five)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(six)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(multi)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(percent))
.addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel1Layout.createSequentialGroup()
.addComponent(one)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(two)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(three)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sub)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(reci))
.addGroup(javax.swing.GroupLayout.Alignment.CENTER, jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(15, 15, 15)
.addComponent(bkSpace, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(clearE, javax.swing.GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(clear, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(seven)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(eight)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(nine)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(div)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(sqrt)))))
.addContainerGap())
);

jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {div, dot, eight, equals, five, four, memCalc, memClear, memRead, memState, memStore, multi, nine, one, percent, plus, reci, seven, sign, six, sqrt, sub, three, two, zero});

jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(textField, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(clear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(clearE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bkSpace, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(memState, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(seven, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(eight, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(nine, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(div, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sqrt, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(memClear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(four, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(five, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(six, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(multi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(percent, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(memRead, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(one, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(two, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(three, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sub, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(reci, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(memStore, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sign, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(zero, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(dot, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(plus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(equals, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(memCalc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);

pack();
}// </editor-fold>//GEN-END:initComponents
//Image icon = Toolkit.getDefaultToolkit().getImage("images.jpeg");

private void sevenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sevenActionPerformed
// TODO add your handling code here:
/*if(Integer.parseInt(textField.getText()) == 0)
textField.setText("7");
else
textField.setText(textField.getText() + "7");*/
dataInTextField(seven.getText());
}//GEN-LAST:event_sevenActionPerformed

private void eightActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eightActionPerformed
// TODO add your handling code here:
dataInTextField(eight.getText());
}//GEN-LAST:event_eightActionPerformed

private void nineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nineActionPerformed
// TODO add your handling code here:
dataInTextField(nine.getText());
}//GEN-LAST:event_nineActionPerformed

private void fourActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fourActionPerformed
// TODO add your handling code here:
dataInTextField(four.getText());
}//GEN-LAST:event_fourActionPerformed

private void fiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fiveActionPerformed
// TODO add your handling code here:
dataInTextField(five.getText());
}//GEN-LAST:event_fiveActionPerformed

private void sixActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sixActionPerformed
// TODO add your handling code here:
dataInTextField(six.getText());
}//GEN-LAST:event_sixActionPerformed

private void threeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_threeActionPerformed
// TODO add your handling code here:
dataInTextField(three.getText());
}//GEN-LAST:event_threeActionPerformed

private void twoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_twoActionPerformed
// TODO add your handling code here:
dataInTextField(two.getText());
}//GEN-LAST:event_twoActionPerformed

private void oneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oneActionPerformed
// TODO add your handling code here:
dataInTextField(one.getText());
}//GEN-LAST:event_oneActionPerformed

private void zeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zeroActionPerformed
// TODO add your handling code here:
dataInTextField(zero.getText());
}//GEN-LAST:event_zeroActionPerformed

private void dotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dotActionPerformed
// TODO add your handling code here:

if(textField.getText().equals("0")) {
if(textField.getText().indexOf(".") == -1)
textField.setText(textField.getText() + ".");
}else if(textField.getText().indexOf(".") == -1) {
dataInTextField(dot.getText());
}

}//GEN-LAST:event_dotActionPerformed

private void signActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_signActionPerformed
// TODO add your handling code here:
if(textField.getText().equals("0")) {
negFlag = true;
}
if(!negFlag) {
negFlag = true;
textField.setText("-" + textField.getText());
} else {
negFlag = false;
//textField.setText();
String str_temp = textField.getText();
int len = str_temp.length();
System.out.println("string is [" + str_temp +"] and length:" + len);
System.out.println("Char at 0: " +str_temp.charAt(0));
if(len == 1)
textField.setText("0");
else
textField.setText(str_temp.substring(1, len));
}
}//GEN-LAST:event_signActionPerformed

private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
// TODO add your handling code here:
textField.setText("0");
}//GEN-LAST:event_clearActionPerformed

private void clearEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearEActionPerformed
// TODO add your handling code here:
tempDataA = 0;
tempDataB = 0;
}//GEN-LAST:event_clearEActionPerformed

private void bkSpaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bkSpaceActionPerformed
// TODO add your handling code here:
String strTemp = textField.getText();
int len = strTemp.length();
int signPlace = 1;
if(strTemp.indexOf("-") != -1)
signPlace =2;
if(len != signPlace)
textField.setText(strTemp.substring(0, len-1));
else
textField.setText("0");
}//GEN-LAST:event_bkSpaceActionPerformed

private void memStoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memStoreActionPerformed
// TODO add your handling code here:
memStoreValue = textField.getText();
memState.setText("M");
System.out.println("Memory store has value [" + memStoreValue +"]");
//memState.setEnabled(false);

}//GEN-LAST:event_memStoreActionPerformed

private void memClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memClearActionPerformed
// TODO add your handling code here:
System.out.println("We going to clear value [" + memStoreValue + "]");
memStoreValue = "0";
memState.setText("");
}//GEN-LAST:event_memClearActionPerformed

private void memReadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_memReadActionPerformed
// TODO add your handling code here:
textField.setText(memStoreValue);
}//GEN-LAST:event_memReadActionPerformed

/**
* @param args the command line arguments
*/
private void dataInTextField(String str) {
if(textField.getText().equals("0"))
textField.setText(str);
else
textField.setText(textField.getText() + str);
}
/*private void dataInTextField(String str, boolean flag) {
if(textField.getText().equals("0"))
textField.setText(str);
else
textField.setText(textField.getText() + str);
}*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calculator().setVisible(true);
}
});
}
private int tempDataA, tempDataB;
boolean negFlag = false;
String memStoreValue;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton bkSpace;
private javax.swing.JButton clear;
private javax.swing.JButton clearE;
private javax.swing.JButton div;
private javax.swing.JButton dot;
private javax.swing.JButton eight;
private javax.swing.JButton equals;
private javax.swing.JButton five;
private javax.swing.JButton four;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton memCalc;
private javax.swing.JButton memClear;
private javax.swing.JButton memRead;
private javax.swing.JToggleButton memState;
private javax.swing.JButton memStore;
private javax.swing.JButton multi;
private javax.swing.JButton nine;
private javax.swing.JButton one;
private javax.swing.JButton percent;
private javax.swing.JButton plus;
private javax.swing.JButton reci;
private javax.swing.JButton seven;
private javax.swing.JButton sign;
private javax.swing.JButton six;
private javax.swing.JButton sqrt;
private javax.swing.JButton sub;
private javax.swing.JTextField textField;
private javax.swing.JButton three;
private javax.swing.JButton two;
private javax.swing.JButton zero;
// End of variables declaration//GEN-END:variables

}

Some code is auto generated using NetBean IDE so may be you have to tell how to add the code in the IDE as i am using the same ide to write the code.

Thanks in advance. :)
 

chandru.in

In the zone
Do not post such long code as it is very cumbersome to read and understand. See this page about SSCCE.

Your requirement can be achieved by adding an individual key listener to every component in the Frame. But it is a very poor way of doing it as it makes your code more VB like and leads to too much of unnecessary code. Here is one way of achieving what you want without such unnecessary code mess. This code just adds whatever character is typed. You may want to filter out characters in your listener code.

Code:
KeyListener listener = new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                char ch = e.getKeyChar();
                textField.setText(textField.getText() + ch);
            }
        };

        Component[] components = getContentPane().getComponents();
        for (Component comp : components)
            comp.addKeyListener(listener);
Adding this code fragment in your construtor after the statement invoking initializeComponents(), should do the trick for you. :)
 
Status
Not open for further replies.
Top Bottom