JFormDesigner / FlatLaf

FlatLaf - Swing Look and Feel (with Darcula/IntelliJ themes support)
https://www.formdev.com/flatlaf/
Apache License 2.0
3.34k stars 266 forks source link

selectAllOnFocusPolicy should apply to _all_ text components, this includes JTextArea (FlatTextAreaUI) #498

Closed jzwolak closed 2 years ago

jzwolak commented 2 years ago

In FlatTextAreaUI.createCaret() the call is made new FlatCaret( null, false ); setting the selectAllOnFocusPolicy to the default. Yet in FlatTextFieldUI.createCaret() the call reads new FlatCaret( "TextComponent.selectAllOnFocusPolicy"), UIManager.getBoolean( "TextComponent.selectAllOnMouseClick" ) );. I think the two calls should be the same; in fact, maybe the two classes should be inheriting a common base class that implements createCaret (and other common methods on text components).

As it stands, JTextArea will always select the text on first focus gained because that is the default behavior and there is no way to change this. JTextField is configurable with the TextComponent.selectAllOnFocusPolicy. I think the policy should apply to JTextArea, too. If not, then a similar configuration options for JTextArea is warranted.

I have a hard time imagining developers wanting to change the policy on JTextField but not JTextArea, so I believe this constitutes a bug and can safely be changed. If differing configurability between the two is desired, then having a separate option seems acceptable. Though I recognize it's more difficult to add a separate option than to use the existing one. Having a separate option also adds complexity to the usability of the code.

DevCharly commented 2 years ago

It is actually a bug (regression in FlatLaf 2.0) that all text is selected in JTextArea, JTextPane and JEditorPane when component is focused for the first time.

Only JTextField and subclasses should select all text when focused. This is how native text fields in various operating systems usually behave. Select all text in multi-line text components usually makes no sense IMHO.

The regression is caused by PR #401 (commit 0156a9a9d51349ce606bceccc3380fcb1e27b814), which added FlatCaret usage to JTextArea, JTextPane and JEditorPane. Before this PR, FlatCaret was only used for JTextField and subclasses.

DevCharly commented 2 years ago

Fixed in FlatLaf 2.1