如何使光标可以进入jtextfield但是给它一个文本的唯一方法是点击一个按钮? [英] how to make cursor can enter jtextfield but the only way to give it a text is click a button?

查看:134
本文介绍了如何使光标可以进入jtextfield但是给它一个文本的唯一方法是点击一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jTextfield和jButton ..

i have jTextfield and jButton..

如何


  • 用户可以点击jTextfield(鼠标可以在jtextfield上输入/退出),但如果用户输入内容,它将不会做任何事情(除了退格将删除整个文本)

  • 当用户点击按钮时,它将


jTextfield.setText(something);

jTextfield.setText("something");

所以给jtextfield文本的唯一方法是点击按钮

so the only way to give jtextfield text is click the button


  • 当那里是那里的文本(当光标在jtextfield中时)然后用户键入退格,它将删除jtextfield上的整个文本..

如何这样做?

原谅我的英语..
非常感谢任何帮助......

forgive my english.. thanks a lot for any kind of help..

推荐答案

使用 DocumentFilter ,只需将其添加到 JTextField 喜欢这样:

 public class Test {

    public void initComponents() {

        //create frame

        //add DoucmentFilter to JTextField
        MyDocumentFilter myFilter = new MyDocumentFilter();
        JTextField myArea = new JTextField();
        ((AbstractDocument)myArea.getDocument()).setDocumentFilter(myFilter);

         //add components set frame visible
    }

 }

class MyDocumentFilter extends DocumentFilter {

    @Override
    public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
        super.replace(fb, i, i1, string, as);
    }

    @Override
    public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
        super.remove(fb, i, i1);
    }

    @Override
    public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
        super.insertString(fb, i, string, as);
    }

}

或者

你可能想创建一个自定义JTextField 已经有 DocumentFilter (用于重复使用),例如:

You may want to create a custom JTextField which already has a DocumentFilter (for re-usability) something like:

public class MyCustomField extends JTextField {

    public MyCustomField(int cols) {
        super(cols);
    }

    protected Document createDefaultModel() {
        return ((Document) new MyDocument());
    }

    static class MyDocument extends DocumentFilter {

        @Override
        public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
            super.insertString(fb, i, string, as);
        }

        @Override
        public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
            super.remove(fb, i, i1);
        }

        @Override
        public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
            super.replace(fb, i, i1, string, as);
        }
    }
}






从Hovercraft编辑

我在这些方面的想法更多


Edit from Hovercraft
I was thinking more along these lines

import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.text.*;

public class Test {

   public void initComponents() {

      JPanel panel = new JPanel();
      final MyDocumentFilter myFilter = new MyDocumentFilter();
      final JTextField myArea = new JTextField(20);
      ((AbstractDocument) myArea.getDocument()).setDocumentFilter(myFilter);

      panel.add(myArea);

      panel.add(new JButton(new AbstractAction("Set Text") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            myFilter.setFiltering(false);
            myArea.setText("Fe Fi Fo Fum");
            myFilter.setFiltering(true);
         }
      }));

      JOptionPane.showMessageDialog(null, panel);

      // add components set frame visible
   }

   public static void main(String[] args) {
      new Test().initComponents();
   }

}

class MyDocumentFilter extends DocumentFilter {
   private boolean filtering = true;

   @Override
   public void replace(FilterBypass fb, int i, int i1, String string,
         AttributeSet as) throws BadLocationException {
      if (!filtering) {
         super.replace(fb, i, i1, string, as);
      }
   }

   @Override
   public void remove(FilterBypass fb, int i, int i1)
         throws BadLocationException {
      int offset = 0;
      int length = fb.getDocument().getLength();
      super.remove(fb, offset, length);
   }

   @Override
   public void insertString(FilterBypass fb, int i, String string,
         AttributeSet as) throws BadLocationException {
      if (!filtering) {
         super.insertString(fb, i, string, as);         
      }
   }

   public void setFiltering(boolean filtering) {
      this.filtering = filtering;
   }

}

这篇关于如何使光标可以进入jtextfield但是给它一个文本的唯一方法是点击一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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