Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: TextField problem in Mac, it keeps selecting the text



On Jul 1, 2008, at 8:15 AM, email@hidden wrote:

Mike Swingler wrote:

On Jun 30, 2008, at 8:31 PM, email@hidden wrote:

There is an amazingly stupid problem with swing TextField in Mac version
of JVM.


If you try a simple jTextField.setText("Hello") method in Java it works
fine in windows, but in Mac OS it selects/highlights the entire text.
Even if I use:
jTextField.setSelectionStart(0)
jTextField.setSelectionEnd(0)


it continues to select the entire text. However if I put:

jTextField.setSelectionStart(1)
jTextField.setSelectionEnd(2)

it behaves correctly but only selecting the 2nd character. Why is this?!

This was done to match the native "select all on focus" behavior present in Mac OS X text widgets. If you move focus between textfields on Mac OS X, you will find that they always select their contents when gaining focus. In Java, we also attempt to preserve the text field selection and the caret location if it has been customized by the user. As pointed out by another developer here, this has been documented in the release notes for Java on Mac OS X 10.5 Leopard: http://developer.apple.com/releasenotes/Java/JavaLeopardRN/ResolvedIssues/chapter_3_section_5.html#4890361


Thanks Mike,
Amazing, I spent hours trying to figure out this bug and now realize that it is done intentionally. For my application it is critical that the cursor stays where it was last time it had the focus! What is the workaround?

As Tilman asked essentially the same question yesterday, the workaround is the same: textfield.setCaret(new DefaultCaret()). If you are doing any sort of advanced selection manipulation, you might want to look into installing your own custom subclass of DefaultCaret as well.


I can understand that Mac behaves in certain ways but I do not believe that the portability of Java should have been scarified! We Java programmers expect JVM to behave exactly the same in different operating systems. If you pick and choose, you will confuse us and discourage us from porting our application to Mac because of the extra coding involved. As insignificant as this issue may seem it rendered my entire product useless with tons of complains from my Mac users.

Textfield selection behavior though is something that is squarely in the domain of "Look and Feel". If you are looking for a consistent appearance and behavior, you should use the cross-platform Metal look and feel.


For the number of developers who receive complaints that their application does something unexpected on the Mac (because they aren't testing on a platform they are deploying on), there are many many more developers who appreciate that their application just does "the right thing" when they use the system look and feel on the Mac. It would be a detriment to our platform to simply port over the appearance of the Mac controls, and not carry along the subtle behaviors and interactions of native Cocoa applications - the appearance and the behaviors are inextricably linked.

Hope this helps fix your immediate problem, and gives some guidance to our overall philosophy to Aqua and Java,
Mike Swingler
Java Runtime Engineer
Apple Inc.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden


This email sent to email@hidden
References: 
 >Re: TextField problem in Mac, it keeps selecting the text (From: "email@hidden" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.