FlatTestFrame: register F1, F2 and F3 keys to switch to Light, Dark or Test LaF

This commit is contained in:
Karl Tauber
2019-08-26 13:01:32 +02:00
parent c3ce469389
commit 82bc03d617

View File

@@ -91,6 +91,11 @@ public class FlatTestFrame
lookAndFeelComboBox.setModel( lafModel ); lookAndFeelComboBox.setModel( lafModel );
// register F1, F2 and F3 keys to switch to Light, Dark or Test LaF
registerSwitchToLookAndFeel( KeyEvent.VK_F1, FlatLightLaf.class.getName() );
registerSwitchToLookAndFeel( KeyEvent.VK_F2, FlatDarkLaf.class.getName() );
registerSwitchToLookAndFeel( KeyEvent.VK_F3, FlatTestLaf.class.getName() );
// register ESC key to close frame // register ESC key to close frame
((JComponent)getContentPane()).registerKeyboardAction( ((JComponent)getContentPane()).registerKeyboardAction(
e -> { e -> {
@@ -103,6 +108,15 @@ public class FlatTestFrame
closeButton.addActionListener(e -> dispose()); closeButton.addActionListener(e -> dispose());
} }
private void registerSwitchToLookAndFeel( int keyCode, String lafClassName ) {
((JComponent)getContentPane()).registerKeyboardAction(
e -> {
selectLookAndFeel( lafClassName );
},
KeyStroke.getKeyStroke( keyCode, 0, false ),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
}
protected void showFrame( JComponent content ) { protected void showFrame( JComponent content ) {
this.content = content; this.content = content;
@@ -112,6 +126,13 @@ public class FlatTestFrame
setVisible( true ); setVisible( true );
} }
private void selectLookAndFeel( String lafClassName ) {
DefaultComboBoxModel<LafInfo> lafModel = (DefaultComboBoxModel<LafInfo>) lookAndFeelComboBox.getModel();
int sel = lafModel.getIndexOf( new LafInfo( null, lafClassName ) );
if( sel >= 0 )
lookAndFeelComboBox.setSelectedIndex( sel );
}
private void lookAndFeelChanged() { private void lookAndFeelChanged() {
LafInfo newLaf = (LafInfo) lookAndFeelComboBox.getSelectedItem(); LafInfo newLaf = (LafInfo) lookAndFeelComboBox.getSelectedItem();
if( newLaf == null ) if( newLaf == null )