
i have one txt field and one button.when i entere any test in testfield then only button should be enabled. i need for this i need for this

import javax.swing.*;
import java.awt.event.*;
class TextFieldAndButton
{
public static void main(String[] args)
{
JFrame f=new JFrame();
final JTextField text=new JTextField(20);
final JButton b=new JButton("Check");
b.setEnabled(false);
text.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent keyEvent) {
if((text.getText()).equals("")){
b.setEnabled(false);
}
else{
b.setEnabled(true);
}
}
});
text.setBounds(10,10,150,20);
b.setBounds(10,30,100,20);
f.setLayout(null);
f.add(text);
f.add(b);
f.setVisible(true);
f.setSize(300,100);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.