焦点获得和焦点丢失的事件 [英] Focus Gained and Focus Lost event

查看:279
本文介绍了焦点获得和焦点丢失的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的java swing形式4个JTextfields。问题是我需要通过Java代码而不是使用Tab键将焦点从一个JTextField移动到其他。

如果通过JTextField2获得的焦点意味着需要选择JTextField2中的内容。我不知道如何做这个PLZ把你的正确的代码关联这个问题

解决方案

这可能有点复杂



你必须包装并延迟你的rel = nofollow> Action 或 ActionListener 转换为 invokeLater(),并放在里面(最安全的方法是设置下面的代码行)

$ ul

  • JTextField2.setText(JTextField2.getText);



  • / b>


    • JTextField2.selectAll();
    • ($)

      编辑到@Andrew Thompson
      $ b

        private FocusListener fcsListener = new FocusListener {

      @Override
      public void focusGained(FocusEvent e){
      dumpInfo(e);
      }

      @Override
      public void focusLost(FocusEvent e){
      // dumpInfo(e);

      $ b $ private void dumpInfo(FocusEvent e){
      System.out.println(Source:+ name(e.getComponent()));
      System.out.println(Opposite:+ name(e.getOppositeComponent()));
      System.out.println(Temporary:+ e.isTemporary());
      Component c = e.getComponent(); //用于编辑JComboBox太多
      if(c instanceof JFormattedTextField){
      ((JFormattedTextField)c).selectAll();
      } else if(c instanceof JTextField){
      ((JTextField)c).selectAll();
      } //这两个方法不正确setText(getText())内部invokeLater


      私有字符串名称(组件c){
      return(c ==空值) ? null:c.getName();
      }
      };


      I have 4 JTextfields in my java swing form. The problem is I need to move the Focus from one JTextField to other through java code not by using tab key.

      If the Focus gained by JTextField2 means the content in the JTextField2 need to be selected. I don't know how to do this plz put your proper code associate with this issue

      解决方案

      that could be little bit complicated

      you have to wrap and delay your Action or ActionListener into invokeLater(), and put inside (most safiest way is to set there follows code lines)

      • JTextField2.setText(JTextField2.getText);

      and

      • JTextField2.selectAll();

      edit to @Andrew Thompson

      private FocusListener fcsListener = new FocusListener() {
      
              @Override
              public void focusGained(FocusEvent e) {
                  dumpInfo(e);
              }
      
              @Override
              public void focusLost(FocusEvent e) {
                  //dumpInfo(e);
              }
      
              private void dumpInfo(FocusEvent e) {
                  System.out.println("Source  : " + name(e.getComponent()));
                  System.out.println("Opposite : " + name(e.getOppositeComponent()));
                  System.out.println("Temporary: " + e.isTemporary());
                  Component c = e.getComponent();//works for editable JComboBox too
                  if (c instanceof JFormattedTextField) {
                      ((JFormattedTextField) c).selectAll();
                  } else if (c instanceof JTextField) {
                      ((JTextField) c).selectAll();
                  }//both methods not correct required setText(getText()) inside invokeLater
              }
      
              private String name(Component c) {
                  return (c == null) ? null : c.getName();
              }
          };
      

      这篇关于焦点获得和焦点丢失的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