Mercurial Hosting > junotu
changeset 46:487e560524c2
TabSimpleSearch: Simplified action listener usage
author | Fox |
---|---|
date | Sun, 30 Oct 2022 02:50:03 +0200 |
parents | 1cad42247892 |
children | bad4e8bb769e |
files | src/junotu/TabSimpleSearch.java |
diffstat | 1 files changed, 28 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/src/junotu/TabSimpleSearch.java Sun Oct 30 02:41:28 2022 +0200 +++ b/src/junotu/TabSimpleSearch.java Sun Oct 30 02:50:03 2022 +0200 @@ -29,9 +29,12 @@ import junotu.Card; import junotu.CardWidget; -public class TabSimpleSearch extends JPanel { +public class TabSimpleSearch extends JPanel implements ActionListener { + + private final String KEY_ACTION_COMMIT = "commit"; private JTextField field; + private JButton create; private Box results; JScrollPane scroll; @@ -42,7 +45,7 @@ JPanel top = new JPanel( new BorderLayout() ); field = new JTextField(); - JButton create = new JButton("+"); + create = new JButton("+"); results = Box.createVerticalBox(); scroll = new JScrollPane( results ); @@ -63,16 +66,7 @@ search(); - create.addActionListener( - new ActionListener() - { - @Override - public void actionPerformed( ActionEvent e ) - { - buttonClickedCreate(); - } - } - ); + create.addActionListener(this); field.getDocument().addDocumentListener( new DocumentListener() @@ -98,22 +92,7 @@ } ); - registerKeyboardAction( - new ActionListener() { - @Override - public void actionPerformed( ActionEvent e ) - { - try { - Main.database.databaseCommit(); - } catch( Exception ex ) { - System.out.print( "Failed to write database: "+ex.getMessage()+"\n" ); - } - } - }, - null, - KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK ), - WHEN_IN_FOCUSED_WINDOW - ); + registerKeyboardAction( this, KEY_ACTION_COMMIT, KeyStroke.getKeyStroke( KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK ), WHEN_IN_FOCUSED_WINDOW ); } @@ -193,5 +172,26 @@ Window window = (Window)this.getTopLevelAncestor(); Main.actionCardCreate( window ); } + + public void actionPerformed( ActionEvent e ) + { + Object source = e.getSource(); + if( source == this ) { + switch( e.getActionCommand() ){ + + case KEY_ACTION_COMMIT: { + try { + Main.database.databaseCommit(); + } catch( Exception ex ) { + System.out.print( "Failed to write database: "+ex.getMessage()+"\n" ); + } + break; + } + + } + } else if( source == create ) { + buttonClickedCreate(); + } + } }