Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7733315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:57:25+00:00 2026-06-01T06:57:25+00:00

I have a JList with a key listener to make it easy for the

  • 0

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. On mac, the program does not respond to the delete key. I am using KeyEvent.VK_DELETE and I thought this was a platform neutral way of detecting special keys. Is there a different way I should be detecting the key press on the Mac?

    studentJList.setModel(studentListModel);  // a custom model I wrote
    studentJList.addKeyListener(new KeyListener() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                studentListModel.remove(studentJList.getSelectedIndex());
                studentJList.revalidate();
            }
        }

        @Override
        public void keyReleased(KeyEvent e) { }

        @Override
        public void keyTyped(KeyEvent e) { }
    });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:57:27+00:00Added an answer on June 1, 2026 at 6:57 am

    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();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JList, and a JButton, user can click an item in the
Currently, I have a JList listen to list selection listener. private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt)
I have a JList with a DefaultListModel . How I can make an item
I have a list of items in a JList for the user to select.
I have a JList inside a Scrollpane. If you click on the list and
I have this method here that gets an object from a JList. The object
I have a JList and JTextArea pair. Based on the selected item on JList
I have a JList and I use a DefaultListModel to store the list entries.
how can i make a swing JList to have its items selected with any
I have a jlist of users. In which some user has presence 1st and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.