Populating JTextField based on latest KeyStroke

前端 未结 2 1203
天命终不由人
天命终不由人 2021-01-24 04:28

The use case in my UI is to populate two JTextField components based on double clicking items in a JList. The easy was is to use a JCheckBox

2条回答
  •  攒了一身酷
    2021-01-24 05:24

    As everyone has suggested initialize a FocusListener to both the text fields and and point it to a string variable when either of the loses focus.

    Code:

    String LastFocusLost = null;
    
    jTextField1.addFocusListener(new java.awt.event.FocusAdapter()
     {
      public void focusLost(java.awt.event.FocusEvent evt)
         {
          LastFocusLost = "jTextField1";
         }  
     });
    
    jTextField2.addFocusListener(new java.awt.event.FocusAdapter()
     {
      public void focusLost(java.awt.event.FocusEvent evt)
         {
          LastFocusLost = "jTextField2";
         }  
     });
    

    In the mouseListener add a if-else then:

    if ("jTextField1".equals(LastFocusLost))
     {
       //;
     }
    
    else if ("jTextField2".equals(LastFocusLost))
     {
      //;
     }
    

提交回复
热议问题