codice:
public class MainGUI extends javax.swing.JFrame implements ComponentListener {
static protected Connection con;
private JButton elimina;
private JButton aggiungi;
private JButton modifica;
private JButton vai;
private JButton logout;
private JButton save;
private JLabel visLabel;
private JComboBox visualizza;
private JScrollPane jScrollPane1;
private JTable table;
private Statement cmd;
private TableModel tableModel;
private static int update;
private static boolean aggiornato;
private int colonne;
private int row;
private String [][] dati;
private String [] nomiColonne;
private int col,riga;
/**
*
*/
private static final long serialVersionUID = 7525219833793207982L;
public MainGUI(Connection con) throws SQLException {
super();
initGUI(con);
cmd = con.createStatement ();
}
void initGUI(final Connection con) {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
this.setVisible(true);
this.setPreferredSize(new java.awt.Dimension(1000, 600));
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setMinimumSize(new java.awt.Dimension(1000, 600));
this.addComponentListener(this);
{
ComboBoxModel visualizzaModel =
new DefaultComboBoxModel(
new String[] { "Ambulanze", "Ambulatori","Inservienti",
"Interventi","Medici","Paramedici",
"Pazienti","Prenotazioni",
"Reparti","Ricoveri", "Strumentazione"});
visualizza = new JComboBox();
getContentPane().add(visualizza);
visualizza.setModel(visualizzaModel);
visualizza.setBounds(81, 3, 130, 23);
}
{
visLabel = new JLabel();
getContentPane().add(visLabel);
visLabel.setText("Visualizza :");
visLabel.setBounds(5, 6, 104, 16);
}
{
save = new JButton();
getContentPane().add(save);
save.setText("Salva");
save.setBounds(12, 544, 79, 23);
}
{
logout = new JButton();
getContentPane().add(logout);
logout.setText("Logout");
logout.setBounds(912, 544, 77, 23);
logout.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try {
cmd.close();
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
dispose();
MenuLogin m = new MenuLogin();
m.setVisible(true);
}
});
}
{
jScrollPane1 = new JScrollPane();
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(12, 37, 976, 495);
jScrollPane1.setAutoscrolls(true);
{
table = new JTable();
jScrollPane1.setViewportView(table);
}
}
{
vai = new JButton();
getContentPane().add(vai);
vai.setText("Vai");
vai.setBounds(223, 3, 67, 23);
vai.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String tabella = "";
if(visualizza.getSelectedItem()=="Ambulanze")
tabella = "AMBULANZA";
else if(visualizza.getSelectedItem()=="Ambulatori")
tabella = "AMBULATORIO";
else if(visualizza.getSelectedItem()=="Inservienti")
tabella = "INSERVIENTE";
else if(visualizza.getSelectedItem()=="Interventi")
tabella = "INTERVENTO";
else if(visualizza.getSelectedItem()=="Medici")
tabella = "MEDICO";
else if(visualizza.getSelectedItem()=="Paramedici")
tabella = "PARAMEDICO";
else if(visualizza.getSelectedItem()=="Pazienti")
tabella = "PAZIENTE";
else if(visualizza.getSelectedItem()=="Prenotazioni")
tabella = "PRENOTAZIONE";
else if(visualizza.getSelectedItem()=="Reparti")
tabella = "REPARTO";
else if(visualizza.getSelectedItem()=="Ricoveri")
tabella = "RICOVERO_ATTUALE";
else if(visualizza.getSelectedItem()=="Strumentazione")
tabella = "STRUMENTAZIONE";
String qry1 ="SELECT COUNT(*) AS RECORD_COUNT FROM "+tabella;
String qry2 = "SELECT * FROM "+tabella;
try {
ResultSet res = cmd.executeQuery(qry1);
row=0;
while(res.next())
row = res.getInt("RECORD_COUNT");
res = cmd.executeQuery(qry2);
ResultSetMetaData md = res.getMetaData();
colonne = md.getColumnCount();
nomiColonne = new String[colonne];
dati = new String[row][colonne];
for(int i = 1; i<= colonne; i++)
nomiColonne[i-1] = md.getColumnName(i);
int counter =0;
while(res.next()){
for (int i = 1; i <= colonne; i++) {
dati[counter][i-1] = res.getString(i);
}counter ++;
}
res.close();
tableModel = new DefaultTableModel(dati , nomiColonne);
table.setModel(tableModel);
tableModel.addTableModelListener(new TableModelListener(){
@Override
public void tableChanged(TableModelEvent e) {
System.out.println("ljksfjskdfh");
if(e.getType()==TableModelEvent.UPDATE){
update = e.getType();
aggiornato = true;
col= e.getColumn();
riga = e.getFirstRow();
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
}
}
});
}
catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}});
if(aggiornato == true && update==TableModelEvent.UPDATE){
System.out.println(tableModel.getValueAt(riga, col));
//Query prova errata String up = "UPDATE AMBULANZA SET "+table.getColumnName(col)+" = "+"'"+table.getValueAt(riga, col)+"'";
int r = cmd.executeUpdate(up);
//JOptionPane op = new JOptionPane("Hai aggiornato "+r);
aggiornato = false;
}
}
{
modifica = new JButton();
getContentPane().add(modifica);
modifica.setText("Modifica");
modifica.setBounds(434, 3, 87, 23);
}
{
aggiungi = new JButton();
getContentPane().add(aggiungi);
aggiungi.setText("Aggiungi");
aggiungi.setBounds(336, 3, 87, 23);
}
{
elimina = new JButton();
getContentPane().add(elimina);
elimina.setText("Elimina");
elimina.setBounds(532, 3, 87, 23);
}
pack();
this.setSize(1000, 600);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
@Override
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub
}
@Override
public void componentResized(ComponentEvent e) {
save.setBounds(12, this.getHeight()-56, 79, 23);
logout.setBounds(this.getWidth()-88, this.getHeight()-56, 79, 23);
table.setSize(this.getWidth()-24, this.getHeight()-127);
jScrollPane1.setBounds(12, 37,this.getWidth()-24, this.getHeight()-127);
}
@Override
public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub
}
}