Java - Enter function

Asked

Viewed 1,178 times

2

In the system there is a field, the person needs to fill this field and then press enter.

By pressing enter the system does a check and then opens a JOptionpane asking to select: yes, nay, okay, etc..

The person presses enter, and the system performs the function, so far everything ok.

The problem is: by pressing enter on JOptionPane (for example pressed enter on the button yes) it performs the function again, but as it was not filled the system ends up accusing an error, then it gets in the loop, because the person who uses the system presses enter, then it loads again, and so it goes...

How to get around this? I’ve tried several things, I’ve searched the internet several things, everything without success has something to do to fix it?

To capture the action, I’m using KeyListener

EDIT 1:

 try {

            if (evt.getKeyCode() == 10 || evt.getKeyCode() == 127) {
                jfObjeto.setText(jfObjeto.getText());
                controller.getAtendimentoController().novoAtendimentoItem();
                controller.getAtendimentoController().getAtendimentoModel().getAtendimentoItem().setObjeto(jfObjeto.getText());

            }

            if (evt.getKeyCode() == 10) {
                //verifica se possui sigla cadastrada
                controller.getAtendimentoController().verificaCampoObjeto(
                        controller.getSistemaOperacaoController(),
                        controller.getSistemaSiglaEtiquetaController());
                //verifica se o objeto possui lancamento para EDITAR
                controller.getAtendimentoController().consultaObjetoEdicao();
                if (controller.getAtendimentoController().getAtendimentoModel().getAtendimentoItem().getId() > 0) {
                    int resposta = new Mensagem().pergunta("Atenção deseja alterar o objeto " + controller.getAtendimentoController().getAtendimentoModel().getAtendimentoItem().getObjeto() + "?");
                    if (resposta == 0) {
                        preencherAtendimentoItemToFrame();
                    }
                }
                 // avanca para proximo campo, ou recua para o anterior

                formatarAvancarRecuarAtendimentoItem(evt, jfObjeto);

            } else if (evt.getKeyCode() == 127) {
                controller.getAtendimentoController().consultaObjetoEdicao();
                if (controller.getAtendimentoController().getAtendimentoModel().getAtendimentoItem().getId() > 0) {
                    int resposta = new Mensagem().pergunta("Deseja excluir o objeto " + controller.getAtendimentoController().getAtendimentoModel().getAtendimentoItem().getObjeto() + "?");
                    if (resposta == 0) {
                        controller.getAtendimentoController().excluirObjetoPostagemItem();
                        controller.getAtendimentoController().carregarListaAtendimentoItem();
                        tabelaAtendimentoItem();
                        limparAtendimentoItem();
                        carregarQuantidadeValorTotalAtendimento();
                        controller.getAtendimentoController().novoAtendimentoItem();
                        jfObjeto.requestFocusInWindow();
                    }
                } else {
                    new Mensagem().erro("Nenhum Objeto Localizado para Exclusão");
                }

            } else if (evt.getKeyCode() == 27) {
                formatarAvancarRecuarAtendimentoItem(evt, jfObjeto);
            }

        } catch (Exception ex) {
            new Mensagem().erro(ex.getMessage());
            jfObjeto.requestFocusInWindow();
        }
  • 1

    Post the code of KeyListener so we can help you.

  • It would not be easier to let open such JOPTIONPANE only and only if the necessary field( No sistema existe um campo, a pessoa precisa preencher este campo e em seguida pressionar enter. ) is different from null or vazio etc. :)

  • I don’t understand the technology but my first idea would be to shift the focus to somewhere else before opening the PANE.

  • is different from null or empty etc :) - If null the message informs that the user needs to enter this field.... then he’s squeezing enter all the time and enters the loop I said....

  • falls into the same error, I take the focus run the command at the end it gets the focus again for the user to type other information... will fall into the eternal loop.... the problem is in ENTER that the person in JOPTION PANE, there due to this enter he enters the infinite loop.... EDIT: I need the key, because the enter has a function... the Esc has another function... the delete key has a function... can not be that too

  • There is the possibility to insert the Keylistener source?

  • this inserted in the keyListener... the code that I put there.... because enter has a function... Esc has another function... the delete key has a function and I need to capture the event....

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Show 3 more comments

2 answers

1

you can work with threads one for the events and the other for the screen then :

if(evt.getKeyCode()== com.sun.glass.events.KeyEvent.VK_ENTER){

            tela.wait(timeout);
 }
  • excuse my ignorance, but how will I do to make this separation?

  • In your system you will make a thread that controls the interface and another that controls the keys when user does an action with the keys you give a Wait() on the screen and executes the code in the thread of the keys.

  • my question is, to make the system I use Netbeans, when it will run it calls java.awt.Eventqueue.invokeLater(new Runnable() { @Override public void run() { new Newjframe().setVisible(true); } }); how do I pause this thread?

1

Use a global control variable, so you can check that this is not the second time ENTER is being squeezed. Shortly after the execution on JOptionpane change again to the default value and the problem will be solved.

For example:

private Boolean respondendo = false;

private void verificarTeclas(KeyEvent evt) {
  Integer resposta;

  if (!respondendo) {
    System.out.println("Tecla " + String.valueOf(evt.getKeyCode()) + " sendo analisada");
    this.respondendo = true;

    if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
      resposta = JOptionPane.showConfirmDialog(this, "Deseja excluir o objeto?");

      if (resposta == JOptionPane.YES_OPTION) {
        // Faz o sim;
      } else {
        // Faz o não
      }
    }

    this.respondendo = false;
  }
}
  • 1

    I don’t see how this can solve this problem. Add an example to make it clear.

Browser other questions tagged

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