• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8857
  • Last Modified:

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

0
Suppai
Asked:
Suppai
  • 5
  • 2
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now