Search code examples
javaswingjtextfield

how to make cursor can enter jtextfield but the only way to give it a text is click a button?


i have jTextfield and jButton..

how to

  • user can click on jTextfield(mouse can enter/exited on jtextfield), but if user typing something it will not do anything (except backspace that will remove entire text)
  • when user click the button, it will

jTextfield.setText("something");

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

  • when there are a text in there (when cursor inside jtextfield) then user typing a backspace, it will remove entire text on jtextfield..

how to do this?

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


Solution

  • Use a DocumentFilter, simply add it to your JTextField like so:

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

    alternatively

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

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