Java difference in key detection between Windows and Mac

前端 未结 2 1450
北荒
北荒 2020-12-21 14:42

I have a JList with a key listener to make it easy for the user to delete an item from the list. On windows, it works fine. You hit the delete key and the item is removed.

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-21 15:37

    for example

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ListDemo extends JPanel {
    
        private static final long serialVersionUID = 1L;
        private JFrame frame = new JFrame("ListDemo");
        private JList list;
        private DefaultListModel listModel;
    
        public ListDemo() {
            super(new BorderLayout());
            listModel = new DefaultListModel();
            listModel.addElement("Jane Doe");
            listModel.addElement("John Smith");
            listModel.addElement("Kathy Green");
            list = new JList(listModel);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            list.setSelectedIndex(0);
            list.setVisibleRowCount(5);
            JScrollPane listScrollPane = new JScrollPane(list);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(listScrollPane, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
            setKeyBindings();
        }
    
        private void setKeyBindings() {
            list.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
                    .put(KeyStroke.getKeyStroke("DELETE"), "clickDelete");
            list.getActionMap().put("clickDelete", new AbstractAction() {
    
                private static final long serialVersionUID = 1L;
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    int index = list.getSelectedIndex();
                    if (index > -1) {
                        listModel.remove(index);
                    }
                }
            });
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    ListDemo listDemo = new ListDemo();
                }
            });
        }
    }
    

提交回复
热议问题