Sou iniciante nao sei mexer com a TableModel, realmente meu codigo está bagunçado, mas estou aprendendo na raça rs, espero sempre poder contar com voceis, mas ja consegui preencher a tabela, pesquisar, e limpar, ou seja, a tabela ja esta pronta so falta colocar o botao imprimir, e gostaria da ajuda de voce:
import java.sql.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class ConsultaBanco extends javax.swing.JFrame {
public ConsultaBanco() {
initComponents();
setBotoes(true,false,false,false,false);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tabela = new javax.swing.JTable();
btExibir = new javax.swing.JButton();
btPesquisar = new javax.swing.JButton();
txtConsulta = new javax.swing.JTextField();
btLimpar = new javax.swing.JButton();
cbxEscolha = new javax.swing.JComboBox();
btImprimir = new javax.swing.JButton();
tabela.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Codigo", "Grupo", "Produto", "Estoque", "Preço Custo", "Preço Venda", "Data da Compra", "Data de Validade"
}
));
jScrollPane1.setViewportView(tabela);
btExibir.setText("Exibir/Atualizar ");
btExibir.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btExibirActionPerformed(evt);
}
});
btPesquisar.setText("Pesquisar");
btPesquisar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btPesquisarActionPerformed(evt);
}
});
btLimpar.setText("Limpar Tabela");
btLimpar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btLimparActionPerformed(evt);
}
});
cbxEscolha.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Código", "Grupo", "Produto", "Data da Compra", "Data de Validade", "Estoque", " " }));
cbxEscolha.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cbxEscolhaActionPerformed(evt);
}
});
btImprimir.setText("Imprimir");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 906, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(btExibir)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btLimpar, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cbxEscolha, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btPesquisar, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(16, Short.MAX_VALUE))
);
layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btExibir, btLimpar});
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btExibir)
.addComponent(btLimpar)
.addComponent(btImprimir))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btPesquisar)
.addComponent(txtConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(cbxEscolha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 456, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void btExibirActionPerformed(java.awt.event.ActionEvent evt) {
if(evt.getSource()==btExibir)
{
preenche_jtable();
setBotoes(true,true,true,true,true);
}
}
private void btLimparActionPerformed(java.awt.event.ActionEvent evt) {
if(evt.getSource()==btLimpar);
{
limpa_jtable();
setBotoes(true,false,false,false,false);
}
}
private void cbxEscolhaActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btPesquisarActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getSource()==btPesquisar)
{
if(cbxEscolha.getSelectedItem().toString().equals("Código"))
{
try
{
String sql = "SELECT * FROM produto Where codigo = '" + txtConsulta.getText() + "'";
BD.setResultSet(sql);
//Aqui você adiciona os campos do banco de dados no jTable.
pesquisaTabela();
}
catch(Exception erro) {}
}
if(cbxEscolha.getSelectedItem().toString().equals("Grupo"))
{
try
{
String sql = "SELECT * FROM produto Where grupo = '" + txtConsulta.getText() + "'";
BD.setResultSet(sql);
//Aqui você adiciona os campos do banco de dados no jTable.
pesquisaTabela();
}
catch(Exception erro) {}
}
if(cbxEscolha.getSelectedItem().toString().equals("Produto"))
{
try
{
String sql = "SELECT * FROM produto Where produto = '" + txtConsulta.getText() + "'";
BD.setResultSet(sql);
//Aqui você adiciona os campos do banco de dados no jTable de produto.
pesquisaTabela();
}
catch(Exception erro) {}
}
if(cbxEscolha.getSelectedItem().toString().equals("Data da Compra"))
{
try
{
String sql = "SELECT * FROM produto Where datacompra = '" + txtConsulta.getText() + "'";
BD.setResultSet(sql);
//Aqui você adiciona os campos do banco de dados no jTable pela data da compra.
pesquisaTabela();
}
catch(Exception erro) {}
}
if(cbxEscolha.getSelectedItem().toString().equals("Data de Validade"))
{
try
{
String sql = "SELECT * FROM produto Where validade = '" + txtConsulta.getText() + "'";
BD.setResultSet(sql);
//Aqui você adiciona os campos do banco de dados no jTable pela validade.
pesquisaTabela();
}
catch(Exception erro) {}
}
if(cbxEscolha.getSelectedItem().toString().equals("Estoque"))
{
try
{
String sql = "SELECT * FROM produto Where estoque = '" + txtConsulta.getText() + "'";
BD.setResultSet(sql);
//Aqui você adiciona os campos do banco de dados no jTable pelo estoque.
pesquisaTabela();
}
catch(Exception erro) {}
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ConsultaBanco().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btExibir;
private javax.swing.JButton btImprimir;
private javax.swing.JButton btLimpar;
private javax.swing.JButton btPesquisar;
private javax.swing.JComboBox cbxEscolha;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tabela;
private javax.swing.JTextField txtConsulta;
// End of variables declaration
//Metodo para preencher a tabela, exibir tudo que tem no banco de dados.
public void preenche_jtable()
{
BD.setResultSet("select * from produto");
DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
modelo.setNumRows(0);
try
{
BD.resultSet.next();
while (BD.resultSet.next())
//Aqui você adiciona os campos do banco de dados no jTable.
modelo.addRow(new Object [] {
BD.resultSet.getString("codigo"),
BD.resultSet.getString("grupo"),
BD.resultSet.getString("produto"),
BD.resultSet.getString("estoque"),
BD.resultSet.getString("custo"),
BD.resultSet.getString("venda"),
BD.resultSet.getString("datacompra"),
BD.resultSet.getString("validade"),
});
BD.resultSet.first();
}
catch(SQLException erro) {
JOptionPane.showMessageDialog(null, "Erro ao listar o jTable "+erro);
}
}
//Metodo para limpar a tabela
public void limpa_jtable()
{
BD.setResultSet("select * from produto");
DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
modelo.setNumRows(0);
try
{
BD.resultSet.next();
while (BD.resultSet.next())
//Aqui limpa os campos da tabela
modelo.addRow(new Object [] {
BD.resultSet.getString(""),
BD.resultSet.getString(""),
BD.resultSet.getString(""),
BD.resultSet.getString(""),
BD.resultSet.getString(""),
BD.resultSet.getString(""),
BD.resultSet.getString(""),
BD.resultSet.getString(""),
});
BD.resultSet.first();
}
catch(SQLException erro) {
JOptionPane.showMessageDialog(null, " Tabela Limpa! ");
}
}
//Metodo para setar os botoes, quando true o botao fica disponivel, false indisponivel.
public void setBotoes(boolean bExibir,boolean bLimpar,boolean bPesquisar,boolean cEscolha,boolean tConsulta)
{
{
btExibir.setEnabled(bExibir);
btLimpar.setEnabled(bLimpar);
btPesquisar.setEnabled(bPesquisar);
cbxEscolha.setEnabled(cEscolha);
txtConsulta.setEnabled(tConsulta);
}
}
//Metodo que filtra as informações da tabela em relaçao ao banco de dados.
public void pesquisaTabela()
{
DefaultTableModel modelo = (DefaultTableModel) tabela.getModel();
modelo.setNumRows(0);
try{
while (BD.resultSet.next())
modelo.addRow(new Object [] {
BD.resultSet.getString("codigo"),
BD.resultSet.getString("grupo"),
BD.resultSet.getString("produto"),
BD.resultSet.getString("estoque"),
BD.resultSet.getString("custo"),
BD.resultSet.getString("venda"),
BD.resultSet.getString("datacompra"),
BD.resultSet.getString("validade"),
});
BD.resultSet.first();
setBotoes(true,true,true,true,true);
}
catch(Exception erro) {}
}
}
por favor aceito criticas, e como eu conseguiria usar uma TableModel, adaptaria a este codigo, mas tambem como colocar para imprimir esta tabela.