diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f80f834..7f3b62bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,10 @@ FlatLaf Change Log to issue #382; regression since fixing #330 in FlatLaf 1.4) - Tree: Fixed editing cell issue with custom cell renderer and cell editor that use same component for rendering and editing. (issue #385) +- Table: Do not select text in cell editor when it gets focus (when + `JTable.surrendersFocusOnKeystroke` is `true`) and + `TextComponent.selectAllOnFocusPolicy` is `once` (the default) or `always`. + (issue #395) ## 1.6.1 diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java index 5553ea1c..b6d4b7b0 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatCaret.java @@ -108,7 +108,7 @@ public class FlatCaret protected void selectAllOnFocusGained() { JTextComponent c = getComponent(); Document doc = c.getDocument(); - if( doc == null || !c.isEnabled() || !c.isEditable() ) + if( doc == null || !c.isEnabled() || !c.isEditable() || FlatUIUtils.isCellEditor( c ) ) return; Object selectAllOnFocusPolicy = c.getClientProperty( SELECT_ALL_ON_FOCUS_POLICY ); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java index 0b7d09c3..c0814b18 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.java @@ -330,6 +330,11 @@ public class FlatComponents2Test } } + private void focusCellEditorChanged() { + for( JTable table : allTables ) + table.setSurrendersFocusOnKeystroke( focusCellEditorCheckBox.isSelected() ); + } + private void treeRendererChanged() { Object sel = treeRendererComboBox.getSelectedItem(); if( !(sel instanceof String) ) @@ -477,6 +482,7 @@ public class FlatComponents2Test sortIconPositionComboBox = new JComboBox<>(); showHorizontalLinesCheckBox = new JCheckBox(); rowSelectionCheckBox = new JCheckBox(); + focusCellEditorCheckBox = new JCheckBox(); showVerticalLinesCheckBox = new JCheckBox(); columnSelectionCheckBox = new JCheckBox(); intercellSpacingCheckBox = new JCheckBox(); @@ -490,7 +496,7 @@ public class FlatComponents2Test // columns "[]" + "[200,grow,sizegroup 1,fill]" + - "[200,grow,sizegroup 1,fill]" + + "[200,grow,sizegroup 1,fill]para" + "[fill]" + "[200,grow,sizegroup 1,fill]" + "[200,grow,sizegroup 1,fill]", @@ -790,6 +796,7 @@ public class FlatComponents2Test "hidemode 3", // columns "[]" + + "[fill]" + "[fill]", // rows "[]" + @@ -800,7 +807,7 @@ public class FlatComponents2Test //---- autoResizeModeLabel ---- autoResizeModeLabel.setText("Auto resize mode:"); - tableOptionsPanel.add(autoResizeModeLabel, "cell 0 0 2 1"); + tableOptionsPanel.add(autoResizeModeLabel, "cell 0 0 3 1"); //---- autoResizeModeField ---- autoResizeModeField.setModel(new DefaultComboBoxModel<>(new String[] { @@ -812,11 +819,11 @@ public class FlatComponents2Test })); autoResizeModeField.setSelectedIndex(2); autoResizeModeField.addActionListener(e -> autoResizeModeChanged()); - tableOptionsPanel.add(autoResizeModeField, "cell 0 0 2 1"); + tableOptionsPanel.add(autoResizeModeField, "cell 0 0 3 1"); //---- sortIconPositionLabel ---- sortIconPositionLabel.setText("Sort icon:"); - tableOptionsPanel.add(sortIconPositionLabel, "cell 0 0 2 1"); + tableOptionsPanel.add(sortIconPositionLabel, "cell 0 0 3 1"); //---- sortIconPositionComboBox ---- sortIconPositionComboBox.setModel(new DefaultComboBoxModel<>(new String[] { @@ -826,7 +833,7 @@ public class FlatComponents2Test "bottom" })); sortIconPositionComboBox.addActionListener(e -> sortIconPositionChanged()); - tableOptionsPanel.add(sortIconPositionComboBox, "cell 0 0 2 1"); + tableOptionsPanel.add(sortIconPositionComboBox, "cell 0 0 3 1"); //---- showHorizontalLinesCheckBox ---- showHorizontalLinesCheckBox.setText("show horizontal lines"); @@ -839,6 +846,11 @@ public class FlatComponents2Test rowSelectionCheckBox.addActionListener(e -> rowSelectionChanged()); tableOptionsPanel.add(rowSelectionCheckBox, "cell 1 1"); + //---- focusCellEditorCheckBox ---- + focusCellEditorCheckBox.setText("focus cell editor"); + focusCellEditorCheckBox.addActionListener(e -> focusCellEditorChanged()); + tableOptionsPanel.add(focusCellEditorCheckBox, "cell 2 1"); + //---- showVerticalLinesCheckBox ---- showVerticalLinesCheckBox.setText("show vertical lines"); showVerticalLinesCheckBox.addActionListener(e -> showVerticalLinesChanged()); @@ -867,7 +879,7 @@ public class FlatComponents2Test //---- tableHeaderButtonCheckBox ---- tableHeaderButtonCheckBox.setText("show button in table header"); tableHeaderButtonCheckBox.addActionListener(e -> tableHeaderButtonChanged()); - tableOptionsPanel.add(tableHeaderButtonCheckBox, "cell 1 4"); + tableOptionsPanel.add(tableHeaderButtonCheckBox, "cell 1 4 2 1"); } add(tableOptionsPanel, "cell 4 4 2 1"); // JFormDesigner - End of component initialization //GEN-END:initComponents @@ -897,6 +909,7 @@ public class FlatComponents2Test private JComboBox sortIconPositionComboBox; private JCheckBox showHorizontalLinesCheckBox; private JCheckBox rowSelectionCheckBox; + private JCheckBox focusCellEditorCheckBox; private JCheckBox showVerticalLinesCheckBox; private JCheckBox columnSelectionCheckBox; private JCheckBox intercellSpacingCheckBox; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.jfd index 8cf889d1..3f0f32b1 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatComponents2Test.jfd @@ -1,4 +1,4 @@ -JFDML JFormDesigner: "7.0.4.0.360" Java: "16" encoding: "UTF-8" +JFDML JFormDesigner: "7.0.5.0.382" Java: "16" encoding: "UTF-8" new FormModel { contentType: "form/swing" @@ -8,7 +8,7 @@ new FormModel { } add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$layoutConstraints": "ltr,insets dialog,hidemode 3" - "$columnConstraints": "[][200,grow,sizegroup 1,fill][200,grow,sizegroup 1,fill][fill][200,grow,sizegroup 1,fill][200,grow,sizegroup 1,fill]" + "$columnConstraints": "[][200,grow,sizegroup 1,fill][200,grow,sizegroup 1,fill]para[fill][200,grow,sizegroup 1,fill][200,grow,sizegroup 1,fill]" "$rowConstraints": "[][150,grow,sizegroup 1,fill][150,grow,sizegroup 1,fill][150,grow,sizegroup 1,fill][fill]" } ) { name: "this" @@ -372,7 +372,7 @@ new FormModel { } ) add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { "$layoutConstraints": "hidemode 3" - "$columnConstraints": "[][fill]" + "$columnConstraints": "[][fill][fill]" "$rowConstraints": "[][]0[]0[]0[]0" } ) { name: "tableOptionsPanel" @@ -381,7 +381,7 @@ new FormModel { name: "autoResizeModeLabel" "text": "Auto resize mode:" }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 0 2 1" + "value": "cell 0 0 3 1" } ) add( new FormComponent( "javax.swing.JComboBox" ) { name: "autoResizeModeField" @@ -400,13 +400,13 @@ new FormModel { } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "autoResizeModeChanged", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 0 2 1" + "value": "cell 0 0 3 1" } ) add( new FormComponent( "javax.swing.JLabel" ) { name: "sortIconPositionLabel" "text": "Sort icon:" }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 0 2 1" + "value": "cell 0 0 3 1" } ) add( new FormComponent( "javax.swing.JComboBox" ) { name: "sortIconPositionComboBox" @@ -422,7 +422,7 @@ new FormModel { } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "sortIconPositionChanged", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 0 0 2 1" + "value": "cell 0 0 3 1" } ) add( new FormComponent( "javax.swing.JCheckBox" ) { name: "showHorizontalLinesCheckBox" @@ -445,6 +445,16 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 1" } ) + add( new FormComponent( "javax.swing.JCheckBox" ) { + name: "focusCellEditorCheckBox" + "text": "focus cell editor" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "focusCellEditorChanged", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 2 1" + } ) add( new FormComponent( "javax.swing.JCheckBox" ) { name: "showVerticalLinesCheckBox" "text": "show vertical lines" @@ -503,14 +513,14 @@ new FormModel { } addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "tableHeaderButtonChanged", false ) ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { - "value": "cell 1 4" + "value": "cell 1 4 2 1" } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 4 4 2 1" } ) }, new FormLayoutConstraints( null ) { "location": new java.awt.Point( 0, 0 ) - "size": new java.awt.Dimension( 1000, 600 ) + "size": new java.awt.Dimension( 1095, 610 ) } ) } }