Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8870307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:41:27+00:00 2026-06-14T17:41:27+00:00

i have jTextfield and jButton.. how to user can click on jTextfield(mouse can enter/exited

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:41:29+00:00Added an answer on June 14, 2026 at 5:41 pm

    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;
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTextField that I'm not always passing a value to, but it
I have a JFrame with some a JTextField and a JButton. I want it
I have a JTextField with some text. When I click text field the cursor
I have a textfield where the user can type what ever he want into
I have a JLabel, a JButton, and a JTextField all arranged horizontally in that
I have made a program with a scroll pane, but it is not working.
I have done an layout in my GUI but it did not work ,
In my program, I have a JTextField reading the user input. The user is
I have a program that uses three JTextField fields as the main data entry
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.