I would like to add Key bindings to my JFileChooser
in order to open a file preview window when the SPACE key is pressed.
Because of the source code is too big, I just did a simple dirty code :
MainWindow.java
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainWindow extends JFrame {
public MainWindow() {
this.setTitle("Test Window");
Dimension dim = new Dimension(800, 600);
this.setSize(dim);
this.setPreferredSize(dim);
MainPanel pane = new MainPanel(dim);
Action damned = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"It Works !");
}
};
pane.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "damned");
pane.getActionMap().put("damned", damned);
this.setContentPane(pane);
this.setVisible(true);
}
}
MainPanel.java
package test;
import java.awt.*;
import javax.swing.*;
public class MainPanel extends JFileChooser {
public MainPanel(Dimension dim) {
this.setSize(dim);
this.setPreferredSize(dim);
}
}
Test.java
package test;
public class Test {
public static void main(String[] args) {
new MainWindow();
}
}
If I use a JPanel instead of a JFileChooser, it works.
Thank you,
Revan