EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Enable or disable a JButton based on JTextField content


Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html


 import java.awt.*;
 import com.sun.java.swing.*;
 import com.sun.java.swing.text.*;
 import com.sun.java.swing.event.*;
 //import javax.swing... *  if Java 2;

 public class tswing extends JApplet implements DocumentListener{
   JButton button;
   JTextField textfield;
   Document document;
   public void init() {
     getContentPane().setLayout(new FlowLayout());
     textfield = new JTextField(10);
     getContentPane().add(textfield);
     document = textfield.getDocument();
     document.addDocumentListener(this);
     button = new JButton("foo");
     getContentPane().add(button);
     button.setEnabled(false);
     }

    public void changedUpdate(DocumentEvent e ) {
      if (e.getDocument()==document) 
        disableIfEmpty(document);
       }   
      
    public void insertUpdate( DocumentEvent e ){
      if (e.getDocument()==document)
        disableIfEmpty(document);
       }   

    public void removeUpdate( DocumentEvent e ){
      if (e.getDocument()==document) 
        disableIfEmpty(document);
       }   

    public void disableIfEmpty(Document d) {
       button.setEnabled(d.getLength() > 0);
       }
 }




NOTE: With the JDK1.2 release, it's recommended to use


 import javax.swing.*;

instead of


 import com.sun.java.swing.*;


Posted On: 5-Jul-1999

internet.commerce



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy