Value Change Listener to JTextField

前端 未结 12 2191
情歌与酒
情歌与酒 2020-11-22 04:16

I want the message box to appear immediately after the user changes the value in the textfield. Currently, I need to hit the enter key to get the message box to pop out. Is

12条回答
  •  醉酒成梦
    2020-11-22 04:34

    I am brand new to WindowBuilder, and, in fact, just getting back into Java after a few years, but I implemented "something", then thought I'd look it up and came across this thread.

    I'm in the middle of testing this, so, based on being new to all this, I'm sure I must be missing something.

    Here's what I did, where "runTxt" is a textbox and "runName" is a data member of the class:

    public void focusGained(FocusEvent e) {
        if (e.getSource() == runTxt) {
            System.out.println("runTxt got focus");
            runTxt.selectAll();
        }
    }
    
    public void focusLost(FocusEvent e) {
        if (e.getSource() == runTxt) {
            System.out.println("runTxt lost focus");
            if(!runTxt.getText().equals(runName))runName= runTxt.getText();
            System.out.println("runText.getText()= " + runTxt.getText() + "; runName= " + runName);
        }
    }
    

    Seems a lot simpler than what's here so far, and seems to be working, but, since I'm in the middle of writing this, I'd appreciate hearing of any overlooked gotchas. Is it an issue that the user could enter & leave the textbox w/o making a change? I think all you've done is an unnecessary assignment.

提交回复
热议问题