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

  • Home
  • SEARCH
  • 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 8386763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:01:46+00:00 2026-06-09T18:01:46+00:00

I run into a problem using form default button when it (form) includes JFormattedTextField

  • 0

I run into a problem using form default button when it (form) includes JFormattedTextField.
On a form with such a field, if it happens to have focus (and was changed), you have to press OK twice to get your default button ‘pressed’. I think I know why it happens – it’s because first Enter gets consumed in Commit processing.

I was also able to make a workaround – if you change Formatter to commit on each valid edit, then you will get proper behavior, but this a) forces you to specify formatters explicilty, and b) it’s not possible to revert to ‘old’ value (eg. using Escape, or programatically).

Code below demonstrates that: when you run it field at the top commits on each edit and works with single Enter (but you cannot revert), field at the bottom allows reverts, but needs two Enters if edited.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DateFormatter;

public class ExFrame extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ExFrame frame = new ExFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public ExFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JFormattedTextField ff_1, ff_2;

        //ff_1 has modified behavior of commit on each (valid) edit
        DateFormatter f=new DateFormatter();
        f.setCommitsOnValidEdit(true);

        ff_1 = new JFormattedTextField(f);
        ff_1.setValue(new Date());

        //ff_2 has default behavior 
        ff_2 = new JFormattedTextField(new Date());

        contentPane.add(ff_1, BorderLayout.NORTH);
        contentPane.add(ff_2, BorderLayout.SOUTH);

        JButton btnDefault = new JButton("I am default button");
        contentPane.add(btnDefault, BorderLayout.CENTER);
        getRootPane().setDefaultButton(btnDefault);
    }
}

So the question is: Is there a way to get JFormattedTextField both commit on Enter (so input is verified but only once) and if succesfully validated, activate default button (with single press)?

  • 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-09T18:01:47+00:00Added an answer on June 9, 2026 at 6:01 pm

    The basic trick is the fool the keyBinding mechanism into believing that the keyStroke wasn’t handled by the textField. To achieve that, we need to subclass the JFormattedTextField and override its processKeyBindings to return false (meaning: “couldn’t use”).

    Something like:

    JFormattedTextField multiplex = new JFormattedTextField() {
        KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
        @Override
        protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
                                            int condition, boolean pressed) {
            boolean processed = super.processKeyBinding(ks, e, condition,
                                                        pressed);
    
            if (processed && condition != JComponent.WHEN_IN_FOCUSED_WINDOW
                    && enter.equals(ks)) {
                // Returning false will allow further processing
                // of the bindings, eg our parent Containers will get a
                // crack at them and f.i. route to a default button.
                return !isEditValid();
            }
            return processed;
        }
    
    };
    multiplex.setValue(new Date());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into a problem with using two models for my form in Django.
I run into an interesting problem while was using combos in input form. My
I have run into a strange problem on Android , using jQuery mobile and
I'm fairly new to using MSSQL and have run into a weird problem. Given
I've recently started using GNU Screen but have run into a very annoying problem.
Friends, I'm converting an Oracle Form to Apex and have run into problem(s?) which
I'm using Thauber's lovely Django schedule app , but have run into a problem:
I'm using MySQL and InnoDB and I've run into some problem recently. This is
I’m new to Flex and Cairngorm.While I’m using ServiceLocator,I do run into the problem:
So I'm using PLUpload to, well, upload files. However, I've run into a problem.

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.