Ajuda com JTable

4 respostas
V

oi gente, criei um form que pega nome e idade e armazena esses dados em uma JTable
só que só consigo armazenar mais de uma linha
sempre que insiro novo registro ele sobrepoeo antigo

alguem pode me ajudar?

abraço

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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;


public class Aplic extends JFrame {

	
	private static final long serialVersionUID = 1L;
	
	JPanel p;
	JTextField tf1;
	JLabel l = new JLabel("Nome:");
	
	JPanel p2;
	JTextField tf2;
	JLabel l2 = new JLabel(" age:  ");
	
	JPanel p3;
	JButton bt;
	
	
	public static void main(String[] args) {
	   Aplic a = new Aplic();
	   a.init();
	}
	
	
	
	public void init(){
		getContentPane().add(getP(),BorderLayout.NORTH);
		getContentPane().add(getP2(),BorderLayout.CENTER);
		getContentPane().add(getP3(),BorderLayout.SOUTH);
		setTitle(" ************** CADASTRO *********** ");
		setSize(400,120);
		setLocation(400,400);
		setVisible(true);
	}
	
	
	
	public JPanel getP(){
	    if(p == null){
	       p = new JPanel();	
	       p.add(l);
	       p.add(getTf1());
	    }
		return p;
	}
	
	
	public JPanel getP2(){
	    if(p2 == null){
	       p2 = new JPanel();	
	       p2.add(l2);
	       p2.add(getTf2());
	    }
		return p2;
	}
	
	
	public JPanel getP3(){
	    if(p3 == null){
	       p3 = new JPanel();	
	       p3.add(getBt());
	    }
		return p3;
	}
	
	
	
	public JTextField getTf1(){
		if(tf1 == null){
		   tf1 = new JTextField(15);	
		}
		return tf1;
	}
	
	
	public JTextField getTf2(){
		if(tf2 == null){
		   tf2 = new JTextField(15);	
		}
		return tf2;
	}

	
	public JButton getBt(){
		if(bt == null){
		   bt = new JButton("Listar");
		   bt.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e) {
					
					JFrame f = new JFrame();
					JPanel p = new JPanel();
					
					
					String[] col = {"Nome", "País"};
					
					
					String[][] comp = {
							{tf1.getText(),tf2.getText()},	
					};
					
					
					JTable t = new JTable(comp,col);	
					
					JScrollPane scroll = new JScrollPane(t);
					scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
					scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
					
					p.add(scroll);
					f.getContentPane().add(p);
					f.setTitle(" *** CLIENTES CADASTRADOS *** ");
					f.setSize(500,100);
					f.setLocation(400,410);
					f.setVisible(true);
					}
				
		   });
		} 
		return bt;
	}
		
}//class

4 Respostas

Marky.Vasconcelos

Percebeu que seu botao sempre abre uma tela nova com apenas os dados que tem no JTextField na hora?

Voce devia criar apenas uma instancia desse TableModel e ir adicionando a medida que for preciso novos itens.

E já te adianto, largue o DefaultTableModel(que a Jtable esta secretamente utilizando quando voce passa aqueles argumentos) e implemente seu TableModel.

ViniGodoy

Faço minhas as palavras do Mark. Para implementar um JTable, é importantíssimo entender como ele funciona. E o primeiro passo para isso é criar o seu próprio TableModel. Não só o código ficará mais simples, como muito mais seguro e organizado.

O link na nossa assinatura ensina a fazer isso. :slight_smile:

erico_kl

isso aí…
confirmo o que o Mark e o Vini disseram por experiência própria… acho que todo mundo algum dia já usou a DefaultTableModel… :lol:
aqui no GUJ também tem um tutorial muito bom sobre como implementar o seu próprio TableModel…

Marky.Vasconcelos

Resumindo… nós 3 dissemos a mesma coisa XD

E o link que o ViniGodoy se refere é:
http://www.guj.com.br/posts/list/15/199067.java#1001295

Vale a pena estudar.

Criado 17 de junho de 2010
Ultima resposta 18 de jun. de 2010
Respostas 4
Participantes 4