Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Why is menu accelerator keystrokes not consumed?



Hi,

I have a problem with menu accelerator keystrokes in combination with JTables. I have attached a small program to illustrate the problem. When I select a row in the JTable and press F1 (which is the my accelerator), two things happen: The corresponding menu item is selected AND the JTable enters edit mode on the cell in focus. Obviously, I want the accelerator keystroke to be consumed by the menu bar.

Maybe this is not the right mailing list for this, because it seems to work ok, if I set apple.laf.useScreenMenuBar=true. If I don't set this property, it doesn't work on the Mac or any other platform I've tried.

Thanks,
Erik Schjott

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;

public class MenuAcceleratorTest {
   JTable output;
   JScrollPane scrollPane;

   public JMenuBar createMenuBar() {
      JMenuBar menuBar;
      JMenu menu;
      JMenuItem menuItem;

      menuBar = new JMenuBar();
      menu = new JMenu("Menu");
      menuItem = new JMenuItem("Test");
      menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
      menuItem.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
         }
      });
      menu.add(menuItem);

      menuBar.add(menu);
      
      return menuBar;
   }

   public Container createContentPane() {
      JPanel contentPane = new JPanel(new BorderLayout());
      contentPane.setOpaque(true);
      
      TableModel dataModel = new AbstractTableModel() {
         public int getColumnCount() {
            return 10;
         }
         public int getRowCount() {
            return 10;
         }
         public Object getValueAt(int row, int col) {
            return new Integer(row * col);
         }
         public boolean isCellEditable(int row, int col) {
            return true;
         }
      };
      output = new JTable(dataModel);
      scrollPane = new JScrollPane(output);

      contentPane.add(scrollPane, BorderLayout.CENTER);

      return contentPane;
   }

   private static void createAndShowGUI() {
      JFrame.setDefaultLookAndFeelDecorated(true);

      JFrame frame = new JFrame("Menu Accelerator Test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      MenuAcceleratorTest demo = new MenuAcceleratorTest();
      frame.setJMenuBar(demo.createMenuBar());
      frame.setContentPane(demo.createContentPane());

      frame.setSize(500, 200);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      System.setProperty("apple.laf.useScreenMenuBar", "true");
      javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

References: 
 >Mouse events changed in 1.4.2_05? (From: Edward van Meeuwen <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.