Hello lehmann2,

My quick answer would be to instead subclass Document and then check wether
something are inserted in the document and then enableButtons()....

Look at the Document interface and setModel on JTextField.

>> I THINK i know what you are asking for - but im not sure. Can you give a
>> short code-example of what it is you are trying to do ?
>>
>> /max

lehmann2> Well,
lehmann2> I have a JPanel in a JFrame that contains a JTextField and JButtons.
lehmann2> Here is a part of my code:

lehmann2> JTextField txtName = new JTextField();
lehmann2>  txtName.addKeyListener(new KeyAdapter() {
lehmann2>             public void keyPressed(KeyEvent e) {
lehmann2>                 System.out.println("KeyPressed");
lehmann2>                 enableButtons()
lehmann2>             };
lehmann2>             public void keyReleased(KeyEvent e) {
lehmann2>                 enableButtons()
lehmann2>                 System.out.println("KeyReleased");
lehmann2>             };
lehmann2>             private void enableButtons() {
lehmann2>                 buttonOK.setEnabled(true);
lehmann2>                 cancelBtn.setEnabled(true);
lehmann2>             }
lehmann2>         });


lehmann2> As you can see, I just want to enable buttons when a key is typed in the
lehmann2> textfield.
lehmann2> I implemented both keyPressed() and keyReleased() because I had ever observed
lehmann2> that when I type a key, it often happens that only one of these two methods
lehmann2> is called ... so that way I am sure that the enablebuttons() method is called
lehmann2> ... but my problem is not here.

lehmann2> In a general way, when I type a key, the buttons are properly enabled, and
lehmann2> traces are OK, but sometimes (I haven't found why), it is as if the textfield
lehmann2> had lost its listener: nothing happens when a key is typed. If I leave the
lehmann2> panel and then come back to it ( My frame has a central panel in witch can be
lehmann2> loaded my  textfield panel or another one), sometimes the listener "comes
lehmann2> back" but it is not garanteed.

lehmann2> I have read in swing doc that the textfield has to be visible and to have the
lehmann2> keyboard focus so that its keylistener is activated; I added a focus listener
lehmann2> to the textfield to trace when the focus is gained and lost, and I could see
lehmann2> it did had the focus ... that is why I thought that having the keyboard focus
lehmann2> was not the same as having the focus detected by the listener ... now, I Know
lehmann2> my supposition was wrong, so I am absolutly impowerished... <:(

lehmann2> Do you have any idea?

lehmann2> greetings,
lehmann2> G.

lehmann2> _______________________________________________
lehmann2> Advanced-swing mailing list
lehmann2> [EMAIL PROTECTED]
lehmann2> http://eos.dk/mailman/listinfo/advanced-swing



-- 
Best regards,
 Max                            mailto:[EMAIL PROTECTED]

_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to