0

Swing Framework を使用した GUI 設計の複雑さを知るために、swing MVC アプリケーションを作成しています。私のJDKバージョンは7で、ファイルへのパスを保存するためのテキストフィールド、参照ボタン、ユーザーIDとパスワード用の2つのテキストフィールド、更新およびキャンセル機能用の2つのボタンを持つダイアログを含む単純なアプリケーションがあります。

これらのボタンをクリックすると、更新またはキャンセルのメッセージが表示されたダイアログが表示されます。

DocumentListener を使用して、テキスト フィールド コンポーネントを検証することを計画しました。

UI には、2 つのテキストフィールドを作成する次のコードがあります。

public JPasswordField getMyPasswordField() {
        if(myPasswordField == null)
        {
            myPasswordField = new JPasswordField();
            myPasswordField.setBounds(133, 93, 163, 21);
            myPasswordField.getDocument().putProperty("Owner", "myPasswordField");
        }
        return myPasswordField;
    }


public JTextField getMyUserNameField() {
        if(myUserNameField== null)
        {
            myUserNameField = new JTextField();
            myUserNameField.setBounds(133, 66, 163, 21);

            myUserNameField.getDocument().putProperty("Owner", "myUserNameField");

        }
        return myUserNameField;
    }

コントローラーでは、次のコードを使用しました。

myReferenceUI.getMyUserNameField().getDocument().addDocumentListener(this);
myReferenceUI.getMyPasswordField().getDocument().addDocumentListener(this);

メソッドの実装では、次のように記述しました。

public void insertUpdate(DocumentEvent e) {

        Object owner = e.getDocument().getProperty("Owner");
         changed(owner);

         }
    @Override
    public void removeUpdate(DocumentEvent e) {

        Object owner =e.getDocument().getProperty("Owner"); 
        changed(owner);

    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        Object owner =e.getDocument().getProperty("Owner"); 
        changed(owner);

    }

   public void changed(Object e)

   {

       System.out.println(e.toString());

     if(  e.toString().equals("myUserNameField"))
     {
         if(myReferenceUI.getMyUserNameField().getText().equals("") )
         {
             myReferenceUI.getMyUpdateButton().setEnabled(false);
             return ;
         }

     }

         if(  e.toString().equals("myPasswordField"))
         {
             if(myReferenceUI.getMyPasswordField().getText().equals("") )
             {
                 myReferenceUI.getMyUpdateButton().setEnabled(false);
                 return ;
             }

         }

          myReferenceUI.getMyUpdateButton().setEnabled(true);    

}

私の意図は、ユーザー名とパスワードの 2 つのテキスト ボックスに null 以外の値がある場合にのみ、更新ボタンを有効にすることでした。

ユーザー名/パスワード フィールドに null を入力すると、リスナー イベントが適切に発生し、更新ボタンが無効になります。

しかし、これらのフィールドに値を入力した後、バックスペースを押してテキストを消去すると、更新ボタンは無効のままになります。

この状態を取り除くにはどうすればよいですか?

4

2 に答える 2