TextField Listener

Hello !I have an authentification frame with two TextFields (Login an Pass) and a button (OK).I would like to make the button enable only if the two textfields are not empty. Is it possible ?I make a Listener to the two Textfield but it dosen't work. Thanks !
[288 byte] By [SnowFalcona] at [2007-11-27 6:58:45]
# 1

one of the ways:

import javax.swing.*;

import javax.swing.event.*;

import java.awt.*;

class Testing

{

JTextField login, pass;

JButton ok;

public void buildGUI()

{

login = new JTextField(10);

pass = new JTextField(10);

ok = new JButton("OK");

ok.setEnabled(false);

login.getDocument().addDocumentListener(new DocumentListener(){

public void changedUpdate(DocumentEvent e){setButtonStatus();}

public void removeUpdate(DocumentEvent e){setButtonStatus();}

public void insertUpdate(DocumentEvent e){setButtonStatus();}

});

pass.getDocument().addDocumentListener(new DocumentListener(){

public void changedUpdate(DocumentEvent e){setButtonStatus();}

public void removeUpdate(DocumentEvent e){setButtonStatus();}

public void insertUpdate(DocumentEvent e){setButtonStatus();}

});

JFrame f = new JFrame();

f.getContentPane().add(login,BorderLayout.NORTH);

f.getContentPane().add(pass,BorderLayout.CENTER);

f.getContentPane().add(ok,BorderLayout.SOUTH);

f.pack();

f.setLocationRelativeTo(null);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

}

public void setButtonStatus()

{

ok.setEnabled(login.getDocument().getLength() > 0 && pass.getDocument().getLength() > 0);

}

public static void main(String[] args)

{

SwingUtilities.invokeLater(new Runnable(){

public void run(){

new Testing().buildGUI();

}

});

}

}

Michael_Dunna at 2007-7-12 18:49:16 > top of Java-index,Desktop,Core GUI APIs...
# 2
You are my hero Michael :)Thank you very much !
SnowFalcona at 2007-7-12 18:49:16 > top of Java-index,Desktop,Core GUI APIs...