Java JTable shortcuts not working

僤鯓⒐⒋嵵緔 提交于 2019-12-11 15:11:40

问题


It's my first time using a JTable and I'm having a problem. I have been testing my JTable with a main method like this :

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame f = new JFrame(APP_TITLE);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                FileManager fileManager = new FileManager();
                f.setContentPane(fileManager);

                f.setIconImage(Icons.SOFTWARE_ICON.getImage());

                f.pack();
                f.setLocationByPlatform(true);
                f.setMinimumSize(f.getSize());
                f.setVisible(true);
            }
        });
    }

And everything works perfectly. I'm able to press the shortcuts (UP, DOWN and CTRL+A) and they all work.

But now I've been adding the FileManager (JPanel) to my UI (JFrame) like this :

public FileManager fMan = new FileManager();

[...]

JSplitPane splitPane = new JSplitPane();

splitPane.setLeftComponent(fMan);

And now, I noticed two things :

  • The shortcuts aren't working

  • The colors aren't automatically inverted on selection

I tried requesting the focus, changing the theme, and neither helped. 🤔


回答1:


After over 5 hours of trying to fix this, I finally can proudly say I HAVE FOUND THE ANSWER!

A subclass used this code :

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {

            @Override
            public boolean dispatchKeyEvent(KeyEvent ke) {
            }

        });

And it is causing the JTable to not receive the imputs properly.

Oh well! :)



来源:https://stackoverflow.com/questions/47162625/java-jtable-shortcuts-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!