Non riesco a capire... ti posto il codice:
codice:
package Palestra;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
/**
* TableRenderDemo is just like TableDemo, except that it
* explicitly initializes column sizes and it uses a combo box
* as an editor for the Sport column.
*/
public class TableRenderDemo extends JPanel {
public JTable table;
public JScrollPane scrollPane;
public JPanel panel;
public JButton salva;
public JButton elimina;
public TableRenderDemo() {
//super(new GridLayout(2,2));
//JTable table;
table = new JTable(new MyTableModel());
scrollPane = new JScrollPane(table);
panel = new JPanel();
salva = new JButton("Salva");
elimina = new JButton("Elimina");
}
/*
* This method picks good column sizes.
* If all column heads are wider than the column's cells'
* contents, then you can just use column.sizeWidthToFit().
*/
private void initColumnSizes(JTable table) {
MyTableModel model = (MyTableModel)table.getModel();
TableColumn column = null;
Component comp = null;
int headerWidth = 0;
int cellWidth = 0;
TableCellRenderer headerRenderer =
table.getTableHeader().getDefaultRenderer();
for (int i = 0; i < 2; i++) {
column = table.getColumnModel().getColumn(i);
comp = headerRenderer.getTableCellRendererComponent(
null, column.getHeaderValue(),
false, false, 0, 0);
headerWidth = comp.getPreferredSize().width;
cellWidth = comp.getPreferredSize().width;
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
}
}
public void setUpSportColumn(JTable table,
TableColumn sportColumn) {
//Set up the editor for the sport cells.
JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Knitting");
comboBox.addItem("Speed reading");
comboBox.addItem("Pool");
comboBox.addItem("None of the above");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
//Set up tool tips for the sport cells.
DefaultTableCellRenderer renderer =
new DefaultTableCellRenderer();
renderer.setToolTipText("Click for combo box");
sportColumn.setCellRenderer(renderer);
}
//####################################################################################
class MyTableModel extends AbstractTableModel {
public String[] columnNames = {"First Name",
"Last Name",
"Sport"};
public Object[][] data = GetListaTipoMacchinaDB();
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
/*
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
*/
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col > 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
private void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + data[i][j]);
}
System.out.println();
}
System.out.println("--------------------------"); }
}
public JPanel setup() {
System.out.println("finestra inizializzata");
table.setPreferredScrollableViewportSize(new Dimension(400, 100));
table.setFillsViewportHeight(true);
initColumnSizes(table);
panel.add(scrollPane);
panel.add(salva, BorderLayout.SOUTH);
panel.add(elimina);
scrollPane.setVisible(true);
salva.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
setListaTipoMacchinaDB();
//initColumnSizes(table);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
);
elimina.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
deleteListaTipoMacchinaDB();
setup();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
);
System.out.println(getRowDB());
return panel;
}
public Object[][] GetListaTipoMacchinaDB(){
Object[][] palestra = new Object[getRowDB()][3];
try {
// Load the MySQL JDBC driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.out.println ("MySQL device driver does not exist");
System.exit(1);
}
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn = DriverManager.getConnection ("jdbc:odbc:magazzino");
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery("SELECT * FROM MACCHINE");
// Iterate through the result and print the employee names
int i=0;
while(rset.next()){
palestra[i][0] = rset.getString("COD");
palestra[i][1] = rset.getString("TIPO");
palestra[i][2] = new Boolean(rset.getString("SELECTION"));
i++;
}
// Close the RseultSet
rset.close();
// Close the Statement
stmt.close();
// Close the connection
conn.close();
} catch (SQLException e) {
System.out.println("Error accessing DB ");
System.out.println(" Error code is : "+e.getErrorCode());
System.out.println(" Error message is :"+e.getMessage());
}
Object[][] data = {
{"Mary", "Campione", new Boolean(true)}
};
return palestra;
}
public void setListaTipoMacchinaDB(){
Object[][] dataSet = GetListaTipoMacchinaDB();
try {
// Load the MySQL JDBC driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.out.println ("MySQL device driver does not exist");
System.exit(1);
}
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn = DriverManager.getConnection("jdbc:odbc:;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Documents and Settings\\NAVARRO_S\\workspace\\XML\\magazzino.mdb");
// Create a Statement
Statement stmt = conn.createStatement();
for(int i=0; i<dataSet.length ; i++){
stmt.executeUpdate("UPDATE MACCHINE SET COD ='" + table.getValueAt(i, 0)+"'" +
", TIPO ='" + table.getValueAt(i, 1) +"'" +
", SELECTION ='" + table.getValueAt(i, 2) +"'" +
" WHERE COD='" + table.getValueAt(i, 0)+"'" +
";");
}
// Close the Statement
stmt.close();
// Close the connection
conn.close();
} catch (SQLException e) {
System.out.println("Error accessing DB ");
System.out.println(" Error code is : "+e.getErrorCode());
System.out.println(" Error message is :"+e.getMessage());
}
}
public void deleteListaTipoMacchinaDB(){
Object[][] dataSet = GetListaTipoMacchinaDB();
System.out.println("cancello: " + dataSet[table.getSelectedRow()][0]);
try {
// Load the MySQL JDBC driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.out.println ("MySQL device driver does not exist");
System.exit(1);
}
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn = DriverManager.getConnection("jdbc:odbc:;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Documents and Settings\\NAVARRO_S\\workspace\\XML\\magazzino.mdb");
// Create a Statement
Statement stmt = conn.createStatement();
for(int i=0; i<dataSet.length ; i++){
stmt.executeUpdate("DELETE FROM MACCHINE WHERE COD='"+ dataSet[table.getSelectedRow()][0] +"'"+";");
}
setListaTipoMacchinaDB();
// Close the Statement
stmt.close();
// Close the connection
conn.close();
} catch (SQLException e) {
System.out.println("Error accessing DB ");
System.out.println(" Error code is : "+e.getErrorCode());
System.out.println(" Error message is :"+e.getMessage());
}
setup();
}
public int getRowDB(){
int i = 1;
try {
// Load the MySQL JDBC driver
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException e) {
System.out.println ("MySQL device driver does not exist");
System.exit(1);
}
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn = DriverManager.getConnection("jdbc:odbc:;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=C:\\Documents and Settings\\NAVARRO_S\\workspace\\XML\\magazzino.mdb");
// Create a Statement
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT COUNT(*) AS numeroRighe FROM MACCHINE;");
rset.next();
i = rset.getInt("numeroRighe");
rset.close();
// Close the Statement
stmt.close();
// Close the connection
conn.close();
} catch (SQLException e) {
System.out.println("Error accessing DB ");
System.out.println(" Error code is : "+e.getErrorCode());
System.out.println(" Error message is :"+e.getMessage());
}
return i;
}
}