How to make difference between textField.setText() and adding text to textField manually in java?

StackOverflow https://stackoverflow.com/questions/11120030

Domanda

I have a textField in my application which will be initiated programmatically (textField.setText() ) when user clicked in an item in JList. later user will change this value manually. I get stuck with using document-listener to detect changes in this text field. When changes happens programmatically it must do nothing but if manually happens, it should change the background to red.

How to detect whether textField has been filled out manually or by textField.setText()?

txtMode.getDocument().addDocumentListener(new DocumentListener() {
        public void insertUpdate(DocumentEvent e) {
            if (!mode.equals(e.getDocument()))
            txtMode.setBackground(Color.red);
        }

        public void removeUpdate(DocumentEvent e) {
            if (mode.equals(e.getDocument()))
            txtMode.setBackground(Color.white);              
        }

        public void changedUpdate(DocumentEvent e) {
            //To change body of implemented methods
        }
    });
È stato utile?

Soluzione

there are two ways

  • remove DocumentListener before setText("...") add DocumentListener back if done

code

public void attachDocumentListener(JComponent compo){
      compo.addDocumentListener(someDocumentListener);
}

//similair void for remove....
  • use boolean value for disabling "if needed", but you have to change contens of your DocumentListener

for example

 txtMode.getDocument().addDocumentListener(new DocumentListener() {
    public void insertUpdate(DocumentEvent e) {
        if (!mode.equals(e.getDocument()))

        if (!updateFromModel){
           txtMode.setBackground(Color.red);
        }  
    }

    public void removeUpdate(DocumentEvent e) {
        if (mode.equals(e.getDocument()))

        if (!updateFromModel){
           txtMode.setBackground(Color.white);
        }  
    }

    public void changedUpdate(DocumentEvent e) {
        //To change body of implemented methods
    }
});

Altri suggerimenti

Keep in mind that all the event listeners are executed on the Swing Event Thread. So things might not go in the exact order as you want them to. In this situation, any solution will be hack-ish because you can not get total control over the swing thread and who posts events on it.

What I'm trying to say is this: suppose you chose to use some flag to let your listeners know that it's a programmatic change. Here's the possible scenario (I assume you're following the nice rule of doing any UI updates from the swing thread via invokeLater):

  1. set the flag to skip events
  2. setText
  3. set flag to false

if you do all of it in one invocation, the setText will trigger update events posted to the end of the event queue, thus, by the time they are executed, the flag will already be false.

So you should do step 1 and 2 in one invokeLater call, or even invokeAndWait, and then post another event with invokeLater to unset the flag. And pray that your user is not so fast as to make some changes between these two invocations, otherwise, they'de be considered a programmatic change as well.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top