Problemas com jtable

0 respostas
Allexb10

Ola,

Nao estou conseguindo popular minha tabela com os dados do banco.
Segue as classes, se alguem tiver uma forma bem simples para fazer isso, agradeco.

package Formularios;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

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 Modelo.Produtos;
import Servicos.ServicoProdutos;
import Servicos.TabelaModelo;

@SuppressWarnings("serial")
public class FrmConsultaProdutos extends JFrame{

	JTable textArea          = new JTable(20, 4);
	JTextField texto  		 = new JTextField(20);
	JButton btp 			 = new JButton("Pesquisar");
	JButton bts 			 = new JButton("Sair");
	JLabel nome 			 = new JLabel("Produto ");
	JLabel cod 				 = new JLabel();
	JScrollPane js 			 = new JScrollPane(textArea);
	ServicoProdutos spro 	 = new ServicoProdutos();
	ArrayList<Produtos> prog = new ArrayList<Produtos>();
	
	public FrmConsultaProdutos() {
		super("Consulta Produtos");
		
		Container c = getContentPane();
		FlowLayout layout = new FlowLayout();
		c.setLayout(layout);
		addWindowListener(new java.awt.event.WindowAdapter() {
	            public void windowActivated(java.awt.event.WindowEvent evt) {
	                ForWindowActivate(evt);
	            }
	        });
		js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		
		
		bts.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		
		JPanel ps = new JPanel();
		ps.setLayout(new FlowLayout(FlowLayout.LEFT));
		ps.add(nome);
		ps.add(texto);
		
		JPanel pd = new JPanel();
		pd.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pd.add(btp);
		pd.add(bts);
		
		JPanel pi = new JPanel();
		pi.setLayout(new FlowLayout(FlowLayout.LEFT));
		pi.add(cod);
		pi.add(textArea);

		c.add(BorderLayout.NORTH,ps);
		c.add(BorderLayout.EAST,pd);
		c.add(BorderLayout.SOUTH,pi);
		
		setSize(550, 350);
		setVisible(true);
		dispose();
		
	}
	
	 private void ForWindowActivate(java.awt.event.WindowEvent evt) {
	      try {
	            prog = spro.getAll();
	        } catch (SQLException ex) {
	        }

	        JTable jTable2 = createJTable();
	        
	        js.setViewportView(jTable2);
	       
	    }
	 
	 @SuppressWarnings("unchecked")
	public JTable createJTable() {
			ArrayList dados = new ArrayList();
	       final String[] coluns = new String[]{"Descrição","Codigo do Setor","Valor"};
	        //Alimenta as linhas de dados
	        for (Produtos f : prog) {
	            dados.add(new String[]{f.getDescricao(),String.valueOf
	                    (f.getCodSetor()), String.valueOf(f.getValor())});
	            
	        }
	        TabelaModelo modelo = new TabelaModelo(dados, coluns);
	        JTable jtable = new JTable(modelo);
	        
	        return jtable;
	     }
	
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servicos;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import Modelo.Produtos;

/**
 *
 */
@SuppressWarnings("serial")
public class TabelaModelo extends AbstractTableModel{

    @SuppressWarnings("unchecked")
	private ArrayList linhas = null;
    private String [] colunas = null;
    public String [] getColunas(){return colunas;}
    @SuppressWarnings("unchecked")
	public ArrayList getLinhas() {return linhas;}
    private void setColunas(String[] strings){colunas = strings;}
    @SuppressWarnings("unchecked")
	private void setLinhas(ArrayList list){linhas = list;}
    
    public int getRowCount(){
        return getLinhas().size();
    }
    
    public int getColumnCount(){
        return getColunas().length;
    }
    
    @Override
    public String getColumnName(int column){
        return colunas[column];
    }
    
    public Object getValueAt(int rowIndex,int columnIndex){
        //Obtem a linha,que é uma String []
        String [] linha = (String []) getLinhas().get(rowIndex);
        //retorna o objeto que esta na coluna
        return linha[columnIndex];
    }
    
    @SuppressWarnings("unchecked")
	public TabelaModelo(ArrayList dados,String[] colunas){
        this.setColunas(colunas);
        this.setLinhas(dados);
    }
	public void TabelaModelo1(List<Produtos> lista) {
		// TODO Auto-generated constructor stub
	}
	public TabelaModelo(List<Produtos> lista) {
		// TODO Auto-generated constructor stub
	}
    
}
Criado 16 de novembro de 2011
Respostas 0
Participantes 1