Olá amigo
// Button PesquisaCliente
jButtonPesquisaCliente.setIcon(clienteButtonIcon); // NOI18N
// jButtonPesquisaCliente.setText(resourceMap.getString("jButton1.text")); // NOI18N
jButtonPesquisaCliente.setName("jButton1"); // NOI18N
jButtonPesquisaCliente.setBounds(400, 20,30,30);
jButtonPesquisaCliente.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonPesquisaClienteActionPerformed(evt);
}
});
jPanel2.add(jButtonPesquisaCliente);
private void jButtonPesquisaClienteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Consultacliente.setNome(tfNomeCliente.getText());
Consultacliente consultaCliente = new Consultacliente(null);
consultaCliente.setModal(true);
consultaCliente.setVisible(true);
id_cliente = consultaCliente.getCodigo();
try {
if (!id_cliente.equals(null)) {
selecionaCliente();
}
else {
tfIdCliente.requestFocus();
}
} catch (Exception ex) {
Logger.getLogger(FrameOrdemServicoOld.class.getName()).log(Level.SEVERE, null, ex);
}
}
/*
* Consultacliente.java
*
* Created on 24 de Agosto de 2008, 19:48
*/
package assistenciadesktop;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.sql.*;
import java.util.*;
import java.awt.GridLayout;
import assistenciadesktop.FrameClientes_old;
import bd.ConexaoBancoDados;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.Object;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
/**
*
* @author silvio
*/
public class Consultacliente extends javax.swing.JDialog {
//private JTable jTable = null;
private java.sql.Statement stmt;
public int linhastabela;
public Object Codigo;
private boolean okSelecionado;
static String nomeConsulta;
/** Creates new form Consultacliente */
public Consultacliente(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
Consultacliente(Object object) {
try {
initialize();
} catch (ClassNotFoundException ex) {
Logger.getLogger(Consultacliente.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Object getCodigo() {
return Codigo;
//throw new UnsupportedOperationException("Not yet implemented");
}
public static void setNome(String Nome) {
nomeConsulta = Nome;
}
public String getNome() {
return nomeConsulta;
}
public boolean SelecionaDados()
{
okSelecionado = false; //Marcamos que o ok não foi selecionado
setModal(true); //A dialog tem que ser modal. Só pode retornar do setVisible após ficar invisível.
setVisible(true); //Mostramos a dialog e esperamos o usuário escolher alguma coisa.
return okSelecionado; //Retornamos true, se ele pressionou ok.
}
private void mostrarResultados(ResultSet rs) throws SQLException, ClassNotFoundException {
Statement statement;
String[] tableColumnsName = {"", "Código", "Nome", "Fone"};
final DefaultTableModel aModel = new DefaultTableModel(tableColumnsName, 0) {
@Override
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
if (getValueAt(0, column) != null) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
} else {
returnValue = Object.class;
}
return returnValue;
}
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
};
if (linhastabela == 0) {
JOptionPane.showMessageDialog(this, "Nehum registro encontrado");
return;
}
else {
final JTable jTable = new JTable();
JScrollPane jScrollPane = new JScrollPane(jTable);
getContentPane().add(jScrollPane,java.awt.BorderLayout.CENTER);
aModel.setColumnIdentifiers(tableColumnsName);
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int colNo = rsmd.getColumnCount();
while(rs.next()){
Object[] objects = new Object[colNo];
objects[0] = rs.getObject(1);
objects[1] = rs.getObject(2);
objects[2] = rs.getObject(3);
objects[3] = rs.getObject(4);
aModel.addRow(objects);
}
jTable.setModel(aModel);
jTable.setFillsViewportHeight(true);
jTable.getTableHeader().setReorderingAllowed(false);
jTable.getColumnModel().getColumn(0).setWidth(0);
jTable.getColumnModel().getColumn(0).setPreferredWidth(0);
jTable.getColumnModel().getColumn(1).setPreferredWidth(50);
jTable.getColumnModel().getColumn(2).setPreferredWidth(300);
jTable.getColumnModel().getColumn(3).setPreferredWidth(100);
jTable.setAutoCreateRowSorter(true);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(jTable.getModel());
jTable.setRowSorter(sorter);
jTable.getColumnModel().removeColumn( jTable.getColumnModel().getColumn(0));
jTable.getSelectionModel().setSelectionInterval(0, 0);
jTable.requestFocusInWindow();
jTable.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if (e.getClickCount() == 2){
// Listaclientes.id = jTable.getModel().getValueAt(jTable.getSelectedRow(),0);
Codigo = jTable.getModel().getValueAt(jTable.getSelectedRow(),0);
dispose();
}
}
});
}
}
private void getJTable() throws ClassNotFoundException, ClassNotFoundException{
Statement statement;
ResultSet result;
try {
stmt = bd.ConexaoBancoDados.conexao().createStatement();
String query = "select id,id,nome,telefone from clientes where upper(nome) ilike "+"upper('%"+
nomeConsulta+"%') order by nome";
ResultSet rs = stmt.executeQuery(query);
mostrarResultados(rs);
stmt.close();
}
catch (SQLException sqlex) {sqlex.printStackTrace();}
}
private void initialize() throws ClassNotFoundException {
ResultSet rst;
this.setSize(600,400);
Dimension tela = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((tela.width-this.getSize().width)/2,
(tela.height-this.getSize().height)/2);
nomeConsulta = this.getNome();
try {
stmt = bd.ConexaoBancoDados.conexao().createStatement();
String query = "select count(*) from clientes where upper(nome) ilike "+"upper('%"+
nomeConsulta+"%')";
rst = stmt.executeQuery(query);
while(rst.next()){
Object[] objects = new Object[1];
objects[0] = rst.getObject(1);
linhastabela = rst.getInt(rst.getRow());
}
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(Consultacliente.class.getName()).log(Level.SEVERE, null, ex);
}
getJTable();
this.setTitle("Clientes");
this.setDefaultCloseOperation(Consultacliente.DISPOSE_ON_CLOSE);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setName("Form"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Consultacliente dialog = new Consultacliente(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}
espero ter ajudado
Silvio Guedes