Trung
Posts: 9
Nickname: chitrung
Registered: Jun, 2002
|
|
Re: Listener Problem(Not working well)
|
Posted: Jul 3, 2002 1:32 AM
|
|
hi, may be this help you...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class frame {
JFrame frame = new JFrame("Test");
Container c = frame.getContentPane();
JPanel panel = new JPanel();
JTextField textfield = new JTextField(20);
JButton button = new JButton("Button");
public void openFrame() {
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
c.add(panel,BorderLayout.CENTER);
panel.setLayout(new BorderLayout());
panel.add(textfield, BorderLayout.NORTH);
panel.add(button, BorderLayout.CENTER);
textfield.addCaretListener(new CaretListener() {
public void caretUpdate(CaretEvent e) {
if (textfield.getCaretPosition() == 0 && textfield.getText().equals("")) {
button.setEnabled(false);
}else button.setEnabled(true);
}
});
button.setEnabled(false);
frame.pack();
frame.show();
}
public static void main(String args[]) {
frame newframe = new frame();
newframe.openFrame();
}
}
|
|