codice:
public void nuovo(ActionEvent n){
JDialog finestra = new JDialog();
JButton send = new JButton(" INVIA ");
JButton canc = new JButton(" CANCELLA ");
Border borderbg = LineBorder.createBlackLineBorder();
JLabel intestazione;
JLabel left;
JLabel right;
JLabel desc = new JLabel("Descrizione");
JLabel part = new JLabel("Partecipanti");
JLabel tipo = new JLabel("Tipologia");
// costruzione finestra
finestra.setTitle("CARICAMENTO DATI NUOVO INFORTUNIO");
finestra.setSize(800,750);
finestra.setLocation(250,10);
finestra.getContentPane().setBackground(Color.white);
finestra.setVisible(true);
finestra.setLayout(null);
finestra.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
// fine costruzione finestra
// costruzione intestazione
ImageIcon logoLeft = createImageIcon("immagini/logonuovo.png",
"logo S.Gobain");
left = new JLabel(logoLeft);
ImageIcon logoRight = createImageIcon("immagini/logonuovo.png",
"logo S.Gobain");
right = new JLabel(logoRight);
String intest ="<html><table bgcolor=white width=100% align=center ><tr>"+
"<td ><h1 align=center><font color=red size=9>Inserimento</font></h1>" +
"<hr align=center size=6 border=8 width=80%>" +
"<h1 align=center><font color=red size=5 >Inserimento di un nuovo evento nel Database</font></h1></td>"+
"</tr></table></html>";
intestazione = new JLabel(intest);
intestazione.setBackground(Color.lightGray);
finestra.add(left);
left.setBounds(30,25, 103, 100);
finestra.add(right);
right.setBounds(660,25, 103, 100);
finestra.add(intestazione);
intestazione.setBounds(180, 20, 650, 100);
// fine intestazione
// costruzione textarea descrizione
String lbd ="<html><font color=red size=6>Descrizione accaduto</font></html>";
JLabel lbdesc = new JLabel(lbd);
finestra.add(lbdesc);
lbdesc.setBounds(30, 420, 250, 25);
final JTextArea descriz = new JTextArea(250,100);
descriz.setEditable(true);
Font f = new Font(" ", Font.BOLD,15);
descriz.setFont(f);
descriz.setForeground(Color.blue);
descriz.setBackground(Color.lightGray);
descriz.setBorder(borderbg);
JScrollPane sc = new JScrollPane(descriz,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
finestra.add(sc);
sc.setBounds(30, 450, 250, 100);
// fine costruzione textarea descrizione
// costruzione textarea note
String lbn ="<html><font color=red size=6>Note</font></html>";
JLabel lbnt = new JLabel(lbn);
finestra.add(lbnt);
lbnt.setBounds(350, 420, 250, 25);
final JTextArea note = new JTextArea(250,100);
note.setEditable(true);
Font fnote = new Font(" ", Font.BOLD,15);
note.setFont(fnote);
note.setForeground(Color.blue);
note.setBackground(Color.lightGray);
note.setBorder(borderbg);
JScrollPane scno = new JScrollPane(note,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
finestra.add(scno);
scno.setBounds(350, 450, 250, 100);
// fine costruzione textarea note
// costruzione cmbobox tipologia infortunio
String cmb ="<html><font color=red size=6>Tipologia </font></html>";
JLabel lbcmb = new JLabel(cmb);
finestra.add(lbcmb);
lbcmb.setBounds(30, 200, 250, 25);
String tp[] = {"eventouno","eventodue","eventotre","Tipologia Incidente"};
final JComboBox tipologia = new JComboBox(tp);
tipologia.setSelectedIndex(3);
tipologia.setEditable(false);
finestra.add(tipologia);
tipologia.setBackground(Color.white);
tipologia.setBounds(300, 200, 150, 35);
// fine costruzione combobox tipologia evento
/** costruzione combobox
* data evento
*/
String dt ="<html><font color=red size=6>Data evento</font></html>";
JLabel lbdt = new JLabel(dt);
finestra.add(lbdt);
lbdt.setBounds(30, 280, 250, 25);
// combo relativo ai giorni
String gg[] = {"31","30","29","28","27","26","25","24","23","22","21","20","19","18","17","16","15","14","13","12","11","10","09","08","07","06","05","04","03","02","01","gg"};
final JComboBox giorni = new JComboBox(gg);
giorni.setSelectedIndex(31);
giorni.setEditable(false);
finestra.add(giorni);
giorni.setBackground(Color.white);
giorni.setBounds(300, 280, 45, 35);
// combo relativo ai mesi
String mm[] = {"01","02","03","04","05","06","07","08","09","10","11","12","mm"};
final JComboBox mesi = new JComboBox(mm);
mesi.setSelectedIndex(12);
mesi.setEditable(false);
finestra.add(mesi);
mesi.setBackground(Color.white);
mesi.setBounds(350, 280, 50, 35);
// combo relativo agli anni
String aa[] = {"2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018","2019","2020","aaaa"};
final JComboBox anni = new JComboBox(aa);
anni.setSelectedIndex(14);
anni.setEditable(false);
finestra.add(anni);
anni.setBackground(Color.white);
anni.setBounds(408, 280, 60, 35);
// fine combobox data incidente
// costruzione bottone inserimento
String ins ="<html><font color=blue size=6>INVIA</font></html>";
JButton insert = new JButton(ins);
finestra.add(insert);
insert.setBounds(80, 600, 130,45);
/** costruzione-preparazione dei dati
* e query dell'insert
*/
insert.addActionListener(new ActionListener() {
private String dtinc;
private DateFormat dt;
private java.util.Date dtu;
private java.sql.Date dti;
private String dta;
private String par;
private String txd;
private String txn;
public void actionPerformed(ActionEvent e) {
String d = (String)giorni.getSelectedItem();
int day = Integer.parseInt(d);
String m = (String)mesi.getSelectedItem();
int month = Integer.parseInt(m);
String a = (String)anni.getSelectedItem();
int year = Integer.parseInt(a);
/** isValid(day,month,year) è un metodo per validare le date
**/
if(isValid(day,month,year)== true){
/** ho provato a passare il valore della JTextArea, sia quello della
* descizione che delle note, ad una stringa, ma il risultato non cambia
**/
txd = descriz.getText();
txn = note.getText();
dta = "Analisi da effettuare"; // data analisi evento si posta con la dicitura analsi da effettuare
par ="Analisi da effettuare"; // partecipanti evento si posta con la dicitura analsi da effettuare
try{
dtinc = ""+ day +"/"+month+"/"+year;
dt = new SimpleDateFormat("dd/MM/yyyy");
dtu = dt.parse(dtinc);
dti = new java.sql.Date(dtu.getTime());
}catch(ParseException pe){
JOptionPane.showMessageDialog(null, pe);
}// fine catch
}// fine if
else{
JOptionPane.showMessageDialog(null, + day +"/"+month+"/"+year+ " Data non valida");
}// fine else
// registrazione nuovo incidente
try {
ConnettiDb.Connessione();
ConnettiDb.stmt= (Statement)ConnettiDb.conn.createStatement();
ConnettiDb.stmt.executeUpdate("INSERT INTO Anagrafica(DataEvento, DataAnalisi, Tipo, Descrizione, Partecipanti, Note, gg, mm, aaaa)VALUES(#" + dti + "#, '" + dta + "', '" + tipologia.getSelectedItem() + "', '" + txd + "', '" + par + "', '" + txn + "', " + day + ", " + month + ", " + year+ ")");
ConnettiDb.stmt.close();
JOptionPane.showMessageDialog(null, "Registrazione effettuata");
}// fine try
catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
ex.printStackTrace();
}// fine primo catch
// fine registrazione incidente
}
});