How to use property change events on a JTextField

For some reason I can't make the JTextFields fire the property change events when changing the text in the textfields. I followed a guide used on JFormattedTextField on sun.com, but maybe its not the same concept? I have attached the non working code:
//init
north.add(new JLabel("Address"));
addrText = new JTextField(link.getAddress(),30);
addrText.addPropertyChangeListener("value", this);
north.add(addrText);
north.add(new JLabel("Port"));
portText = new JTextField(String.valueOf(link.getPort()),5);
portText.addPropertyChangeListener("value", this);
north.add(portText);
//the propertyChange implementation 
public void propertyChange(PropertyChangeEvent evt) {
       Object source = evt.getSource();
       if (source == addrText) {
            link.setAddress(addrText.getText());
        } else if (source == portText) {
            try {
            	link.setPort(Integer.parseInt(portText.getText()));
           } catch (NumberFormatException e) {
	portText.setText((String)evt.getOldValue());
           }         	
       }}

Open in new window

SuppaiAsked:
Who is Participating?
 
ksivananthCommented:
I think, that get fired only when you call setValue method!

in this case, you may want to use the FocusListeners!
0
 
ksivananthCommented:
or DocumentListener
0
 
SuppaiAuthor Commented:
So how would I do it if I want an event to fire when the has inputted a new value and removed focus from the textfield?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ksivananthCommented:
yes!
0
 
ksivananthCommented:
you can use the DocumentListener also,

class MyDocumentListener implements DocumentListener {
    public void insertUpdate(DocumentEvent e) {
        setValue(e);
    }
    public void removeUpdate(DocumentEvent e) {
        setValue(e);
    }
    public void changedUpdate(DocumentEvent e) {
        // we won't ever get this with a PlainDocument
    }
    private void setValue(DocumentEvent e) {
        Document whatsup = e.getDocument();
        if (whatsup.getProperty("name").equals("address"))
            link.setAddress( addrText.getValue() );
        else if (whatsup.getProperty("name").equals("port"))
            link.setPort( portText.getValue() );
    }
}    
        MyDocumentListener myDocumentListener = new MyDocumentListener();
        addrText.getDocument().addDocumentListener(myDocumentListener);
        addrText.getDocument().putProperty("name", "amount");
        portText.getDocument().addDocumentListener(myDocumentListener);
        portText.getDocument().putProperty("name", "amount");


0
 
SuppaiAuthor Commented:
That did the trick. thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.