Bom Pessoal, preciso muito da ajuda de vocês, não consigo achar uma solução, gostaria de saber como faço para diminuir 1 txtField por outro e mostrar apenas na busca que eu fizer na
tabela.
A tela Do Sistema.
E O Codigo Do JFrame
package br.com.sistema.JFrame;
import br.com.sistema.bean.FormularioBean;
import br.com.sistema.bean.FormularioControl;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
public class Formulario extends javax.swing.JFrame {
public Formulario() {
initComponents();
}
public void sair(){
int resposta = JOptionPane.showConfirmDialog(this, "Sair Do Programa?",
"Encerrar", JOptionPane.OK_CANCEL_OPTION);
if (resposta == JOptionPane.OK_OPTION) {
dispose();
}
}
DefaultTableModel tmFormulario = new DefaultTableModel(null, new String[]{"Codigo", "Data", "Solcitante", "Discriminação", "Valor Saida", "Valor Entrada", "Saldo"});
ListSelectionModel lsmFormulario;
String tipoCadastro;
List<FormularioBean> formularios;
public void cancelarRegistro(){
cfData.setText(null);
tfDiscri.setText(null);
tfSolicitante.setText(null);
tfValorSaida.setText(null);
tfvalorEntrada.setText(null);
btSalvar.setEnabled(false);
desabilitarCampos();
}
public void calculaSaldo(){
}
private void novoFormulario(){
habilitarCampos();
cfData.setText("");
tfSolicitante.setText("");
tfDiscri.setText("");
tfValorSaida.setText("");
tfvalorEntrada.setText("");
}
private void desabilitarCampos(){
cfData.setEditable(false);
tfSolicitante.setEditable(false);
tfDiscri.setEditable(false);
tfValorSaida.setEditable(false);
tfvalorEntrada.setEditable(false);
//tfSaldo.setEditable(false);
btSalvar.setEnabled(false);
}
private void habilitarCampos(){
cfData.setEditable(true);
tfSolicitante.setEditable(true);
tfDiscri.setEditable(true);
tfValorSaida.setEditable(true);
tfvalorEntrada.setEditable(true);
//tfSaldo.setEditable(true);
btSalvar.setEnabled(true);
}
private void cadastraFormulario(){
if(verificarCampos()&& verificarValorEnt()&&verificarValorSaida()){
FormularioBean formBean = new FormularioBean();
formBean.setData_form(cfData.getText().trim().toUpperCase());
formBean.setSolicit_form(tfSolicitante.getText().trim().toUpperCase());
formBean.setDiscrimi_form(tfDiscri.getText().trim().toUpperCase());
formBean.setValorSaida_form(Double.parseDouble(tfValorSaida.getText()));
formBean.setValorEnt_form(Double.parseDouble(tfvalorEntrada.getText()));
FormularioControl formControl = new FormularioControl();
formControl.cadastrarFormulario(formBean);
desabilitarCampos();
}
}
private boolean verificarValorSaida(){
if(tfValorSaida.getText().length() <= 0){
return false;
}else{
return true;
}
}
private boolean verificarValorEnt(){
if(tfvalorEntrada.getText().length() <= 0){
return false;
}else{
return true;
}
}
private boolean verificarCampos(){
if(!cfData.getText().trim().equals("")||!tfSolicitante.getText().trim().equals
("")||!tfDiscri.getText().trim().equals("")||!tfValorSaida.getText().trim().equals("")
||!tfvalorEntrada.getText().trim().equals("")){
return true;
}else{
return false;
}
}
private void alteraFormulario(){
if(tbRegistro.getSelectedRow()!= -1){
habilitarCampos();
}else{
JOptionPane.showMessageDialog(this, "Selecione Um Registros!!!");
}
}
private void alterarRegistro(){
if(verificarCampos()){
FormularioBean formBean = new FormularioBean();
formBean.setCod_form(formularios.get(tbRegistro.getSelectedRow()).getCod_form());
formBean.setData_form(cfData.getText().trim().toUpperCase());
formBean.setSolicit_form(tfSolicitante.getText().trim().toUpperCase());
formBean.setDiscrimi_form(tfDiscri.getText().trim().toUpperCase());
formBean.setValorSaida_form(Double.parseDouble(tfValorSaida.getText().trim()));
formBean.setValorEnt_form(Double.parseDouble(tfvalorEntrada.getText().trim()));
FormularioControl formControl = new FormularioControl();
formControl.alterarFormulario(formBean);
desabilitarCampos();
}else{
JOptionPane.showMessageDialog(this, "Informe Todos os Dados Do Formulario!!!");
cfData.requestFocus();
tfSolicitante.requestFocus();
tfDiscri.requestFocus();
tfValorSaida.requestFocus();
tfvalorEntrada.requestFocus();
}
}
private void listarRegistros(){
FormularioControl formControl = new FormularioControl();
formularios = formControl.listarFormularios("%" + tfPesquisaRegistro.getText().trim()+ "%");
mostrarRegistros(formularios);
}
private void mostrarRegistros(List<FormularioBean> formularios){
while(tmFormulario.getRowCount()>0){
tmFormulario.removeRow(0);
}
if(formularios.size()== 0){
JOptionPane.showMessageDialog(this, "Nenhum Registro Encontrado!!!");
}else{
//remover todas as linha q tenha em nossa tabelas de clientes criando um laço
String[] campos = new String[]{"null", "null", "null", "null", "null", "null", "null"};//array vazio
for(int i = 0; i < formularios.size(); i++ ){
tmFormulario.addRow(campos);
tmFormulario.setValueAt(formularios.get(i).getCod_form(),i,0);
tmFormulario.setValueAt(formularios.get(i).getData_form(),i,1);
tmFormulario.setValueAt(formularios.get(i).getSolicit_form(),i,2);
tmFormulario.setValueAt(formularios.get(i).getDiscrimi_form(),i,3);
tmFormulario.setValueAt(formularios.get(i).getValorSaida_form(),i,4);
tmFormulario.setValueAt(formularios.get(i).getValorEnt_form(),i,5);
tmFormulario.setValueAt(formularios.get(i).getSaldo(), i, 6);
}
}
}
//tbRegistroLinhaSelecionada
private void tbRegistroLinhaSelecionada(JTable tb){
// /*
if(tb.getSelectedRow()!= -1){
cfData.setText(formularios.get(tb.getSelectedRow()).getData_form());
tfSolicitante.setText(formularios.get(tb.getSelectedRow()).getSolicit_form());
tfDiscri.setText(formularios.get(tb.getSelectedRow()).getDiscrimi_form());
tfValorSaida.setText(String.valueOf(formularios.get(tbRegistro.getSelectedRow()).getValorSaida_form()));
tfValorSaida.setText(String.valueOf(formularios.get(tbRegistro.getSelectedRow()).getValorEnt_form()));
}else{
cfData.setText("");
tfSolicitante.setText("");
tfDiscri.setText("");
tfValorSaida.setText("");
tfvalorEntrada.setText("");
}
}
private void excluirRegistro(){
if(tbRegistro.getSelectedRow() !=-1){
int resposta = JOptionPane.showConfirmDialog(this, "Excluir Registro Selecionado?", "Confirmação", JOptionPane.YES_NO_OPTION);
if(resposta == JOptionPane.YES_NO_OPTION){
FormularioControl formControl = new FormularioControl();
formControl.deletaRegistro(formularios.get(tbRegistro.getSelectedRow()).getCod_form());
}
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
cfData = new javax.swing.JFormattedTextField();
tfSolicitante = new javax.swing.JTextField();
tfValorSaida = new javax.swing.JTextField();
tfvalorEntrada = new javax.swing.JTextField();
tfSaldo = new javax.swing.JTextField();
btNovo = new javax.swing.JButton();
btSalvar = new javax.swing.JButton();
btSair = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
tbRegistro = new javax.swing.JTable();
btPesquisaForm = new javax.swing.JButton();
tfPesquisaRegistro = new javax.swing.JTextField();
btExclui = new javax.swing.JButton();
btAlterar = new javax.swing.JButton();
tfDiscri = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setText("Data:");
jLabel2.setText("Solicitante/Origem:");
jLabel3.setText("Discriminação:");
jLabel4.setText("Valor Saida:");
jLabel5.setText("Valor Entrada:");
jLabel6.setText("Saldo:");
cfData.setEditable(false);
tfSolicitante.setEditable(false);
tfValorSaida.setEditable(false);
tfvalorEntrada.setEditable(false);
tfSaldo.setEditable(false);
tfSaldo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tfSaldoActionPerformed(evt);
}
});
btNovo.setText("Novo");
btNovo.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btNovoActionPerformed(evt);
}
});
btSalvar.setText("Salvar");
btSalvar.setEnabled(false);
btSalvar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSalvarActionPerformed(evt);
}
});
btSair.setText("Sair");
btSair.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSairActionPerformed(evt);
}
});
tbRegistro.setModel(tmFormulario);
jScrollPane2.setViewportView(tbRegistro);
btPesquisaForm.setText("Pesquisar");
btPesquisaForm.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btPesquisaFormActionPerformed(evt);
}
});
btExclui.setText("Excluir");
btExclui.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btExcluiActionPerformed(evt);
}
});
btAlterar.setText("Alterar");
btAlterar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAlterarActionPerformed(evt);
}
});
tfDiscri.setEditable(false);
jButton1.setText("Cancelar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(117, 117, 117)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(tfvalorEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tfValorSaida, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tfDiscri, javax.swing.GroupLayout.PREFERRED_SIZE, 488, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(cfData, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(31, 31, 31)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(tfSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(101, 101, 101)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(tfSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 1018, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(btPesquisaForm)
.addGap(36, 36, 36)
.addComponent(tfPesquisaRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(79, 79, 79)
.addComponent(btNovo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btSalvar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btAlterar)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btExclui)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(btSair)))
.addContainerGap(25, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btPesquisaForm)
.addComponent(tfPesquisaRegistro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btNovo)
.addComponent(btSalvar)
.addComponent(btSair)
.addComponent(btExclui)
.addComponent(btAlterar)
.addComponent(jButton1))
.addGap(21, 21, 21)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 379, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(cfData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2)
.addComponent(tfSolicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(tfSaldo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tfDiscri, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel3)
.addGap(6, 6, 6)))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tfValorSaida, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(tfvalorEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(62, 62, 62))
);
pack();
}// </editor-fold>
private void btSairActionPerformed(java.awt.event.ActionEvent evt) {
sair();
}
private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {
tipoCadastro = "novo";
novoFormulario();
}
private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {
if(tipoCadastro.equals("novo")){
cadastraFormulario();
}
}
private void btPesquisaFormActionPerformed(java.awt.event.ActionEvent evt) {
listarRegistros();
}
private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {
tipoCadastro = "alteracao";
alterarRegistro();
}
private void btExcluiActionPerformed(java.awt.event.ActionEvent evt) {
excluirRegistro();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
tipoCadastro = "cancelar";
cancelarRegistro();
}
private void tfSaldoActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Formulario().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btAlterar;
private javax.swing.JButton btExclui;
private javax.swing.JButton btNovo;
private javax.swing.JButton btPesquisaForm;
private javax.swing.JButton btSair;
private javax.swing.JButton btSalvar;
private javax.swing.JFormattedTextField cfData;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable tbRegistro;
private javax.swing.JTextField tfDiscri;
private javax.swing.JTextField tfPesquisaRegistro;
private javax.swing.JTextField tfSaldo;
private javax.swing.JTextField tfSolicitante;
private javax.swing.JTextField tfValorSaida;
private javax.swing.JTextField tfvalorEntrada;
// End of variables declaration
}
E O codigo de Operacoes do Banco
package br.com.sistema.bean;
import br.com.sistema.conexao.ConexaoBanco;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class FormularioControl {
//
PreparedStatement ps;
ResultSet rs;
String consultaFormularioBD = "SELECT * FROM formulario WHERE data_form LIKE?";
String cadastraFormularioBD = "INSERT INTO formulario(data_form, solicit_form, discrimi_form, valorSaida_form, valorEnt_Form)"
+ " VALUES (?,?,?,?,?)";
String alteraFormulario = "UPDATE formulario SET data_form=?, =?, solicit_form=?, discrimi_form=?, valorSaida_form=?, valorEnt_form=?, "
+ " WHERE cod_form=?";
String deletarFormularioBD = "DELETE FROM formulario WHERE cod_form=?";
String deletarRegistro = "DELETE FROM formulario WHERE cod_form=?";
public FormularioControl(){
}
public void alterarFormulario(FormularioBean formBean){
try{
ConexaoBanco conexao = new ConexaoBanco();
ps = conexao.conectar().prepareStatement(alteraFormulario);
ps.setString(1, formBean.getData_form());
ps.setString(2, formBean.getSolicit_form());
ps.setString(3, formBean.getDiscrimi_form());
ps.setDouble(4, formBean.getValorSaida_form());
ps.setDouble(5, formBean.getValorEnt_form());
// ps.setDouble(6, formBean.getSaldo());
ps.setInt(6, formBean.getCod_form());
ps.executeUpdate();
conexao.desconectar();
}catch(Exception e){
e.printStackTrace();
}
}
public void cadastrarFormulario(FormularioBean formBean){
try{
ConexaoBanco conexao = new ConexaoBanco();
ps = conexao.conectar().prepareStatement(cadastraFormularioBD);
ps.setString(1, formBean.getData_form());
ps.setString(2, formBean.getSolicit_form());
ps.setString(3, formBean.getDiscrimi_form());
ps.setDouble(4, formBean.getValorSaida_form());
ps.setDouble(5, formBean.getValorEnt_form());
//ps.setDouble(6, formBean.getSaldo());
ps.executeUpdate();
conexao.desconectar();
}catch(Exception e){
e.printStackTrace();
}
}
public List<FormularioBean>listarFormularios(String data_form){
List<FormularioBean> formularios = new ArrayList();
try{
ConexaoBanco conexaoBanco = new ConexaoBanco();
ps = conexaoBanco.conectar().prepareStatement(consultaFormularioBD);
ps.setString(1, data_form);
rs = ps.executeQuery();
FormularioBean formBean;
while(rs.next()){
formBean = new FormularioBean();
formBean.setCod_form(rs.getInt("cod_form"));
formBean.setData_form(rs.getString("data_form"));
formBean.setSolicit_form(rs.getString("solicit_form"));
formBean.setDiscrimi_form(rs.getString("Discrimi_form"));
formBean.setValorSaida_form(rs.getDouble("valorSaida_form"));
formBean.setValorEnt_form(rs.getDouble("valorEnt_form"));
//formBean.setSaldo(rs.getDouble("saldo_form"));
formularios.add(formBean);
}
conexaoBanco.desconectar();
}catch(Exception e){
e.printStackTrace();
}
return formularios;
}
public void deletaRegistro(int codigoRegistro){
try{
ConexaoBanco conexao = new ConexaoBanco();
ps = conexao.conectar().prepareStatement(deletarRegistro);
ps.setInt(1, codigoRegistro);
ps.executeUpdate();
conexao.desconectar();
}catch(Exception e){
e.printStackTrace();
}
}
}