codice:
public class PannelloRicercaCollaudi extends JPanel implements ActionListener, KeyListener{
PannelloIndex index2;
JPanel panelNord = new JPanel();
JTextField input1 = new JTextField(2);
JTextField input2 = new JTextField(2);
JTextField input3 = new JTextField(4);
JTextField input4 = new JTextField(25);
JTextField input5 = new JTextField(25);
JTextField input6 = new JTextField(25);
Connection con;
Statement stat;
ResultSet rs;
String query;
JTable table;
JScrollPane scrollpane;
JButton cerca = new JButton("CERCA");
Border bord1 = BorderFactory.createRaisedBevelBorder();
Border bord2 = BorderFactory.createLoweredBevelBorder();
Border bord = BorderFactory.createCompoundBorder(bord1,bord2);
JPanel buttonPanelSud = new JPanel();
JButton stampa = new JButton("Stampa Lista");
JButton esci = new JButton("Chiudi");
JButton cancella = new JButton("Cancella");
public PannelloRicercaCollaudi(PannelloIndex index, Connection conn) {
//setta il riferimento passato
index2=index;
try {
con=conn;
stat = con.createStatement();
// se non ci sono dati chiude il resultset
if(rs != null){rs.close();}
// stringa della query per la visualizzazione delle materiali
query = "select id,commessa,data_com,cod_specifica,quantita,data,cartone from collaudo where cartone!='10000' order by id desc;";
// esegue la query con le caratteristiche della tabbellascrollabile
rs = stat.executeQuery(query);
// crea la tabella con il modello
ModelloTabella model= new ModelloTabella(rs);
table = new JTable(model);
// abilita la selezione di una linea
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//metodo che ordina la tabella in base alla header(intestazione di colonna) cliccato.
table.setRowSorter(new TableRowSorter (model));
// aggiunge la tabella allo scrollpane
scrollpane = new JScrollPane(table);
// setta il layout
this.setLayout(new BorderLayout());
//aggiunge i pannelli
panelNord.setLayout(new GridLayout(4,3));
JPanel labpanel1 = new JPanel();
JLabel label1 = new JLabel("Commessa :");
labpanel1.add(label1);
panelNord.add(labpanel1);
JPanel strPanel1 = new JPanel();
strPanel1.add(input4);
panelNord.add(strPanel1);
panelNord.add(new JPanel());
JPanel labpanel6 = new JPanel();
JLabel label6 = new JLabel("Specifica :");
labpanel6.add(label6);
panelNord.add(labpanel6);
JPanel strPanel6 = new JPanel();
strPanel6.add(input6);
panelNord.add(strPanel6);
panelNord.add(new JPanel());
JPanel labpanel2 = new JPanel();
JLabel label2 = new JLabel("Cartone :");
labpanel2.add(label2);
panelNord.add(labpanel2);
JPanel strPanel2 = new JPanel();
strPanel2.add(input5);
panelNord.add(strPanel2);
panelNord.add(new JPanel());
JPanel labPanel =new JPanel();
JLabel label = new JLabel("Data Collaudo :");
labPanel.add(label);
panelNord.add(labPanel);
JPanel strPanel = new JPanel();
strPanel.add(input1);
strPanel.add(new JLabel(" / "));
strPanel.add(input2);
strPanel.add(new JLabel(" / "));
strPanel.add(input3);
panelNord.add(strPanel);
JPanel strPanel3 = new JPanel();
strPanel3.add(cerca);
panelNord.add(strPanel3);
panelNord.setBorder(bord);
esci.setBackground(Color.red);
buttonPanelSud.add(stampa);
buttonPanelSud.add(cancella);
buttonPanelSud.add(esci);
scrollpane.setBorder(bord);
//scrollpane2.setBorder(bord);
this.add(panelNord,BorderLayout.NORTH);
this.add(scrollpane,BorderLayout.CENTER);
this.add(buttonPanelSud,BorderLayout.SOUTH);
stampa.addActionListener(this);
esci.addActionListener(this);
cancella.addActionListener(this);
cerca.addActionListener(this);
cerca.addKeyListener(this);
input1.addKeyListener(this);
input2.addKeyListener(this);
input3.addKeyListener(this);
input4.addKeyListener(this);
input5.addKeyListener(this);
input6.addKeyListener(this);
}catch(Exception e){e.printStackTrace();}
}
public void actionPerformed(ActionEvent event) {
if(event.getActionCommand()=="CERCA"){
if((!input1.getText().equals("")||!input2.getText().equals("")||!input3.getText().equals(""))&&(!input4.getText().equals("")||!input5.getText().equals("")||!input6.getText().equals(""))){
JOptionPane.showMessageDialog(null, "Inserire un parametro per la ricerca !!","Errore !",JOptionPane.ERROR_MESSAGE);
}else{
if(input1.getText().equals("")&&input2.getText().equals("")&&input3.getText().equals("")&&input4.getText().equals("")&&input5.getText().equals("")&&input6.getText().equals("")){
JOptionPane.showMessageDialog(null, "Nessun parametro inserito per la ricerca !!","Errore !",JOptionPane.ERROR_MESSAGE);
query = "select id,commessa,data_com,cod_specifica,quantita,data,cartone from collaudo where cartone!='10000' order by id desc;";
try {
Statement st1 = con.createStatement();
ResultSet rs1 = st1.executeQuery(query);
ModelloTabella model1= new ModelloTabella(rs1);
table.setModel(model1);
} catch (SQLException e) {JOptionPane.showMessageDialog(null, "Inserimento dati errato !!","Errore !",JOptionPane.ERROR_MESSAGE);}
}else{
if(input1.getText().equals("")&&input2.getText().equals("")&&input3.getText().equals("")&&!input4.getText().equals("")&&(!input5.getText().equals("")||!input6.getText().equals(""))){
JOptionPane.showMessageDialog(null, "Inserire un parametro !!","Errore !",JOptionPane.ERROR_MESSAGE);
}else{
if(input1.getText().equals("")&&input2.getText().equals("")&&input3.getText().equals("")&&(!input4.getText().equals("")||!input5.getText().equals(""))&&!input6.getText().equals("")){
JOptionPane.showMessageDialog(null, "Inserire un parametro !!","Errore !",JOptionPane.ERROR_MESSAGE);
}else{
if(!input1.getText().equals("")&&!input2.getText().equals("")&&!input3.getText().equals("")){
String data = input1.getText()+"/"+input2.getText()+"/"+input3.getText();
if(input4.getText().equals("")&&input5.getText().equals("")&&input6.getText().equals("")){
//query solo per data
query = "select id,commessa,data_com,cod_specifica,quantita,data,cartone from collaudo where cartone!='10000' and data = '"+data+"' order by id desc;";
}
}
if(input1.getText().equals("")&&input2.getText().equals("")&&input3.getText().equals("")&&!input4.getText().equals("")&&input5.getText().equals("")&&input6.getText().equals("")){
//query solo per commessa
query = "select id,commessa,data_com,cod_specifica,quantita,data,cartone from collaudo where cartone!='10000' and commessa like '%"+input4.getText()+"%';";
}
if(input1.getText().equals("")&&input2.getText().equals("")&&input3.getText().equals("")&&input4.getText().equals("")&&!input5.getText().equals("")&&input6.getText().equals("")){
//query solo per cartone
query = "select id,commessa,data_com,cod_specifica,quantita,data,cartone from collaudo where cartone!='10000' and cartone like '%"+input5.getText()+"%';";
}
if(input1.getText().equals("")&&input2.getText().equals("")&&input3.getText().equals("")&&input4.getText().equals("")&&input5.getText().equals("")&&!input6.getText().equals("")){
//query solo per specifica
query = "select id,commessa,data_com,cod_specifica,quantita,data,cartone from collaudo where cartone!='10000' and cod_specifica like '%"+input6.getText()+"%';";
}
try {
Statement st2 = con.createStatement();
ResultSet rs2 = st2.executeQuery(query);
ModelloTabella model2= new ModelloTabella(rs2);
table.setModel(model2);
} catch (SQLException e) {JOptionPane.showMessageDialog(null, "Inserimento dati errato !!","Errore !",JOptionPane.ERROR_MESSAGE);}
}
}
}
}
}
if(event.getActionCommand()=="Cancella"){
int par[]=table.getSelectedRows();
// messaggio con input per eliminazione
int delete = JOptionPane.showConfirmDialog(null, "Sei sicuro di voler cancellare il collaudo ID "+table.getValueAt(par[0], 0)+"? ");
if(delete==0){
// crea oggetto per la classe di eliminazione
CancellaCollaudo xxx = new CancellaCollaudo();
String str=""+table.getValueAt(par[0], 0);
xxx.CancellaCollaudo(str,con);
cerca.doClick();
}
if(delete==1){
JOptionPane.showMessageDialog(null, "Operazione Cancellazione annullata !!","Attenzione !",JOptionPane.INFORMATION_MESSAGE);
}
}
}
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == 10) {
cerca.doClick();
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
}