简体   繁体   中英

Workaround for JFormattedTextField delete bug in Java for Mac OS X 10.6 Update 2 (1.6.0_20)

There is apparently a bug introduced in the latest Java update for Mac OS X, which causes deletes in JFormattedTextFields to be performed twice. See http://lists.apple.com/archives/java-dev/2010/May/msg00092.html

The DefaultEditorKit.deletePrevCharAction is invoked twice when the delete key is pressed.

Are there any suggestions for a workaround?

I'm thinking of replacing the delete action for my text fields with a patched version that somehow filters out these duplicate invocations.

My workaround, that seems to be working quite well:

public class PatchedTextField extends JFormattedTextField {

    public PatchedTextField() {
        super();

        final Action originalDeleteAction =
            getActionMap().get(DefaultEditorKit.deletePrevCharAction);

        getActionMap().put(DefaultEditorKit.deletePrevCharAction,
            new AbstractAction() {
                ActionEvent previousEvent;

                public void actionPerformed(ActionEvent e) {
                // Filter out events that happen within 1 millisecond from each other
                if (previousEvent == null || e.getWhen() - previousEvent.getWhen() > 1) {
                    originalDeleteAction.actionPerformed(e);
                }
                previousEvent = e;
            }
        });
    }
}

The only downside that I have found so far is that you cannot delete more than one character per millisecond.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM