Problem with KeyEvent

0

I have these two classes:

public class ArgentumUI {

    private JFrame janela;
    private JPanel painelPrincipal;

    public static void main(String[] args) {
         new ArgentumUI().montaTela();
       }


    public void montaTela() {
        preparaJanela();
        preparaPainelPrincipal();
        preparaBotaoCarregar();
        preparaBotaoSair();
        mostraJanela();
    }

    private void mostraJanela() {
        // TODO Auto-generated method stub
        janela.pack();
        janela.setSize(540, 540);
        janela.setVisible(true);
    }

    private void preparaPainelPrincipal() {
          painelPrincipal = new JPanel();
          janela.add(painelPrincipal);
        }

    private void preparaBotaoSair() {
        // TODO Auto-generated method stub
        JButton botaoSair = new JButton("Sair");
        botaoSair.setMnemonic(KeyEvent.VK_S);

        //Action Listener       
        botaoSair.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonActionSair(e);
            }
        });

//      Key Listener para o Frame
        botaoSair.addKeyListener(new KeyListener() {

            //Quando soltar a tecla
            public void keyReleased(KeyEvent e) {

                //Se a tecla pressionada for igual a F2
                if (e.getKeyCode() == KeyEvent.VK_S){ 
                    buttonActionSair(e);
                }
            }

            public void keyTyped(KeyEvent e) {}
            public void keyPressed(KeyEvent e) {}
        });


        painelPrincipal.add(botaoSair);
    }

    private void preparaBotaoCarregar() {
        // TODO Auto-generated method stub
        JButton botaoCarregar = new JButton("Carregar XML");
        botaoCarregar.setMnemonic(KeyEvent.VK_C);

        //Action Listener       
        botaoCarregar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                buttonActionCarregar(e);
            }
        });

//      Key Listener para o Frame
        botaoCarregar.addKeyListener(new KeyListener() {

            //Quando soltar a tecla
            public void keyReleased(KeyEvent e) {

                //Se a tecla pressionada for igual a F2
                if (e.getKeyCode() == KeyEvent.VK_C){ 
                    buttonActionCarregar(e);
                }
            }

            public void keyTyped(KeyEvent e) {}
            public void keyPressed(KeyEvent e) {}
        });


        painelPrincipal.add(botaoCarregar);
    }

    private void preparaJanela() {
        // TODO Auto-generated method stub
        janela = new JFrame("Argentum");
        janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void buttonActionSair(AWTEvent e) {
        if (e instanceof ActionEvent || e instanceof KeyEvent) {
            System.exit(0);
            janela.requestFocus();
        }
    }

    private void buttonActionCarregar(AWTEvent e) {
        if (e instanceof KeyEvent || e instanceof ActionEvent) {
            new EscolhedorDeXML().escolhe();
            janela.requestFocus();
        }
    }


}

and

public class EscolhedorDeXML {

    public void escolhe() {
        try {
            JFileChooser chooser = new JFileChooser(
                    "/home/victorge/git/fj22-argentum");
            chooser.setFileFilter(new FileNameExtensionFilter("Apenas XML",
                    "xml"));
            int retorno = chooser.showOpenDialog(null);

            if (retorno == JFileChooser.APPROVE_OPTION) {
                FileReader reader = new FileReader(chooser.getSelectedFile());
                List<Negociacao> negocios = new LeitorXML().carrega(reader);

                Negociacao primeiroNegocio = negocios.get(0);
                String mensagem = "Primeiro negocio: "
                        + primeiroNegocio.getPreco();
                JOptionPane.showMessageDialog(null, mensagem);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        new EscolhedorDeXML().escolhe();
    }
}

By clicking on the Load XML button, everything happens exactly, and the dialog that came up is closed at the end of the file selection or when I click the cancel button. Everything working right.

I'm having problems with KeyEvent , more precisely on buttonActionCarregar() method.

My problem is: when I use alt + c , the Load XML button is enabled and a new dialog box to select the files appears, and with it the buttons open and cancel. When you click cancel, nothing happens , and I have to click cancel again to have the dialog closed.

    
asked by anonymous 08.10.2014 / 20:50

1 answer

1

The problem that was occurring has already been resolved. Just a% w / w%

    
09.10.2014 / 17:26