Converter jtextfield para inteiro

3 respostas
R

Boa tarde gurizada, me deparei com mais uma duvida cruel, depois de pesquisar varios topicos relacionados e não conseguir sanar a minha duvida aqui vai,
tenho um form com uma jtable funcionando, populada já, coloquei um campo de pesquisa, só que esta dando erro, vou postar o codigo para ficar mais facil de entender;

esta e a classe que faz a consulta no banco:

package br.com.financeira.emprestimos;

import br.com.financeira.acesobd.AcessoPostgres;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author rafael
 */
public class EmprestimosControl {
    public EmprestimosControl(){
}
    PreparedStatement pstm;
    ResultSet rs;
    AcessoPostgres bd = new AcessoPostgres();
    
    String consultaCliente = "SELECT * FROM EMPRESTIMOS WHERE CODIGO = ?";
    String consultaUsuario = "SELECT * FROM EMPRESTIMOS WHERE COD_USUARIO = ?";
    String consultaAgente = "SELECT * FROM EMPRESTIMOS WHERE COD_AGENTE = ?";
    String cadastraEmprestimo = "INSERT INTO EMPRESTIMOS (COD_USUARIO, CODIGO_AGENTE, CODIGO, DATA_EMPRESTIMO, QUANT_PARCELAS,"
            + "DATA_PROX_EMPRESTIMO, TIPO_EMPRESTIMO, BANCO) VALUES (?,?,?,?,?,?,?,?,?)";


public List<EmprestimosBean> EmprestimosCliente(Integer codigo){
        List<EmprestimosBean> emprestimos = new ArrayList();
        try{
            pstm = bd.conectar().prepareStatement(consultaCliente);
            pstm.setInt(1, codigo);//SELECT * FROM CLIENTES WHERE CODIGO = 1;
         
            rs = pstm.executeQuery();
            EmprestimosBean emp;
            while (rs.next()){
                emp = new EmprestimosBean();
                emp.setCod_emprestimo(rs.getInt("cod_emprestimo"));
                emp.setCod_usuario(rs.getInt("cod_usuario"));
                emp.setCodigo_agente(rs.getInt("codigo_agente"));
                emp.setCodigo(rs.getInt("codigo"));
                emp.setData_emprestimo(rs.getString("data_emprestimo"));
                emp.setValor_emprestimo(rs.getString("valor_emprestimo"));      
                emp.setQuant_parcelas(rs.getString("quant_parcelas"));
                emp.setData_prox_emprestimo(rs.getString("data_prox_emprestimo"));
                emp.setTipo_emprestimoo(rs.getString("tipo_emprestimo"));
                emp.setBanco(rs.getString("banco"));
                emprestimos.add(emp);
            }
            bd.desconectar();
        } catch(Exception e){
            e.printStackTrace();
        }
        return emprestimos;
    }

e esta é a classe que esta a jtable:

package br.com.financeira.view;

import br.com.financeira.emprestimos.EmprestimosBean;
import br.com.financeira.emprestimos.EmprestimosControl;
import java.util.List;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author rafael
 */
public class Emprestimos extends javax.swing.JFrame {
DefaultTableModel tmEmprestimos = new DefaultTableModel(null, new String[]{"Data Emprestimo", "Valor Emprestimo", "Quant Parcelas", "Proximo Emp.", "Banco"});
    /** Creates new form Emprestimos */
ListSelectionModel lsmEmprestimos;
List<EmprestimosBean> emprestimos;   
String tipoConsulta;

public Emprestimos() {
        initComponents();
    }

    protected void tbEmprestimosLinhaSelecionada(JTable tb){
        if (tb.getSelectedRow() != -1){
      
        } else {
       
        }
    }
     


    protected void consultaCliente(){
        EmprestimosControl emp = new EmprestimosControl();
        emprestimos = emp.EmprestimosCliente("%" + tfCliente.getText().trim() + "%");
        mostrarEmpCli(emprestimos);
        
    }

protected void mostrarEmpCli(List<EmprestimosBean> emprestimos){
        while (tmEmprestimos.getRowCount() > 0){
            tmEmprestimos.removeRow(0);
        }
        
        if (emprestimos.size() == 0){
            JOptionPane.showMessageDialog(this, "Nenhum emprestimos encontrado!");
        }else{
            String [] campos = new String[] {null, null, null, null, null};
            for (int i = 0; i < emprestimos.size(); i++){
                tmEmprestimos.addRow(campos);
                tmEmprestimos.setValueAt(emprestimos.get(i).getData_emprestimo(), i, 0);
                tmEmprestimos.setValueAt(emprestimos.get(i).getValor_emprestimo(), i, 1);
                tmEmprestimos.setValueAt(emprestimos.get(i).getQuant_parcelas(), i, 2);
                tmEmprestimos.setValueAt(emprestimos.get(i).getData_prox_emprestimo(), i, 3);
                tmEmprestimos.setValueAt(emprestimos.get(i).getBanco(), i, 4);
            }
        }
    }

ai da um erro na linha 40, pq o jtextfield espera um inteiro e encontra uma string, como eu poderia fazer para este textfield encontrar um inteiro? desde já obrigado.

3 Respostas

A

cara,

vc tem fazer assim

emprestimos = emp.EmprestimosCliente(Integer.parseInt(tfCliente.getText().trim()));

so se o usuario digitar algo que nao seja um numero, vai dar pau.

t+

R

Cara, perfeito, muito obrigado.

Andre_Rosa

Eu faria algo diferente:

campoTexto = new JFormattedTextField(); validos = new MaskFormatter("**********"); validos.setValidCharacters("[telefone removido]"); validos.install(campoTexto);

Criado 2 de novembro de 2011
Ultima resposta 2 de nov. de 2011
Respostas 3
Participantes 3