I created a classA
for designing process and classB
for creating methods; that method is used to call a classA
JTextField
. JtextField1
is used to update only uppercase String
, JTextField2
is used to update only numeric values; Jtextfield3
is used to update email ID format. I am getting an error when I am calling from another class. I can't to call that ClassB
method in ClassA
JTextField
.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class sample {
JLabel label1 = new JLabel("name");
JLabel label2 = new JLabel("reg.no");
JTextField text1 = new JTextField(10);
JTextField text2 = new JTextField(10);
JFrame fr = new JFrame();
public sample() {
JPanel jp = new JPanel(new GridLayout(2, 2));
KeyListener textkey = new textListener();
text1.addKeyListener(textkey);//created a keylistener for text1
text2.addKeyListener(textkey);//created a keylistener for text2
jp.add(label1);
jp.add(text1);
jp.add(label2);
jp.add(text2);
fr.add(jp);
fr.pack();
fr.setSize(430, 350);
fr.setTitle("form1");
fr.setVisible(true);
}
class textListener implements KeyListener {//keyListener is implemented
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
JLabel label = new JLabel();
String value = text1.getText();
String value1 = text2.getText();
String keyString;
int l = value.length();
if ((e.getKeyCode() >= KeyEvent.VK_A
&& e.getKeyCode() <= KeyEvent.VK_Z)
|| (e.getKeyCode() == KeyEvent.VK_BACK_SPACE)
|| (e.getKeyCode() == KeyEvent.VK_CAPS_LOCK)) {
text1.setEditable(true);
} else {
JOptionPane.showMessageDialog(label, "Enter Alpha",
"InputError", JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String ar[]) {
new sample();
}
}