在键入时如何同时更新Jtextfield文本?

时间:2015-10-25 17:42:44

标签: java swing jtextfield

我有一个JTextField。我希望在用户输入的每个字符之后,一个方法调用并在同一个JTextField中更改字符的顺序,但是在我的代码中displayArrestNo2()方法调用并在consol中打印正确答案但是不要更新内容JTextField.How可以做到吗?

  public static void main(String args[]) {


         final JTextField textField = new JTextField();
            textField.setSize(100, 100);
            textField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void insertUpdate(DocumentEvent e) {  
                try{

          Stringx=displayArrestNo2(e.getDocument().getText(0,e.getDocument().getLength()));
                    System.out.println(x);



                }catch(Exception ex){
                    ex.printStackTrace();
                }


           }

      public void warn() {   
      }
          public  String displayArrestNo2(String aValue){
                   System.out.println("oroginal value: " + aValue);
          aValue = " "+aValue+" ";
          System.out.println("space added value: "+aValue);
          final String validStr = " ابپتثجچحخدرزژسشصضطظعغفقکگلمنوهیي";
        int lPos, fPos, i, j, k;
        boolean flgEnterIntSection, lastWasChar;
        String result = "";

        flgEnterIntSection= false;
          lastWasChar = false;
          lPos = 0;
          i = aValue.length()-1;
          while( i >= 0) {
             char[] aValueArr = aValue.toCharArray();
             /* char[] tmpArr = aValue.toCharArray();
              char[] aValueArr = new char[tmpArr.length];
              for(int x=0; x<tmpArr.length; x++)
                  aValueArr[x]= tmpArr[tmpArr.length-x-1]; */

              if (aValueArr[i] == 'ß')
                  aValueArr[i] = '˜'; 

              if (convert(aValueArr[i]) == -1)
              {
                  if(validStr.indexOf(aValueArr[i]) > 0)
                  {
                      j = 0;
                      while ( ( (i - j) > 0) && (validStr.indexOf(aValueArr[i-j]) > 0 ))
                          j = j + 1;

                          if (j >1)//j>1 
                          {
                              k = j;
                              while (j > 0) 
                              {
                                result =  result + aValueArr[i - j+1];//i-j+1
                                j--;
                              }
                              i = i - k+1;//i-k+1
                            }
                            else
                              result = result + aValueArr[i];
                            }
                        else
                          result = result + aValueArr[i];
                        i--;
                        flgEnterIntSection = false;
                        }

              else
                {
                  fPos = i;
                  j = i;
                  while ((j >= 0) && ((!flgEnterIntSection) || (convert(aValueArr[j]))!=-1))// (StrToIntDef(AValue[j], -1) <> -1)
                  {
                    flgEnterIntSection = true;
                    while ((j >= 0) && (convert(aValueArr[j]))!=-1) // StrToIntDef(AValue[j], -1) <> -1)
                      j--;
                    if ((j > 0) && ((aValueArr[j] == '.') || (aValueArr[j] == '/')) )
                      if (((j-1) >= 0) && (convert(aValueArr[j-1]))!=-1)  //StrToIntDef(AValue[j - 1], -1) <> -1)
                        j--;
                  }
                  lPos = j + 1;

                    for (j = lPos; j <= fPos; j++)

                    {
                        result = result + aValueArr[j];
                    }
                    i = lPos-1;
                }
          }

    //  return result;
        //  System.out.println("before reverse "+result);
          return reverseOrder(result);
    }

     String reverseOrder(String input){
         String [] inArr = input.split(" ");
         String reversed = "";
         for(int i = inArr.length-1;  i>=0; i--){
             reversed += inArr[i] + " ";
         }
         return reversed;
     }

        public int convert(char ch) {

            try {
                int m = Integer.parseInt(String.valueOf(ch));
                return m;
            } catch (Exception e) {
                return -1;
            }

        }

              TestJTextField x=new TestJTextField();
                    x.add(textField);
                   x.setVisible(true);                 
    }

在insertUpdate()方法中,每个字符调用displayArrestNo2()但不改变JTextField的内容。

0 个答案:

没有答案