Добрый вечер. Пытаюсь приделать комбинацию клавиш на пункт меню "Открыть файл" в созданном мною блокноте.
Итак. Обычным кликом мыши я задействую такой механизм:
//JMenuItem openF - объект пункта верхнего меню "Открыть файл" моего блокнота
openF.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JFileChooser fc = new JFileChooser();
int value = fc.showOpenDialog(null);
if(value == JFileChooser.APPROVE_OPTION){
File file = fc.getSelectedFile();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuilder mainLine = new StringBuilder();
String inputText;
while((inputText = br.readLine())!=null){
mainLine.append(inputText);
}
outputText = mainLine.toString();
System.out.println(outputText);
jTextArea.setText(outputText);
} catch (FileNotFoundException ex) {
Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Notepad.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
Теперь я хочу сделать туже операцию, но при нажимании клавиш CTRL+O. Приделываю KeyListener к JTextArea jTextArea
jTextArea.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e ){
if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_O){
//что здесь должно быть?? конечно же не по новой создавать
//JFileChooser. как воспользоваться уже существующим? Его нужно //было поместить в какой то метод, прежде чем использовать его выше?
}
}
});
Очень признателен за помощь