perfetto ecco tutto il codice allora
codice:
package Panel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import Componenti_Swing.CentraComponents;
import Componenti_Swing.Dimensione;
import Componenti_Swing.LabelFormat;
import Componenti_Swing.TextFieldFormat;
import Panel.MainPanel.TableExample2;
import Print.FoglioFattura;
import com.itextpdf.text.DocumentException;
import com.toedter.calendar.JDateChooser;
import esempio.Visualizzazioni;
public class PanelNoteCredito extends JPanel{
static String tutto = "Stampa tutto";
static String dataa = "Stampa per data";
static JDialog frame;
MainPanel mPanel;
JButton buttonSalva,buttonModifica,buttonCancella;
JLabel picture;
TextFieldFormat textDescrizione;
Visualizzazioni db;
JScrollPane scrollPane;
JTable tableRicette;
Font fontTable;
String descrVecchia;
Vector v;
MyTableModel3 tableModel3;
public PanelNoteCredito() {
super(new BorderLayout());
db = new Visualizzazioni("provaDB");
try {
db.connetti();
} catch (InstantiationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
fontTable = new Font("Century Gothic", Font.PLAIN, 14);
//Create the radio buttons.
// birdButton.setSelected(false);
//Group the radio buttons.
//Register a listener for the radio buttons.
//Set up the picture label.
buttonSalva = new JButton();
buttonSalva.setIcon(new ImageIcon(getClass().getResource("/resources/SalvaR.png")));
buttonSalva.setText("Salva");
// buttonSalva.setPreferredSize(new Dimension(45,49));
buttonSalva.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
String descrizione = textDescrizione.getText();
if(descrizione.length()==0)
{
JOptionPane.showMessageDialog(getRootPane(), "Non hai inserito alcun carattere nel campo Descrizione","Attenzione",JOptionPane.WARNING_MESSAGE);
}
else
{
if(db.eseguiAggiornamento("INSERT INTO DESCRIZIONENOTE VALUES('"+descrizione+"')"))
{
JOptionPane.showMessageDialog(getRootPane(), "Salvataggio eseguito con successo");
pulisci();
dimensionaTabella();
}
else
{
JOptionPane.showMessageDialog(getRootPane(), "Errore durante il salvataggio","Errore",JOptionPane.WARNING_MESSAGE);
}
}
}});//fine button stampa
buttonModifica = new JButton();
buttonModifica.setIcon(new ImageIcon(getClass().getResource("/resources/Modifica.png")));
buttonModifica.setText("Modifica");
buttonModifica.setEnabled(false);
buttonModifica.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
if(db.eseguiAggiornamento("UPDATE DESCRIZIONENOTE SET Descrizione ='"+textDescrizione.getText()+"' WHERE Descrizione ='"+descrVecchia+"'"))
{
JOptionPane.showMessageDialog(getRootPane(), "Modifica eseguita con successo");
pulisci();
dimensionaTabella();
}
else
{
JOptionPane.showMessageDialog(getRootPane(), "Errore durante la modifica","Errore",JOptionPane.WARNING_MESSAGE);
}
}});//fine button stampa
buttonCancella = new JButton();
buttonCancella.setIcon(new ImageIcon(getClass().getResource("/resources/cestino.png")));
buttonCancella.setText("Cancella");
buttonCancella.setEnabled(false);
buttonCancella.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt)
{
if(db.eseguiAggiornamento("DELETE FROM DESCRIZIONENOTE WHERE Descrizione ='"+textDescrizione.getText()+"'"))
{
JOptionPane.showMessageDialog(getRootPane(), "Eliminazione eseguita con successo");
pulisci();
dimensionaTabella();
}
else
{
JOptionPane.showMessageDialog(getRootPane(), "Errore durante l'eliminazione della nota","Errore",JOptionPane.WARNING_MESSAGE);
}
}});//fine button stampa
v = db.eseguiQuery("SELECT DESCRIZIONE FROM DESCRIZIONENOTE ORDER BY DESCRIZIONE");
tableModel3 = new MyTableModel3(v);
tableRicette = new JTable(tableModel3);
tableRicette.setRowHeight(20);
tableRicette.setFont(fontTable);
tableRicette.setFillsViewportHeight(true);
tableRicette.addMouseListener(new MyMouseAdapter2());
scrollPane = new javax.swing.JScrollPane();
scrollPane.setName("jScrollPane1");
scrollPane.setViewportView(tableRicette);
// table.setPreferredSize(dTable);
scrollPane.setPreferredSize(new Dimension(600,300));
scrollPane.getViewport().add(tableRicette);
scrollPane.setVisible(true);
// System.out.println(v);x
// tableModel3 = new MyTableModel3(v);
// tableRicette = new JTable(tableModel3);
//dimensionaTabella();
// tableRicette.setPreferredScrollableViewportSize(new Dimension(700, 150));
// scrollPane = new JScrollPane(tableRicette);
LabelFormat labelDescrizione = new LabelFormat("Descrizione Nota Credito: ");
textDescrizione = new TextFieldFormat();
//Put the radio buttons in a column in a panel.
JPanel radioPanel = new JPanel();
GridBagConstraints GBC5 = new GridBagConstraints();
Container CR5 = new Container();
GridBagLayout GBL5 = new GridBagLayout();
CR5.setComponentOrientation(ComponentOrientation.UNKNOWN);
CR5.setLayout(GBL5);
radioPanel.add(CR5);
GBC5 = new GridBagConstraints();
CR5.add(scrollPane);
GBC5.gridx=0;
GBC5.gridy=0;
GBC5.gridwidth=8;
GBC5.gridheight=2;
GBC5.insets.top=5;
GBC5.fill=GridBagConstraints.HORIZONTAL;
GBL5.setConstraints(scrollPane,GBC5);
GBC5 = new GridBagConstraints();
CR5.add(labelDescrizione);
GBC5.gridx=0;
GBC5.gridy=3;
GBC5.insets.top=10;
GBC5.insets.left=7;
GBC5.fill=GridBagConstraints.HORIZONTAL;
GBL5.setConstraints(labelDescrizione,GBC5);
GBC5 = new GridBagConstraints();
CR5.add(textDescrizione);
GBC5.gridx=1;
GBC5.gridy=3;
GBC5.insets.top=10;
GBC5.gridwidth=4;
GBC5.insets.left=7;
GBC5.fill=GridBagConstraints.HORIZONTAL;
GBL5.setConstraints(textDescrizione,GBC5);
GBC5 = new GridBagConstraints();
CR5.add(buttonSalva);
GBC5.gridx=1;
GBC5.gridy=4;
GBC5.insets.top=10;
GBC5.insets.left=7;
GBC5.fill=GridBagConstraints.HORIZONTAL;
GBL5.setConstraints(buttonSalva,GBC5);
GBC5 = new GridBagConstraints();
CR5.add(buttonModifica);
GBC5.gridx=2;
GBC5.gridy=4;
GBC5.insets.top=10;
GBC5.insets.left=7;
GBC5.fill=GridBagConstraints.HORIZONTAL;
GBL5.setConstraints(buttonModifica,GBC5);
GBC5 = new GridBagConstraints();
CR5.add(buttonCancella);
GBC5.gridx=3;
GBC5.gridy=4;
GBC5.insets.top=10;
GBC5.insets.left=7;
GBC5.fill=GridBagConstraints.HORIZONTAL;
GBL5.setConstraints(buttonCancella,GBC5);
add(radioPanel, BorderLayout.LINE_START);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
}
private void dimensionaTabella() {
// TODO Auto-generated method stub
v = db.eseguiQuery("SELECT DESCRIZIONE FROM DESCRIZIONENOTE ORDER BY DESCRIZIONE");
tableModel3 = new MyTableModel3(v);
tableRicette = new JTable(tableModel3);
tableRicette.setRowHeight(20);
tableRicette.setFont(fontTable);
tableRicette.addMouseListener(new MyMouseAdapter2());
// tableOperazioni.setEditingColumn(1);
scrollPane.setName("jScrollPane1");
scrollPane.setViewportView(tableRicette);
// jScrollPane2.setPreferredSize(dTable2);
}
private void pulisci(){
textDescrizione.setText("");
buttonModifica.setEnabled(false);
buttonCancella.setEnabled(false);
buttonSalva.setEnabled(true);
}
public static void createAndShowGUI() {
//Create and set up the window.
frame = new JDialog();
Dimensione d2 = new Dimensione();
d2.setSize(700, 500);
frame.setPreferredSize(d2);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
CentraComponents centra = new CentraComponents();
Point coordinate = centra.centra(frame);
frame.setLocation(coordinate);
JComponent newContentPane = new PanelNoteCredito();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public void chiudi(){
frame.dispose();
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public class MyTableModel3 extends AbstractTableModel {
Vector v = null;
// intestazioni delle colonne
String[] ColName = { "Note Descrizione Credito Registrate" };
public MyTableModel3(Vector v) {
// System.out.println("vediamo se l'errore è qui sopra");
this.v = v; // inizializzato con il vettore
// System.out.println("vediamo se l'errore è qui sotto");
// this.addMouseListener(new MyMouseAdapter());
}
/*
* public int getRowCount() { // System.out.println("Stampo"+v.size());
* return null==v?0:v.size();}
*/
public int getRowCount() {
return v.size();
}
public String getColumnName(int col) {
return ColName[col];
}
public int getColumnCount() {
return ColName.length;
}
/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col) {
// nessuna cella editabile
return false;
}
public Object getValueAt(int row, int col) {
// Object rowVal = v.get(row);
// return((Object[])rowVal)[col];//[col];
Object colonna = null;
try {
Vector riga = (Vector) v.elementAt(row);
colonna = riga.elementAt(col);
// System.out.println("stampo questo "+colonna);
return colonna;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("errore: " + e);
}
return colonna;
}
}
public class MyMouseAdapter2 extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
JTable t = (JTable)me.getSource();
if (me.getClickCount() == 2) {
int row = t.rowAtPoint(me.getPoint());
int column = t.columnAtPoint(me.getPoint());
descrVecchia= tableRicette.getValueAt(row, 0).toString();
textDescrizione.setText(descrVecchia);
buttonModifica.setEnabled(true);
buttonCancella.setEnabled(true);
buttonSalva.setEnabled(false);
}//fine if
}
}//fine mouseadapter
}
l'unica cos che devi togliere il codice relativo al d in quanto sicuramente la tua clase risulta diversa dalla mia