How to validate jcomboBox before saving

Asked

Viewed 356 times

0

I have the following problem, I am trying to validate these jcombobox, I want that if the user does not select an item from the movie jcombobox, a message appears: "Select Movie", if it is the client’s, a message appears: "Select Customer" and if it is the employee’s, also appear a message of this type, I need that validation before saving, as I would do it?

Method I call to popular jcomboBox:

    protected void preenche_filmes() {
    java.sql.Connection conn = null;
    java.sql.PreparedStatement pstm = null;
    ResultSet rs = null;
    List<String> resultados = new ArrayList();
    //importe a classe java.util.List

    try {

        conn = ConnectDB.conexaoDB();
        pstm = conn.prepareStatement(SELECT_FILME);         
        rs = pstm.executeQuery();
        resultados.add("Selecione o Filme:");
        while(rs.next()){

            resultados.add(rs.getString("codigo_filme")+"-"+ rs.getString("titulo"));

        }

        cbFilmes = new JComboBox(resultados.toArray());
    } catch(SQLException e) {
        JOptionPane.showMessageDialog(null, "Ocorreu um erro "+ e.getMessage()+ e.getErrorCode()+ e.getSQLState()+e.getLocalizedMessage());
        e.printStackTrace();
    }



}

Method of saving on the seat:

protected void salvar(){



    String selecao_filmes = cbFilmes.getSelectedItem().toString();
    String[] campos_filmes = selecao_filmes.replace(" ","").split("-"); // suponho que esteja separado por -
    System.out.println(campos_filmes[0]); // deve pegar o código
   int  codigo_filme  = Integer.parseInt(campos_filmes[0]);//Integer.parseInt(campos_filmes[0]);

  String selecao_funcionario = cbFuncionario.getSelectedItem().toString();
    String[] campos_funcionarios = selecao_funcionario.replace(" ", "").split("-");
    System.out.println(campos_funcionarios[0]);
int codigo_funcionario = Integer.parseInt(campos_funcionarios[0]);
    String campos_clientes[];


    String selecao_cliente = cbClientes.getSelectedItem().toString();
     campos_clientes = selecao_cliente.replace(" ", "").split("-");
    System.out.println(campos_clientes[0]);
   int codigo_cliente  = Integer.parseInt(campos_clientes[0]);

    String data_locacao = dt_locacao.getText();
    String data_devolucao = dt_devolucao.getText();
 if(data_devolucao.equals("  /  /    ")){
        JOptionPane.showMessageDialog(null, "O campo Data de devolução é Obrigatório!");
        }else if (data_locacao.equals("  /  /    ")){
    JOptionPane.showMessageDialog(null, "O campo Data de locação é Obrigatório!");

        }else{

    Locacao locacao = new Locacao();
    locacao.setCodigo_cliente(codigo_cliente);
    locacao.setCodigo_funcionario(codigo_funcionario);
    locacao.setCodigo_filme(codigo_filme);
    locacao.setDt_locacao(data_locacao);
    locacao.setDt_devolucao(data_devolucao);

    control.Locacao manutencao = new control.Locacao();
    if(codigo_editar==0){
        manutencao.inserir(locacao);


    }else{
        manutencao.alterar(locacao);
    }
    limparInformacoes();

    tabbedPane.setSelectedIndex(1);
        }
}

1 answer

3


Following the line of reasoning of your code, you can do it this way:

if(cbFilmes.getSelectedItem() == null || cbFilmes.getSelectedgetSelectedIndex() == 0) {

  JOptionPane.showMessageDialog(null, "Selecione um filme.");

} else {
    //código que faz o salvamento aqui
}

The Czech condition if there is something selected in the JCombobox or if the selected item is the first item, because in the I code that populates the component, the first item is a caption(resultados.add("Selecione o Filme:");, and I believe it is not desirable to save him.

  • The problem is that now, it gives me this error: Exception in thread "AWT-Eventqueue-0" java.lang.Numberformatexception: For input string: "Selectioneofilme:"

  • And it always falls on condition select a movie, not letting me save.

  • 1

    @Pr99 the cause of the error is something else, add a [mcve] of your code so it is possible to test.

  • Your answer is right, I was able to correct the error, thank you for your help.

  • @Pr99 dispo :), I did not point out the error because I did not understand how the combobox was being populated, so if I pointed out what I thought was wrong, it could cause more problems elsewhere. That is why it is always important to try to add a [mcve], then it is easier to correct other errors that may arise in the solution.

Browser other questions tagged

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