Criar Atalho!

3 respostas
I

Como crio um atalho para um JMenuItem… por exemplo pro Item Abrir colocar o atalho CTRL+O, Copiar CTRL+C, etc…

e aproveitando o tópico…

alguém pode postar um código pra abrir um arquivo qlqr…e futuramente colocar o arquivo num JTEXTAREA ou JEditorPane???

já olhei alguns exemplos mas naum consegui entender…

Muito Obrigado Galera

3 Respostas

M

cara, atalho eu não sei, mas vc pode usar minemonicos… por ex:

JMenuItem aboutItem = new JMenuItem( "Sobre..." ); aboutItem.setMnemonic( 'S' );

…vai criar um JMenuItem com o label “Sobre…” … e o ‘S’ vai estar sublinhado, ou seja… qnd vc apertar a letra ‘s’ no teclado, ja vai executar o aboutItem…

…e sobre o arquivo, de uma olhada no FAQ… com certeza vc se acha…

http://www.portaljava.com/home/modules.php?name=Encyclopedia&op=terms&eid=12<r=

I

esse eu sei… mas eu queria atalho tipo CTRL + (alguma letra)

igual a todos os programas tem… Word por exemplo… pra abrir é CTRL+A, PotoShop é CTRL+O…

naum tem como fazer isso???

M

bem, vc pode usar um keyListener nesse caso então... ai chama o método apropriado do menu... veja este código, creio q vc vai entender:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class KeyDemo extends JFrame implements KeyListener &#123;

   private String line1 = &quot;&quot;, line2 = &quot;&quot;;
   private String line3 = &quot;&quot;;
   private JTextArea textArea;

   public KeyDemo&#40;&#41;
   &#123;
      super&#40; &quot;Demonstrating Keystroke Events&quot; &#41;;

      textArea = new JTextArea&#40; 10, 15 &#41;;
      textArea.setText&#40; &quot;Pressione qualquer tecla...&quot; &#41;;
      textArea.setEnabled&#40; false &#41;;
      getContentPane&#40;&#41;.add&#40; textArea &#41;;

      addKeyListener&#40; this &#41;;

      setSize&#40; 350, 100 &#41;;
      setVisible&#40; true &#41;;
   &#125;

   public void keyPressed&#40; KeyEvent e &#41;
   &#123;
      line1 = &quot;Key Pressed&#58; &quot; + e.getKeyText&#40; e.getKeyCode&#40;&#41; &#41;;
      setLines2and3&#40; e &#41;;
   &#125;

   public void keyReleased&#40; KeyEvent e &#41;
   &#123;
      line1 = &quot;Key Released&#58; &quot; + e.getKeyText&#40; e.getKeyCode&#40;&#41; &#41;;
      setLines2and3&#40; e &#41;;
   &#125;

   public void keyTyped&#40; KeyEvent e &#41;
   &#123;
     line1 = &quot;Key Typed&#58; &quot; + e.getKeyChar&#40;&#41;;
     setLines2and3&#40; e &#41;;
   &#125;

   private void setLines2and3&#40; KeyEvent e &#41;
   &#123;
      line2 = &quot;This key is &quot; +
         &#40; e.isActionKey&#40;&#41; ? &quot;&quot; &#58; &quot;not &quot; &#41; +
         &quot;ahn action key&quot;;

      String tmp = e.getKeyModifiersText&#40; e.getModifiers&#40;&#41; &#41;;

      line3 = &quot;Modifier keys pressed &quot; +
         &#40; tmp.equals&#40; &quot;&quot; &#41; ? &quot;none&quot; &#58; tmp &#41;;

      textArea.setText&#40; line1 + &quot;\n&quot; + line2 + &quot;\n&quot; + line3 + &quot;\n&quot; &#41;;
   &#125;

   public static void main&#40; String args&#91;&#93; &#41;
   &#123;
      KeyDemo win = new KeyDemo&#40;&#41;;

      win.setDefaultCloseOperation&#40; JFrame.EXIT_ON_CLOSE &#41;;
   &#125;

&#125;
Criado 26 de março de 2004
Ultima resposta 27 de mar. de 2004
Respostas 3
Participantes 2