JTable enter key

后端 未结 2 560
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 09:12

I am developing an application using jTable for inventory management.

The action is, by typing the item code in a jTextField and by pressin

2条回答
  •  萌比男神i
    2020-11-30 10:01

    You can use Java 8 lambda functions:

    final String tustakmaad = "Solve";
    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
            panel.getTblBelgetarihiliste().getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                enter, tustakmaad);
            panel.getTblBelgetarihiliste().getActionMap().put(tustakmaad, new DelegateAction(
                ae -> eventMytable_enterkey()));
    

    and DelegateAction class should be like this:

    package com.ozpas.entegre.controller;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.AbstractAction;
    
    public class DelegateAction extends AbstractAction {
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        ActionListener myaction = (ae) -> {
            System.out.println("empty action");
        };
    
        public DelegateAction(ActionListener customaction) {
            this.myaction = customaction;
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            myaction.actionPerformed(e);
        }
    
        public ActionListener getMyaction() {
            return myaction;
        }
    
        public void setMyaction(ActionListener myaction) {
            this.myaction = myaction;
        }
    
    }
    

提交回复
热议问题