JTABLE-nao consigo fazer exibir os dados inseridos no banco no JTABLE

8 respostas
Y

EU nao sei porque nao exibe os valores do banco de dados no JTABLE eu ja tentei debugar e nao consigo achar o erro
se alguem me puder ajudar eu agradeco.
obs:estou usando so o JTABLE SIMPLES

// ESTA CLASSE E A INTERFACE
package telaTest;

import java.awt.event.ActionEvent;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.util.Vector;
import javax.swing.JOptionPane;

import javax.swing.JRadioButton;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.TableModel;

import banco.ClienteDB1;

import negocio.Clientes;

public class ClienteTelaTest extends JFrame implements MouseListener {

private static final long serialVersionUID = 1L;

private JPanel container = null;

private JTable table;

private JButton addButton, updateButton, deleteButton, searchButton,
		cleanButton, fecharButton;

JTable jtable;

private JTextField codiPessoa, codClientes, nomeContrato;

private JLabel lcod, codCliente, nomeContrato1;

private JScrollPane js;

private Object jTable;

public ClienteTelaTest() {
	this.init();
}

public void init() {

	this.setSize(700, 900);
	this.setTitle("CALL CENTER- CLIENTES");
	this.setContentPane(this.getContainer());
	this.setVisible(true);
}

public JPanel getContainer() {
	if (container == null) {

		container = new JPanel();
		container.setLayout(null);

		lcod = new JLabel();
		lcod.setBounds(10, 30, 200, 19); // (altura, , , )
		lcod.setText("Código Pessa");
		container.add(lcod);
		// o label do JLabel do codigo do cliente
		codCliente = new JLabel();
		codCliente.setBounds(10, 60, 200, 19);
		codCliente.setText("CODIGO DO CLIENTE ");
		container.add(codCliente);

		// o label do JLabel do nomeContrato
		nomeContrato1 = new JLabel();
		nomeContrato1.setBounds(10, 90, 200, 19);// (Esquerda, do centro,// à direita,// CONDUZINDO)
		nomeContrato1.setText("NOME CONTRATO");
		container.add(nomeContrato1);

		// a barra do JTextField codigo da Pessoa
		codiPessoa = new JTextField(); // codPessoa
		codiPessoa.setBounds(150, 30, 200, 20);
		container.add(codiPessoa);

		// a barra do JTextField codigo do cliente
		codClientes = new JTextField();
		codClientes.setBounds(150, 60, 200, 20);
		container.add(codClientes);

		// a barra do JTextField nomeContrato
		nomeContrato = new JTextField();
		nomeContrato.setBounds(150, 90, 200, 20);
		container.add(nomeContrato);

		// criacao dos botoes
		jtable = new javax.swing.JTable();
		addButton = new JButton();
		addButton.setBounds(400, 30, 100, 35);

		addButton.setText("Adicionar");
		addButton.addMouseListener(this);
		container.add(addButton);

		updateButton = new JButton();
		updateButton.setBounds(400, 70, 100, 35);
		updateButton.setText("Atualizar");
		updateButton.addMouseListener(this);
		container.add(updateButton);

		deleteButton = new JButton();
		deleteButton.setBounds(400, 110, 100, 35);
		deleteButton.setText("Excluir");

		deleteButton.addMouseListener(this);
		container.add(deleteButton);

		searchButton = new JButton();
		searchButton.setBounds(400, 140, 100, 35);

		//botao pesquisar											
		searchButton.setText("Pesquisar");
		searchButton.addMouseListener(this);
		container.add(searchButton);

		cleanButton = new JButton();
		cleanButton.setBounds(400, 170, 100, 35);
		cleanButton.setText("Limpar");
		cleanButton.addMouseListener(this);
		container.add(cleanButton);

		fecharButton = new JButton();
		fecharButton.setBounds(290, 200, 100, 35);
		fecharButton.setText("FECHAR");
		fecharButton.addMouseListener(this);
		container.add(fecharButton);

		js = new JScrollPane();
		js.setBounds(15, 250, 500, 800);
		js.setViewportView(getTable());
		container.add(js);
	}
	return container;
}

public JTable getTable() {
	Vector cabecalho = new Vector();
	cabecalho.add("CODIGO PESSOA");

	cabecalho.add("NOME");
	cabecalho.add("IDADE");

	cabecalho.add("ENDERECO");
	cabecalho.add("TELEFONE");
	cabecalho.add("CODIGO CLIENTE");
	cabecalho.add("NOME CONTRATO");

	Vector vector = new ClienteDB1().getVectorClientes();
	this.table = new JTable(new Vector(), cabecalho);
	table.addMouseListener(this);
	table.setDoubleBuffered(false);
	return table;
}

//acao do botao
public void mouseClicked(MouseEvent e) {

	String idCodPessoa = this.codiPessoa.getText();
	String idCliente = this.codCliente.getText();
	String nomeContrato = this.nomeContrato.getText();

	// acao do botao adicionar
	if (e.getSource() == this.addButton) {

		Clientes cli = new Clientes();
		// inserindo os dados
		cli.setCodPessoa(Integer.parseInt(idCodPessoa));
		cli.setIdCliente((Integer.parseInt(idCliente)));
		cli.setNomeContrato(nomeContrato);

		ClienteDB1 clienteDB1 = new ClienteDB1();
		clienteDB1.addCliente(cli);
		js.setViewportView(getTable());

		codiPessoa.setText("");
		codClientes.setText("");

	}

}

public void mouseEntered(MouseEvent arg0) {
	// TODO Auto-generated method stub

}

public void mouseExited(MouseEvent arg0) {
	// TODO Auto-generated method stub

}

public void mousePressed(MouseEvent arg0) {
	// TODO Auto-generated method stub

}

public void mouseReleased(MouseEvent arg0) {

	// TODO Auto-generated method stub

}

}

