Skip to content

Commit

Permalink
Table: select all text in cell editor when starting editing using `F2…
Browse files Browse the repository at this point in the history
…` key; can be disabled using `Table.editorSelectAllOnStartEditing` (issue 652)

also added missing `Table.paintOutsideAlternateRows`
  • Loading branch information
DevCharly committed Feb 19, 2024
1 parent a8e7189 commit 2972300
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ FlatLaf Change Log
- Improved log messages for loading fails.
- Fonts: Updated **Inter** to
[v4.0](https://github.com/rsms/inter/releases/tag/v4.0).
- Table: Select all text in cell editor when starting editing using `F2` key.
(issue 652)

#### Fixed bugs

Expand Down
49 changes: 49 additions & 0 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTableUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Map;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JViewport;
import javax.swing.LookAndFeel;
import javax.swing.SwingConstants;
Expand Down Expand Up @@ -89,6 +93,7 @@
* @uiDefault Table.selectionInactiveBackground Color
* @uiDefault Table.selectionInactiveForeground Color
* @uiDefault Table.paintOutsideAlternateRows boolean
* @uiDefault Table.editorSelectAllOnStartEditing boolean
*
* <!-- FlatTableCellBorder -->
*
Expand Down Expand Up @@ -284,6 +289,18 @@ public void focusLost( FocusEvent e ) {
};
}

@Override
protected void installKeyboardActions() {
super.installKeyboardActions();

if( UIManager.getBoolean( "Table.editorSelectAllOnStartEditing" ) ) {
// get shared action map, used for all tables
ActionMap map = SwingUtilities.getUIActionMap( table );
if( map != null )
StartEditingAction.install( map, "startEditing" );
}
}

