Problem with Keyevent

Asked

Viewed 158 times

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();
    }
}

When you click the Load XML button everything happens right, and the dialog box that appeared is closed at the end of the file selection or when I click the cancel button. All working right.

I’m just having trouble with KeyEvent, more precisely in the method buttonActionCarregar().

My problem is, when I use alt+c, the button Load XML is activated and a new dialog box to select the files appears, and with it the open and cancel buttons. By clicking with the mouse on cancel, nothing happens, and I have to click cancel again to close the dialog box.

1 answer

1

The problem that was already occurring solved. All it took was one JFrame.requestFocus().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.