8 Respostas

Y

o restante do programa

O BANCO QUE ESTOU USANDO E O  MYSQL

tabela Pessoa

CREATE TABLE  PESSOA (

IDPESSOA int(11) NOT NULL,

NOME varchar(20) NOT NULL,

IDADE int(11) NOT NULL,

ENDERECO varchar(30) NOT NULL,

TEL varchar(20) default NULL,

PRIMARY KEY  (IDPESSOA)

)

TABELA CLIENTE

CREATE TABLE CLIENTE(

IDCLIENTE INTEGER NOT NULL,

IDPESSOA INTEGER NOT NULL,

NOMECONTRATO VARCHAR(25),

PRIMARY KEY(IDCLIENTE),

FOREIGN KEY(IDPESSOA) REFERENCES PESSOA(IDPESSOA)

);
laudenpower

Bom pelo que vejo você está usando uma implementação de vector para poder armazenar dados na tabela, aconselho a utilizar o AbstractTableModel com List ao invés disso… mas com relação aos dados não aparecerem eu acho que pode estar faltando esse comando jTable1.UpdateUI logo após a inserção do objeto.

Espero ter ajudado…

:slight_smile:

Marky.Vasconcelos

Voce precisa aprender a trabalhar com o TableModel invés de fazer tudo na JTable.

Y

BOA TARDE!!
ainda nao entendi o comando que era pra ser adicionado sera que voce poderia explicar melhor pois ainda estou aprendendo usar o JTABLE
OBS: O PROBLEMA E A CLASSE ClienteDB1 e a ClientTelaTest acho que nao esta havendo uma comunicacao entre elas nao sei porque.por isso que nao esta retornando os valores do banco de dados.

obrigado a todos que estao tentando me ajudar!!

Marky.Vasconcelos

Para aparecer os dados na tabela depois de adicionar voce precisa pegar o TableModel dela (getTableModel) e lançar o evento fireTableDataChanged.

Y

sera que voce poderia passar algum exemplo pois nao estou conseguindo entender como que ficaria no codigo

obrigado!!!

fredsilva.sistemas

Dá uma olhada aqui:
http://www.guj.com.br/posts/list/129257.java#697831

Y

bom dia!!
sera que alguem podera me explicar o que sera que esta acontecendo nesse codigo no tablemodel

if (e.getSource() == this.table) {

TableModel tm = table.getModel();

// verificar o que esta errado

if (table.getSelectedColumn() > 0) { // o que sera que esta acontecendo

// se esta certo ou erado

codigo1.setText((String) tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn() -2));

nome1.setText((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));

endereco1.setText((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()-1));

idade1.setText((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));

<a href="//telefone.setText">//telefone.setText</a>((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
} else {
			// o que esta acontecendo aqui
			
					nome1.setText((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
			endereco1.setText((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
			idade1.setText((String)tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
			codigo1.setText((String) tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn()+1));
			endereco1.setText((String )tm.getValueAt(table.getSelectedRow(), table.getSelectedColumn() + 2));
		}
	}

OBRIGADO A TODOS QUE ME TENTARAM AJUDAR

Criado 11 de junho de 2009
Ultima resposta 22 de jun. de 2009
Respostas 8
Participantes 4