/** @since 2 */
protected void installStyle() {
try {
Expand Down Expand Up @@ -579,4 +596,36 @@ protected void setValue( Object value ) {
selected = (value != null && (Boolean) value);
}
}

//---- class StartEditingAction -------------------------------------------

private static class StartEditingAction
extends FlatUIAction
{
static void install( ActionMap map, String key ) {
Action oldAction = map.get( key );
if( oldAction == null || oldAction instanceof StartEditingAction )
return; // not found or already installed

map.put( key, new StartEditingAction( oldAction ) );
}

private StartEditingAction( Action delegate ) {
super( delegate );
}

@Override
public void actionPerformed( ActionEvent e ) {
JTable table = (JTable) e.getSource();

Component oldEditorComp = table.getEditorComponent();

delegate.actionPerformed( e );

// select all text in editor if editing starts with F2 key
Component editorComp = table.getEditorComponent();
if( oldEditorComp == null && editorComp instanceof JTextField )
((JTextField)editorComp).selectAll();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,8 @@ Table.rowHeight = 20
Table.showHorizontalLines = false
Table.showVerticalLines = false
Table.showTrailingVerticalLine = false
Table.paintOutsideAlternateRows = false
Table.editorSelectAllOnStartEditing = true
Table.consistentHomeEndKeyBehavior = true
Table.intercellSpacing = 0,0
Table.scrollPaneBorder = com.formdev.flatlaf.ui.FlatScrollPaneBorder
Expand Down
2 changes: 2 additions & 0 deletions flatlaf-testing/dumps/uidefaults/FlatDarkLaf_1.8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ Table.dropCellBackground [lazy] #3c588b HSL 219 40 39 javax.swing.pl
Table.dropCellForeground [lazy] #bbbbbb HSL 0 0 73 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineColor [lazy] #6d8ac0 HSL 219 40 59 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineShortColor [lazy] #b4c3df HSL 219 40 79 javax.swing.plaf.ColorUIResource [UI]
Table.editorSelectAllOnStartEditing true
Table.focusCellBackground #46494b HSL 204 3 28 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellForeground #bbbbbb HSL 0 0 73 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellHighlightBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Focused [UI] lineColor=#6d8ac0 HSL 219 40 59 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000
Expand All @@ -1119,6 +1120,7 @@ Table.font [active] $defaultFont [UI]
Table.foreground #bbbbbb HSL 0 0 73 javax.swing.plaf.ColorUIResource [UI]
Table.gridColor #5a5e60 HSL 200 3 36 javax.swing.plaf.ColorUIResource [UI]
Table.intercellSpacing 0,0 javax.swing.plaf.DimensionUIResource [UI]
Table.paintOutsideAlternateRows false
Table.rowHeight 20
Table.scrollPaneBorder [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatScrollPaneBorder [UI]
Table.selectionBackground #4b6eaf HSL 219 40 49 javax.swing.plaf.ColorUIResource [UI]
Expand Down
2 changes: 2 additions & 0 deletions flatlaf-testing/dumps/uidefaults/FlatLightLaf_1.8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ Table.dropCellBackground [lazy] #3f8fd9 HSL 209 67 55 javax.swing.pl
Table.dropCellForeground [lazy] #ffffff HSL 0 0 100 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineColor [lazy] #6aa7e1 HSL 209 66 65 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineShortColor [lazy] #15416a HSL 209 67 25 javax.swing.plaf.ColorUIResource [UI]
Table.editorSelectAllOnStartEditing true
Table.focusCellBackground #ffffff HSL 0 0 100 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellForeground #000000 HSL 0 0 0 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellHighlightBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Focused [UI] lineColor=#15416a HSL 209 67 25 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000
Expand All @@ -1124,6 +1125,7 @@ Table.font [active] $defaultFont [UI]
Table.foreground #000000 HSL 0 0 0 javax.swing.plaf.ColorUIResource [UI]
Table.gridColor #ebebeb HSL 0 0 92 javax.swing.plaf.ColorUIResource [UI]
Table.intercellSpacing 0,0 javax.swing.plaf.DimensionUIResource [UI]
Table.paintOutsideAlternateRows false
Table.rowHeight 20
Table.scrollPaneBorder [lazy] 1,1,1,1 false com.formdev.flatlaf.ui.FlatScrollPaneBorder [UI]
Table.selectionBackground #2675bf HSL 209 67 45 javax.swing.plaf.ColorUIResource [UI]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ OS Windows 10
#---- javax.swing.JMenuBar ----

1 javax.swing.plaf.basic.LazyActionMap [UI]
takeFocus com.formdev.flatlaf.ui.FlatMenuBarUI$TakeFocus (null)
takeFocus com.formdev.flatlaf.ui.FlatMenuBarUI$TakeFocusAction


#---- javax.swing.JMenuItem ----
Expand Down Expand Up @@ -613,7 +613,7 @@ OS Windows 10
selectPreviousRowCell javax.swing.plaf.basic.BasicTableUI$Actions
selectPreviousRowChangeLead javax.swing.plaf.basic.BasicTableUI$Actions
selectPreviousRowExtendSelection javax.swing.plaf.basic.BasicTableUI$Actions
startEditing javax.swing.plaf.basic.BasicTableUI$Actions
startEditing com.formdev.flatlaf.ui.FlatTableUI$StartEditingAction
toggleAndAnchor javax.swing.plaf.basic.BasicTableUI$Actions


Expand Down
2 changes: 2 additions & 0 deletions flatlaf-testing/dumps/uidefaults/FlatMacDarkLaf_1.8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ Table.dropCellBackground [lazy] #003f99 HSL 215 100 30 javax.swing.pl
Table.dropCellForeground [lazy] #ffffff HSL 0 0 100 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineColor [lazy] #0069ff HSL 215 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineShortColor [lazy] #66a5ff HSL 215 100 70 javax.swing.plaf.ColorUIResource [UI]
Table.editorSelectAllOnStartEditing true
Table.focusCellBackground #282828 HSL 0 0 16 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellForeground #dddddd HSL 0 0 87 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellHighlightBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Focused [UI] lineColor=#0069ff HSL 215 100 50 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000
Expand All @@ -1129,6 +1130,7 @@ Table.font [active] $defaultFont [UI]
Table.foreground #dddddd HSL 0 0 87 javax.swing.plaf.ColorUIResource [UI]
Table.gridColor #3c3c3c HSL 0 0 24 javax.swing.plaf.ColorUIResource [UI]
Table.intercellSpacing 0,0 javax.swing.plaf.DimensionUIResource [UI]
Table.paintOutsideAlternateRows false
Table.rowHeight 20
Table.scrollPaneBorder [lazy] 3,3,3,3 false com.formdev.flatlaf.ui.FlatScrollPaneBorder [UI]
Table.selectionBackground #0054cc HSL 215 100 40 javax.swing.plaf.ColorUIResource [UI]
Expand Down
2 changes: 2 additions & 0 deletions flatlaf-testing/dumps/uidefaults/FlatMacLightLaf_1.8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ Table.dropCellBackground [lazy] #1a79ff HSL 215 100 55 javax.swing.pl
Table.dropCellForeground [lazy] #ffffff HSL 0 0 100 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineColor [lazy] #4d97ff HSL 215 100 65 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineShortColor [lazy] #003580 HSL 215 100 25 javax.swing.plaf.ColorUIResource [UI]
Table.editorSelectAllOnStartEditing true
Table.focusCellBackground #ffffff HSL 0 0 100 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellForeground #262626 HSL 0 0 15 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellHighlightBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Focused [UI] lineColor=#003580 HSL 215 100 25 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000
Expand All @@ -1133,6 +1134,7 @@ Table.font [active] $defaultFont [UI]
Table.foreground #262626 HSL 0 0 15 javax.swing.plaf.ColorUIResource [UI]
Table.gridColor #ebebeb HSL 0 0 92 javax.swing.plaf.ColorUIResource [UI]
Table.intercellSpacing 0,0 javax.swing.plaf.DimensionUIResource [UI]
Table.paintOutsideAlternateRows false
Table.rowHeight 20
Table.scrollPaneBorder [lazy] 3,3,3,3 false com.formdev.flatlaf.ui.FlatScrollPaneBorder [UI]
Table.selectionBackground #005fe6 HSL 215 100 45 javax.swing.plaf.ColorUIResource [UI]
Expand Down
2 changes: 2 additions & 0 deletions flatlaf-testing/dumps/uidefaults/FlatTestLaf_1.8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,7 @@ Table.dropCellBackground #ff0000 HSL 0 100 50 javax.swing.plaf.Colo
Table.dropCellForeground #00ff00 HSL 120 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineColor #0000ff HSL 240 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.dropLineShortColor #ffff00 HSL 60 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.editorSelectAllOnStartEditing true
Table.focusCellBackground #fffff0 HSL 60 100 97 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellForeground #ff0000 HSL 0 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.focusCellHighlightBorder [lazy] 2,3,2,3 false com.formdev.flatlaf.ui.FlatTableCellBorder$Focused [UI] lineColor=#ff0000 HSL 0 100 50 javax.swing.plaf.ColorUIResource [UI] lineThickness=1.000000
Expand All @@ -1157,6 +1158,7 @@ Table.font [active] $defaultFont [UI]
Table.foreground #ff0000 HSL 0 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.gridColor #00ffff HSL 180 100 50 javax.swing.plaf.ColorUIResource [UI]
Table.intercellSpacing 0,0 javax.swing.plaf.DimensionUIResource [UI]
Table.paintOutsideAlternateRows false
Table.rowHeight 25
Table.scrollPaneBorder [lazy] 1,9,1,9 false com.formdev.flatlaf.ui.FlatScrollPaneBorder [UI]
Table.selectionBackground #00aa00 HSL 120 100 33 javax.swing.plaf.ColorUIResource [UI]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,7 @@ Table.dropCellBackground
Table.dropCellForeground
Table.dropLineColor
Table.dropLineShortColor
Table.editorSelectAllOnStartEditing
Table.focusCellBackground
Table.focusCellForeground
Table.focusCellHighlightBorder
Expand All @@ -918,6 +919,7 @@ Table.font
Table.foreground
Table.gridColor
Table.intercellSpacing
Table.paintOutsideAlternateRows
Table.rowHeight
Table.scrollPaneBorder
Table.selectionBackground
Expand Down

0 comments on commit 2972300

Please sign in to comment.