如何使光标可以进入jtextfield,但是给它一个文本的唯一方法是点击一个按钮?

时间:2012-11-22 16:48:57

标签: java swing jtextfield

我有jTextfield和jButton ..

如何

  • 用户可以点击jTextfield(鼠标可以在jtextfield上输入/退出),但如果用户输入内容,它将不会执行任何操作(除了退格将删除整个文本)
  • 当用户点击按钮时,它将
  

jTextfield.setText( “东西”);

因此,提供jtextfield文本的唯一方法是单击按钮

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

怎么做?

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

2 个答案:

答案 0 :(得分:5)

使用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);
    }

}

或者

您可能想要创建一个custom JTextField已经有DocumentFilter(用于重复使用),例如:

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);
        }
    }
}

从气垫船编辑
我正在考虑更多这些方面

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;
   }

}

答案 1 :(得分:3)

在jTextfield的键监听器中,在keyTyped事件中,检查e.getKeyChar()是否是退格,如果不是,请执行e.consume();它会取消活动

退格键的keychar是8

这是一个例子:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;


public class ConsumeExceptForBackSpace extends JFrame {

    private boolean canWrite = false;

    public ConsumeExceptForBackSpace() {
        super();

        JButton b = new JButton("Click");
        JTextField f = new JTextField("");
        this.setLayout(new BorderLayout());
        this.add(b, BorderLayout.CENTER);
        this.add(f, BorderLayout.SOUTH);

        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                canWrite = !canWrite;
            }
        });

        f.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar() != KekEvent.VK_BACK_SPACE && !canWrite) e.consume();
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

            }
        });

        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        new ConsumeExceptForBackSpace();
    }
}