问题
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