Atualizando Linha JTable

2 respostas
F

bom dia pessoal, estou inserindo uma nova linha em minha tabela, mas o ID ele não atualiza, fica 0 até que reinicie a aplicação.... se alguém souber o que posso fazer, segue minha tabela:

public class ClienteTableModel extends AbstractTableModel implements TableModelListener{
	private List<Cliente> clientes;
	private List<String> colunas;
	private ClienteDAO dao;

	// Construtores
	public ClienteTableModel() {
	
	}

	public ClienteTableModel(ClienteDAO dao) throws SQLException {
		this.dao = dao;
		this.clientes = dao.retornaTodas();
		colunas = Arrays.asList("Código", "CPF", "Nome");
		this.addTableModelListener(this);
	}

	// Métodos Geters
	public List<String> getColunas() {
		return colunas;
	}

	// Retorna o numero de colunas no modelo

	public int getColumnCount() {
		return colunas.size();
	}

	// Retorna o numero de linhas existentes no modelo

	public int getRowCount() {
		return clientes.size();
	}

	public String getColumnName(int i) {
		return colunas.get(i);
	}

	// Obtem o valor na linha e coluna

	public Object getValueAt(int r, int c) {
		Cliente cliente = clientes.get(r);
		switch (c) {
		case 0:
			return cliente.getId();
		case 1:
			return cliente.getCpf();
		case 2:
			return cliente.getNome();
		}
		return null;
	}

	public void setValueAt(Object aValue, int r, int c){
		Cliente cliente = clientes.get(r);
		switch (c) {
		case 0:
			cliente.setId((Long) aValue);
			break;
		case 1:
			cliente.setCpf((String) aValue);
			break;
		case 2:
			cliente.setNome((String) aValue);
			break;
			default:
				throw new IndexOutOfBoundsException("não existe");
		}
		fireTableCellUpdated(r, c);
	}
		

        public void addRow(Cliente cliente){
		clientes.add(cliente);
		fireTableDataChanged();
	}
	
	
	@Override
	public void tableChanged(TableModelEvent e) {
		int i = e.getFirstRow();
		Cliente cliente = clientes.get(i);
		System.out.println(i);
		dao.atualiza(cliente);
	}

}

2 Respostas

U

fernandolacerdanunes vc tem que setar novamente a tabela na interface. Dependendo da interface vc terá que dar um release nela tbm, por exemplo se a tabela estiver dentro de um JInternalFrame vc talvez precise dar um dispose(); e logo após um new.

F

ta ai a parte onde chamo a tabela, como posso fazer o que você falou?

Desde já agradeço...

public PainelInferiorCliente() throws SQLException {

		// Definindo que as configurações serão manuais
		this.setLayout(null);

		// Definindo tmanho da tela
		this.setSize(600, 600);

		this.setBackground(Color.LIGHT_GRAY);
		
		try {
			tc = new ClienteTableModel(dao);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JTable t = new JTable(tc);
		
		scroll = new JScrollPane(t);
		scroll.setBounds(70, 305, 470, 100);
		scroll.setVisible(true);
		this.add(scroll);
		id = new JLabel("Código : ");
		id.setBounds(70, 110, 100, 20);
		id.setVisible(true);
		this.add(id);

		campoID = new JTextArea();
		campoID.setBounds(120, 110, 30, 20);
		campoID.setVisible(true);
		campoID.setEnabled(false);
		this.add(campoID);
		
		localizar = new JButton("Localizar");
		localizar.setBounds(160, 110, 150, 20);
		localizar.setVisible(true);
		this.add(localizar);
		localizar.addActionListener(this);

		cpf = new JLabel("Cpf : ");
		cpf.setBounds(70, 130, 100, 20);
		cpf.setVisible(true);
		this.add(cpf);

		campoCpf = new JTextArea();
		campoCpf.setBounds(70, 155, 100, 20);
		campoCpf.setVisible(true);
		this.add(campoCpf);

		nome = new JLabel("Nome : ");
		nome.setBounds(180, 130, 100, 20);
		nome.setVisible(true);
		this.add(nome);

		campoNome = new JTextArea();
		campoNome.setBounds(180, 155, 360, 20);
		campoNome.setVisible(true);
		this.add(campoNome);

		endereco = new JLabel("Endereço : ");
		endereco.setBounds(70, 180, 100, 20);
		endereco.setVisible(true);
		this.add(endereco);

		campoEndereco = new JTextArea();
		campoEndereco.setBounds(70, 205, 310, 20);
		campoEndereco.setVisible(true);
		this.add(campoEndereco);

		numero = new JLabel("Número : ");
		numero.setBounds(390, 180, 100, 20);
		numero.setVisible(true);
		this.add(numero);

		campoNumero = new JTextArea();
		campoNumero.setBounds(390, 205, 150, 20);
		campoNumero.setVisible(true);
		this.add(campoNumero);

		bairro = new JLabel("Bairro : ");
		bairro.setBounds(70, 230, 100, 20);
		bairro.setVisible(true);
		this.add(bairro);

		campoBairro = new JTextArea();
		campoBairro.setBounds(70, 255, 150, 20);
		campoBairro.setVisible(true);
		this.add(campoBairro);

		cep = new JLabel("Cep : ");
		cep.setBounds(230, 230, 100, 20);
		cep.setVisible(true);
		this.add(cep);

		campoCep = new JTextArea();
		campoCep.setBounds(230, 255, 150, 20);
		campoCep.setVisible(true);
		this.add(campoCep);

		telefone = new JLabel("Telefone : ");
		telefone.setBounds(390, 230, 100, 20);
		telefone.setVisible(true);
		this.add(telefone);

		campoTelefone = new JTextArea();
		campoTelefone.setBounds(390, 255, 150, 20);
		campoTelefone.setVisible(true);
		this.add(campoTelefone);

		salvar = new JButton("Cadastrar");
		salvar.setBounds(70, 280, 150, 20);
		salvar.setVisible(true);
		this.add(salvar);
		salvar.addActionListener(this);

		excluir = new JButton("Excluir");
		excluir.setBounds(230, 280, 150, 20);
		excluir.setVisible(true);
		this.add(excluir);
		excluir.addActionListener(this);

		alterar = new JButton("Alterar");
		alterar.setBounds(390, 280, 150, 20);
		alterar.setVisible(true);
		this.add(alterar);
		alterar.addActionListener(this);

	
		// Deixar painel visível
		this.setVisible(true);

	}

	public void habilitaCampo() {

		campoNome.setEnabled(false);
		campoCpf.setEnabled(false);
		campoEndereco.setEnabled(false);
		campoNumero.setEnabled(false);
		campoBairro.setEnabled(false);
		campoCep.setEnabled(false);
		campoTelefone.setEnabled(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(salvar)) {

			cliente1.setNome(campoNome.getText());
			cliente1.setCpf(campoCpf.getText());
			cliente1.setEndereco(campoEndereco.getText());
			cliente1.setNumero(campoNumero.getText());
			cliente1.setBairro(campoBairro.getText());
			cliente1.setTelefone(campoTelefone.getText());

			campoID.setText(String.valueOf(dao.insereBanco(cliente1)));
			
			tc.addCliente(cliente1);
			
		}
	}
}
Criado 12 de março de 2013
Ultima resposta 12 de mar. de 2013
Respostas 2
Participantes 2