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 7607139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:35:12+00:00 2026-05-31T00:35:12+00:00

For a program, I was using a KeyListener to add something to an ArrayList

  • 0

For a program, I was using a KeyListener to add something to an ArrayList when pressing the button ‘1’. Objects in this list are being visualised constantly. With the KeyListener, this worked fluently, even when keeping the button pressed.

Later, I added a JMenuBar to the GUI. Adding something to the ArrayList now has an own JMenuItem with its accelerator set to the KeyStroke ‘1’ and an ActionListener which performs the same stuff than the KeyListener before. However, the performance now is very bad. Keeping ‘1’ pressed is going to lag extremely, it’s very slow compared to the KeyListener.

Why is it so slow? Am I doing something wrong? Is there a better way?

    ...
    AL al = new AL();
    menu.add(createMenuItem("Add", KeyEvent.VK_1, al));
}

private JMenuItem createMenuItem(String text, int key, ActionListener al){
    JMenuItem menuItem = new JMenuItem(text);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(key, 0));
    menuItem.addActionListener(al);
    return menuItem;
}

private class AL implements ActionListener{
    public void actionPerformed(ActionEvent e){
        int keycode = ((JMenuItem)e.getSource()).getAccelerator().getKeyCode();
        bla(keycode);
    }
}
  • 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-05-31T00:35:14+00:00Added an answer on May 31, 2026 at 12:35 am

    It looks like the slowdown is how the menu accelerators are handled. It might be L&F or even OS since when I profile it, there is no hotspot in the Java code (WindowsXP) dependent. A workaround could be to add the key binding to the root pane instead of using an menu accelerator.

    Press ‘1’ to trigger KeyListener on button (fast)
    Press ‘2’ to trigger menu accelerator (slow)
    Press ‘3’ to trigger KeyBinding on button (fast)
    Press ‘4’ to trigger KeyBinding on root pane (fast)

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    
    import javax.swing.AbstractAction;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.KeyStroke;
    
    public class TestKeySpeed {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    final JTextArea area = new JTextArea(20, 40);
                    area.setEditable(false);
    
                    JButton button = new JButton("Just something that has focus");
                    button.addKeyListener(new KeyAdapter() {
                        @Override
                        public void keyPressed(KeyEvent e) {
                            if (e.getKeyCode() == KeyEvent.VK_1) {
                                area.append("1");
                            }
                        }
                    });
    
                    AbstractAction action = new AbstractAction("Add") {
                        {
                            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke('2'));
                        }
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            area.append("2");
                        }
                    };
                    button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                            KeyStroke.getKeyStroke('3'), "add3");
                    button.getActionMap().put("add3", action);
    
                    JMenu menu = new JMenu("File");
                    menu.add(action);
                    JMenuBar bar = new JMenuBar();
                    bar.add(menu);
                    JFrame frame = new JFrame("Test");
                    frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
                            KeyStroke.getKeyStroke('4'), "add4");
                    frame.getRootPane().getActionMap().put("add4", action);
    
                    frame.setJMenuBar(bar);
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.getContentPane().add(new JScrollPane(area));
                    frame.getContentPane().add(button, BorderLayout.PAGE_END);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    
                    button.requestFocusInWindow();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to add a KeyListener to the entire UI by using: frame.addKeyListener(this); In
This is a FIFO program using linked list . The program does not give
When programming an OO program using a SQL database back-end, do objects' attributes correspond
I'm using a KeyListener on a JFrame object which I set as FullScreenWindow, something
I'm writing a program using python 2.6 and pyqt4. I want this program to
I have this simple program using powershell. This is just a proof of concept,
This is my first program using Haskell. I'm writing it to put into practice
I created a program using dev-cpp and wxwidgets which solves a puzzle. The user
I wrote a program using AutoIT to fetch information from a number of websites
Suppose I write a program using immutable data structures in Java. Even though it

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.